Ask Your Question
0

Get the contours of largest blob

asked 2019-09-09 04:09:29 -0600

Hello, please i need your help, i can't figure out what am i doing wrong, i am trying to get the contours of the largest blob, and then draw an ellipse that englobe that blob to calculate the angle of the ellipce but i am getting this :

image description

In the line " if (area > largest_area)" : i get the largest blob and i draw a rectangle in this blob, but when i try to get the contours of the largest blob to draw an ellipse that englobe it ( line : "Ncontours.push_back(contours[i]);) " it's not working, i need your help and thank you.

//=====================================deb blob============================ int largest_area = 0; int largest_contour_index = 0; Rect bounding_rect;

    Mat thr;
    threshold(mask_combined, thr, 125, 255, THRESH_BINARY); //Threshold the gray

                                                            //test
    namedWindow("Display window11", WINDOW_AUTOSIZE);
    imshow("Display window11", thr);

    Mat kernel = Mat::ones(6, 6, CV_32F);
    Mat kernel1 = Mat::ones(4, 4, CV_32F);
    morphologyEx(thr, thr, cv::MORPH_CLOSE, kernel);
    morphologyEx(thr, thr, cv::MORPH_DILATE, kernel1); 


    vector<vector<Point>> contours; // Vector for storing contours
    vector<vector<Point>> Ncontours;
    findContours(thr, contours, RETR_CCOMP, CHAIN_APPROX_SIMPLE); // Find the contours in the image

    for (size_t i = 0; i < contours.size(); i++) // iterate through each contour.
    {
        double area = contourArea(contours[i]);  //  Find the area of contour

        if (area > largest_area)
        {
            largest_area = area;
            largest_contour_index = i;               //Store the index of largest contour
            bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
            Ncontours.push_back(contours[i]);
        }
    }
    rectangle(thr, bounding_rect, cv::Scalar(255, 255, 255));
    imshow("threshold ouput11", thr);
    vector < Point > Y;
    for (vector<vector<Point> >::iterator it = Ncontours.begin(); it != Ncontours.end(); ++it)
        Y.insert(Y.end(), it->begin(), it->end());

    if (Y.size() != 0 && Y.size() > 5)
    {
        RotatedRect e = fitEllipse(Y);
        ellipse(thr, e, Scalar(255, 255, 255), 1, 8);
        imshow("threshold ouput11", thr);

        float theta = e.angle;              // in degrees


        cout << " theta    " << theta << endl;
    }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2019-09-09 04:42:28 -0600

Witek gravatar image

Is this helping?

if (area > largest_area)
    {
        largest_area = area;
        largest_contour_index = i;               //Store the index of largest contour

    }

}
bounding_rect = boundingRect(contours[largest_contour_index]); // Find the bounding rectangle for biggest contour
Ncontours.push_back(contours[largest_contour_index]);
rectangle(thr, bounding_rect, cv::Scalar(255, 255, 255));
edit flag offensive delete link more

Comments

Thank you, i edited vector<vector<point>> Ncontours; to vector<point> Ncontours; And i used Ncontours instead of "Y", and it's working.

Kitnos gravatar imageKitnos ( 2019-09-09 07:20:19 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-09-09 04:09:29 -0600

Seen: 1,610 times

Last updated: Sep 09 '19