C++ 在 openCV 中加入足够接近的轮廓

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19123165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:31:52  来源:igfitidea点击:

Join close enough contours in openCV

c++opencv

提问by Sohaib

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.

我有一组从图像中检测到的轮廓/斑点。问题在于,某些 blob 在 blob 检测和平滑期间是分裂的。我尝试使用以下代码。

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);
    }
}

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

问题是,即使使用该approxPolyDP方法检测并连接了这些轮廓,它也会导致一些小斑点消失,即使它们单独存在并且附近没有任何其他斑点。

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.

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

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

以下是初始和最终图像,它们将使我的问题更加清晰。初始图像:
http: //sdrv.ms/1bp8x89
最终图像: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.

正如我们在最终图像中看到的那样,即使某些 blob 没有正确连接,图像右侧的小 blob 也会丢失。

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

此外,此技术需要几秒钟才能获得一帧。给定视频,这种技术变得非常低效。

Please suggest some remedies.

请建议一些补救措施。

回答by Michele mpp Marostica

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

我可以建议你对你的图像进行形态变换,称为膨胀

wiki link

维基链接

OpenCV

OpenCV

回答by blacatus

Hope this link to another similar question works. They solve this problem in a rude way. If it works, go for optimization.

希望这个指向另一个类似问题的链接有效。他们以粗鲁的方式解决了这个问题。如果有效,请进行优化。

Regarding any libraries with functions that attemp to solve this problem, there are two in openCV (groupRectangles & partition) but they may not solve your problem.

关于任何具有试图解决此问题的函数的库,openCV 中有两个(groupRectangles & partition),但它们可能无法解决您的问题。

Here is the link: https://dsp.stackexchange.com/questions/2564/opencv-c-connect-nearby-contours-based-on-distance-between-them

这是链接:https: //dsp.stackexchange.com/questions/2564/opencv-c-connect-nearby-contours-based-on-distance-between-them