How to find No of inner Holes using cv::findcontours and hierarchy

Viewed 9057

I need to find the number of inner holes in the below image.i.e my ultimate requirement is to detect and find the area of round shape black holes alone using contour hierarchy in opencv.No need to use any other algorithms.

Based on this link Using hierarchy in findContours () in OpenCV? i tried but it won't worked.

is there any other method to find the no of holes in the image?

here with i have attached the sample image and code.Can anybody give idea to find the inner black holes alone using hierarchy.I don't have a much experience in contour hierarchy.Thanks in advance. i used opencv c++ lib.

<code>enter image description here</code>

cv::Mat InputImage = imread("New Image.jpg");
int Err;

if(InputImage.empty() == 1)
{
    InputImage.release();
    cout<<"Error:Input Image Not Loaded"<<endl;
    return 1;
}
cv::Mat greenTargetImage;

std::vector<cv::Mat> Planes;
cv::split(InputImage,Planes);

greenTargetImage = Planes[1];
cv::Mat thresholdImage = cv::Mat (greenTargetImage.size(),greenTargetImage.type());
cv::threshold(greenTargetImage,thresholdImage,128,255,THRESH_OTSU);
imwrite("thresholdImage.jpg",thresholdImage);

std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(thresholdImage,contours,hierarchy,cv::RETR_CCOMP,cv::CHAIN_APPROX_SIMPLE,cv::Point(-1,-1));
cout<<contours.size()<<endl;
cout<<hierarchy.size()<<endl;
int count = 0;
if (!contours.empty() && !hierarchy.empty())
{
    for (int i = 0;i<contours.size();i++ )
    {
        if ( hierarchy[i][3] != -1)
        {
            cv::drawContours(InputImage,contours,i,CV_RGB(0,255,0),3);
            count = count+1;
        }
    }   
}
cout<<count<<endl; //No of inner holes in same level  
imwrite("ContourImage.jpg",InputImage);

After applying this code i got the output count value is 11.But my requirement is count value should be 10 and also i need to draw only inner black holes alone not all boundaries of outer contours.Sorry for my english.

1 Answers
Related