在openCV中加入足够近的轮廓 [英] Join close enough contours in openCV

查看:200
本文介绍了在openCV中加入足够近的轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组检测到的轮廓/ blobs从一个图像。
问题是,一些块是在blob检测和平滑期间的拆分。
我尝试使用下面的代码。

I have a set of detected contours/blobs from an image. The problem is that some of the blobs are a split during blob detection and smoothing. I have tried to use the following code.


Mat outlines = Mat :: zeros(m3.size ),CV_8UC3);
findContours(m3,contour,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,Point(0,0));

Mat outlines=Mat::zeros(m3.size(),CV_8UC3); findContours(m3,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,Point(0,0));

//Approximate Contours
std::vector<std::vector<cv::Point> > contours_poly( contours.size() );
for( int i = 0; i < contours.size(); i++ ) {
    approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 5, true );
    std::vector<cv::Point> hull;
    cv::convexHull(cv::Mat(contours_poly[i]),hull);
    cv::Mat hull_points(hull);
    cv::RotatedRect rotated_bounding_rect = minAreaRect(hull_points);
    Point2f vertices[4];
    if(rotated_bounding_rect.size.area()==0){
        continue;
    }
    rotated_bounding_rect.points(vertices);
    for (int i = 0; i < 4; ++i)
    {
        cv::line(outlines, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
    }

}

问题是,即使使用 approxPolyDP 方法检测并加入这些轮廓,一些小的blob,即使他们是单独的,在附近没有任何其他blob。

The problem is that even though these contours are detected and joined using the approxPolyDP method it leads to the disappearance of some small blobs even when they are alone and do not have any other blobs in the vicinity.

我想要的是那些非常接近要加入或最少一个应该从我的轮廓列表中删除,所有其余的应该保留。

What I want is for those blobs who are very near to be joined or at least one of them should be deleted from my list of contours and all the rest should be preserved.

下面是初始和最终的图像,这将使我的问题更清楚。
初始图片:

http://sdrv.ms/1bp8x89

最终图片:

http://sdrv.ms/1bp8tp5

Below are the initial and final images which would make my question more clear. Initial Image:
http://sdrv.ms/1bp8x89
Final Image:
http://sdrv.ms/1bp8tp5

正如我们在最终图片中看到的,即使某些斑点未正确连接,图像右侧的小斑点也会丢失。

As we can see in the final image the small blob on the right side of the image goes missing even when some of the blobs have not been joined properly.

此技术对于单个帧需要几秒钟。给定一个视频,这种技术变得非常低效。

Also this technique is taking a few seconds for a single frame. Given a video this technique becomes very inefficient.

请提出一些补救措施。

Please suggest some remedies.

推荐答案

我可以建议你一个形态变化的形象叫膨胀

I can suggest you a morphological transformation of your image called dilation

wiki链接

OpenCV

这篇关于在openCV中加入足够近的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