0
votes

i m new in this topic.i'm trying to use some openCV library to make a project but i have some problems with findContour,drawContours.After i read an input image and make a tresholding , i use findContours as in the code

cv::Mat cont;    // i create a matrix


    result2.copyTo(cont);  // this is the copy of the input image tresholded    
    std::vector<std::vector<cv::Point> > contours;          
    std::vector<cv::Vec4i> hierarchy;
            cv::findContours(cont,contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,  cv::Point(0,0 ));

    eiid::imshow("Im_Find", cont);   // I watch the points
            for( int s = 0; s< contours.size(); s++ )
    {

     printf(" * Contour[%d]=  Area OpenCV: %.2f \n", s,cv::contourArea(contours[s])) ;   
     drawContours( cont, contours, s, cv::Scalar(255,255), 10, 1, hierarchy, 0, cv::Point() );          
 }
   eiid::imshow ( "FINE" , cont);

I have 7 elements connected, ( so 7 areaContours) This is my objective: knowing the areas of my all elemets, I want to draw, to paint, only the biggest area (and so exclude the other 6 areas ) I can't do it , someone can help me please? (I tried to store my 7 areas in double array but i can't go on :( )

Thanks to answer me man, but i still have a problem. Your code works but i don't know why some pixels are still in the image... When i watch the final image i see my biggest contour but also some white pixels :( .. The problem is that i have to use this algoritm for more pictures, so my code has to be standard for all pictures..Maybe i use uncorrectly the functions??


My image is black and white but Color(255,255) i think is good in my case. Howerver i understood my problem is that findContours put pixels in my image,so i copyed in other image and now i don t have problems. HOwever i still have a problem :( Now i have the biggest contour,but i need to apply it on my original image to exclude some text from it..The problem is that the inside of the biggest contour is black , but in my original image , the inside is white...so i can 't get a operation like " less - " (interseption) ....:( how can i do man?

2
Is the image color or grayscale? I don't think this would be the problem, but you should change cv::Scalar(255,255) to cv::Scalar(255) is grayscale or cv::Scalar(255,255,255) if color.David Nilosek

2 Answers

0
votes

Just find the biggest one first, then draw.

int idx = -1;
double maxArea = 0.0;

for( int s = 0; s< contours.size(); s++ )
 {
  double area = cv::contourArea(contours[s]);
  if( area > maxArea){
         maxArea = area;
         idx = s;
    }
 }  

 cv::drawContours( cont, contours, idx , cv::Scalar(255,255), 10, 1, hierarchy, 0, cv::Point() );          
0
votes

There we go. (FYI: try not to be lazy and figure out what happens in my function below.

cv::Mat findBiggestBlob(cv::Mat & matImage){
    int largest_area=0;
    int largest_contour_index=0;

    vector< vector<Point> > contours; // Vector for storing contour
    vector<Vec4i> hierarchy;

    findContours( matImage, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image

    for( int i = 0; i< contours.size(); i++ ) {// iterate through each contour. 
        double a=contourArea( contours[i],false);  //  Find the area of contour
        if(a>largest_area){
            largest_area=a;
            largest_contour_index=i;                //Store the index of largest contour
            //bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
        }
    }

    drawContours( matImage, contours, largest_contour_index, Scalar(255), CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
    return matImage;
}