https://answers.opencv.org/question/87583/detach-blobs-with-a-contact-point/

一、问题描述
带有接触点的斑点时遇到问题,需要从上图中区分出每一个物件。最后能够得到类似这样的结果:
 

二、难点分析
 
简单2值化后会发现主要存在2个问题:1个是存在粘连的情况; 1个是目标物体不是全部一样的,有两个洞的情况、有一个洞的情况。

三、可行建议
1、A basic idea based on morphological operations and distance transformation(代码1)
 
主要是通过距离变化,能够将这三个物件区分开来,虽然解决了粘连问题,但是在形态学变化的过程中,孔洞的信息丢失掉了;虽然可以使用findblobs等方法找到孔洞,但是孔洞和物件之间的连接信息也肯定会丢掉。
2、基于凸性缺陷的试用代码为您提供了一些关键点(代码2)
3、它进一步进行了优化(代码3)
这里就是能够将缺陷的部分给检测出来了,方便后面进行分割。

四、小结反思
这个例子并没有给出完整的解答,最大的价值在于说明了“通过轮廓缺陷检测的方法解决粘连问题”的思路和方法。
代码1:
#include "pch.h"
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>
#include "gocvhelper.h"
using namespace std;
using namespace cv;
//2020年11月8日10:26:09
//ssd 流水线文件
int main(int argc, char *argv[])
{
    // Load your image
    cv::Mat src = cv::imread("e:/template/twoblobs_1.bmp");
    // Check if everything was fine
    if (!src.data)
        return -1;
    // Show source image
    cv::imshow("src", src);
    // Create binary image from source image
    cv::Mat gray;
    cv::cvtColor(src, gray,COLOR_BGR2GRAY);
    //cv::imshow("gray", gray);
    // Obtain binary image
    Mat bw;
    cv::threshold(gray, bw, 40, 255, cv::THRESH_BINARY_INV|cv::THRESH_OTSU);
    cv::imshow("bin", bw);
    // Erode a bit 
    Mat kernel = Mat::ones(3, 3, CV_8UC1);    
    erode(bw, bw, kernel);
    //imshow("erode", bw);
    // Perform the distance transform algorithm 
    Mat dist;    
    distanceTransform(bw, dist, cv::DIST_L2, 5);
    // Normalize the distance image for range = {0.0, 1.0} 
    // so we can visualize and threshold it    
    normalize(dist, dist, 0, 1., NORM_MINMAX);    
    imshow("distTransf", dist);
    // Threshold to obtain the peaks
    // This will be the markers for the foreground objects
    threshold(dist, dist, .5, 1., cv::THRESH_BINARY);
    // Dilate a bit the dist image, this can be optional since in
    // other use case might cause problems. Here though it works quite well
    Mat kernel1 = Mat::ones(5, 5, CV_8UC1);
    dilate(dist, dist, kernel1, Point(-1, -1), 2);
    imshow("peaks", dist);
    // Create the CV_8U version of the distance image
    // It is needed for findContours()
    Mat dist_8u;
    dist.convertTo(dist_8u, CV_8U);
    // Find total markers
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;
    findContours(dist_8u, contours, hierarchy, cv::RETR_TREE,cv::CHAIN_APPROX_SIMPLE);
    // Find the rotated rectangles
    vector<RotatedRect> minRect(contours.size());
    for (size_t i = 0; i < contours.size(); i++)
    {
        minRect[i] = minAreaRect(Mat(contours[i]));
    }
    RNG rng(12345);
    for (size_t i = 0; i < contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        // contour
        drawContours(src, contours, static_cast<int>(i), color, 1, 8, vector<Vec4i>(), 0, Point());
        // rotated rectangle
        Point2f rect_points[4]; minRect[i].points(rect_points);
        for (int j = 0; j < 4; j++)
            line(src, rect_points[j], rect_points[(j + 1) % 4], color, 1, 8);
    }
    /* From here you can extract the orientation of each object by using
    * the information that you can extract from the contours and the
    * rotate rectangles. For example, the center point, rectange angle, etc...
    */
    cv::imshow("result", src);
    waitKey(0);
    return 0;
}

