FindContours for blob search

I use OpenCV findContours() to search for a blob, by filling at an arbitrary starting point on the path and accepting the bounding box of the fill box. However, when two drops touch in a corner, for example

enter image description here

they share the contour, so only one of the two drops will be flooded with a bay, depending on the selected seeding point.

I could change the fill connection setting from 4 to 8 so that the droplets are drained on the fill pack. What I really would like to do is ignore a small defect and count only a large chunk. Can this be done without a significant change in the algorithm?

+4
source share
2 answers

Unlike floodfill, there is no way to use findContours with 4 connections initially in OpenCV.

+2
source

You should take a look at the findContours() documentation .

findContours can return multiple findContours if they appear in the image, in your case, if you select a 4-connection, you should get 2 contours , and then you can compare their frame size to decide which one to hold.

 cv::Mat img = cv::imread('test.png', 0); std::vector<std::vector<cv::Point> > contours; std::vector<cv::Vec4i> hierarchy; cv::findContours(img, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); for (size_t i = 0;i < contours.size(); ++i) { cv::Rect bbox = cv::boundingRect(contours[i]); std::cout<<"Contour"<<i<<" Area"<<bbox.area()<<std::endl; } 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1485193/


All Articles