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.
source share