代码2:
#include "pch.h"
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>
#include "gocvhelper.h"
using namespace std;
using namespace cv;
//2020年11月8日10:26:09
//ssd 流水线文件
int main(int argc, char *argv[])
{
    // Load your image
    cv::Mat src = cv::imread("e:/template/twoblobs_1.bmp");
    if (src.empty())
        return -1;
    Mat bw;
    cvtColor(src, bw, COLOR_BGR2GRAY);
    bw = bw < 60;
    // Find contours
    vector<vector<Point> > contours;
    vector<int> contoursHull;
    vector<Vec4i> defects;
    findContours(bw, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
    for (size_t i = 0; i < contours.size(); i++)
    {
        if (contourArea(contours[i]) > 500)
        {
            approxPolyDP(contours[i], contours[i], 9, true);//多边形拟合
            convexHull(contours[i], contoursHull, true);//寻找凸包
            convexityDefects(contours[i], contoursHull, defects);//计算缺陷
            for (size_t j = 0; j < defects.size(); j++)
            {
                Vec4i defpoint = defects[j];
                circle(src, contours[i][defpoint[2]], 2, Scalar(0, 255, 0), 1);
            }
        }
    }
    imshow("result", src);
    waitKey();
    return 0;
}

代码3:
#include "pch.h"
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>
#include "gocvhelper.h"
using namespace std;
using namespace cv;
//2020年11月8日10:26:09
//ssd 流水线文件
int main(int argc, char *argv[])
{
    // Load your image
    cv::Mat src = cv::imread("e:/template/twoblobs_1.bmp");
    if (src.empty())
        return -1;
    Mat bw;
    cvtColor(src, bw, COLOR_BGR2GRAY);
    bw = bw < 60;
    // Find contours
    vector<vector<Point> > contours;
    vector<int> contoursHull;
    vector<Vec4i> defects;
    findContours(bw.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
    for (size_t i = 0; i < contours.size(); i++)
    {
        if (contourArea(contours[i]) > 500)
        {
            approxPolyDP(contours[i], contours[i], 2, true);
            convexHull(contours[i], contoursHull, true);
            convexityDefects(contours[i], contoursHull, defects);
            for (size_t j = 0; j < defects.size(); j++)
            {
                Vec4i defpoint = defects[j];
                Point pt = contours[i][defpoint[2]]; // get defect point
                Rect r3x3(pt.x - 2, pt.y - 2, 5, 5); // create 5x5 Rect from defect point
                // maybe no need but to be sure that the rect is in the image
                r3x3 = r3x3 & Rect(0, 0, bw.cols, bw.rows);
                int non_zero_pixels = countNonZero(bw(r3x3));
                cout << non_zero_pixels << endl;
                if (non_zero_pixels > 17)
                    circle(src, contours[i][defpoint[2]], 2, Scalar(0, 255, 0), 1);
            }
        }
    }
    imshow("result", src);
    waitKey();
    return 0;
    waitKey();
    return 0;
}

代码4
Here's the code for the axes and the curve:
// Drawing orientation angle
float angle = -rod.orientation;
float length = rod.length/5;
ellipse(image, rod.barycenter, Size(rod.length/6,rod.length/6), 0, 0, -rod.orientation, Scalar(0,0,255));
Point2f P2;
P2.x =  (rod.barycenter.x + length * cos(angle * CV_PI / 180.0));
P2.y =  (rod.barycenter.y + length * sin(angle * CV_PI / 180.0));
arrowedLine(image, rod.barycenter, P2, Scalar(255,255,255));
P2.x =  (rod.barycenter.x + length * cos(0));
P2.y =  (rod.barycenter.y + length * sin(0));
arrowedLine(image, rod.barycenter, P2, Scalar(0,0,255));

最新文章

  1. webapi返回json格式优化
  2. java的VO和PO的用途
  3. [Android Pro] 网络流量安全测试工具Nogotofail
  4. UVA 12380 Glimmr in Distress --DFS
  5. FileOutputStream与FileInputStream互相转换
  6. 定义提示符 PS1 PS4
  7. Hive常用的SQL命令操作
  8. 异常:java.lang.UnsupportedOperationException: Manual close is not allowed over a Spring managed SqlSession
  9. 在 sys.servers 中找不到服务器的解决办法,自己解决的
  10. springmvc入门demo
  11. CentOS 6.5 安装Suricata(with PF_RING &amp; CUDA)
  12. 安装linux环境及相关包方法
  13. 一、Kubernetes系列之介绍篇
  14. FileDb
  15. python爬虫积累(一)--------selenium+python+PhantomJS的使用(转)
  16. Android学习笔记(五一):服务Service(上)- IntentService
  17. python性能分析——insert()
  18. PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!
  19. 【Unity】2.2 Unity编辑器中的常用菜单项
  20. PHP获取代码段执行的毫秒时间和消耗内存

热门文章

  1. 2016 最新的 树莓派3 Raspberry Pi 3 上手评测 图解教程 新手必看!(VNC 安装,启动,关闭)
  2. vue-cli &amp; plugin:vue/strongly-recommended bug
  3. 微信小程序 TypeScript bug
  4. Flutter 使用p5
  5. RocketMq灰皮书(二)------本地部署启动MQ
  6. 08.手写KNN算法测试
  7. Python学习笔记_类
  8. 11_MySQL如何让数据分页显示
  9. nginx日志文件切分
  10. 【原创】Linux虚拟化KVM-Qemu分析(十)之virtio驱动