Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to get 4 image points(extreme point along diagonal) from circular contour?

Hello,

I am detecting contour of circular object like this. code snippet,

`vector<vector<Point> > contours;
        vector<vector<Point> > largest_contours;
        vector<Vec4i> hierarchy;

    findContours(src_gray, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));          

    //Draw contours

    Mat drawing = Mat::zeros(src_gray.size(), CV_8UC3);
    vector<Point> approx;
    for (int i = 0; i < contours.size(); i++) 
    {

        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        //drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());//draw all contours

        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true) *0.02, true);
        if (approx.size() >= 8) //More than 6-8 vertices means its likely a circle
        {

               drawContours(drawing, contours, i, Scalar(0, 255, 0), 1, 8);
        }   
    }
    imshow("OUTPUT1", drawing);`

I am extracting only circular contour from all contour.

But how can I use this circular contour to define 4 extreme points (along 2 diagonals//4 end points) ?

so, I can use this points as image points as vector in SolvePnp algorithm.

Thanks.

How to get 4 image points(extreme point along diagonal) from circular contour?

Hello,

I am detecting contour of circular object like this. code snippet,

`vector<vector<Point> > contours;
        vector<vector<Point> > largest_contours;
         vector<Vec4i> hierarchy;

    findContours(src_gray, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));          

    //Draw contours

    Mat drawing = Mat::zeros(src_gray.size(), CV_8UC3);
    vector<Point> approx;
    for (int i = 0; i < contours.size(); i++) 
    {

        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        //drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());//draw all contours

        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true) *0.02, true);
        if (approx.size() >= 8) //More than 6-8 vertices means its likely a circle
        {

               drawContours(drawing, contours, i, Scalar(0, 255, 0), 1, 8);
        }   
    }
    imshow("OUTPUT1", drawing);`

I am extracting only circular contour from all contour.

But how can I use this circular contour to define 4 extreme points (along 2 diagonals//4 end points) ?

so, I can use this points as image points as vector in SolvePnp algorithm.

Thanks. Thanks.

How to get 4 image points(extreme point along diagonal) from circular contour?

Hello,

I am detecting contour of circular object like this. code snippet,

`vector<vector<Point> > contours;

        vector<Vec4i> hierarchy;

    findContours(src_gray, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));          

    //Draw contours

    Mat drawing = Mat::zeros(src_gray.size(), CV_8UC3);
    vector<Point> approx;
    for (int i = 0; i < contours.size(); i++) 
    {

        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        //drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());//draw all contours

        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true) *0.02, true);
        if (approx.size() >= 8) //More than 6-8 vertices means its likely a circle
        {

               drawContours(drawing, contours, i, Scalar(0, 255, 0), 1, 8);
        }   
    }
    imshow("OUTPUT1", drawing);`

I am extracting only circular contour from all contour.

But how can I use this circular contour to define 4 extreme points (along 2 diagonals//4 end points) ?

so, I can use this points as image points as vector in SolvePnp algorithm.

Thanks.

How to get 4 image points(extreme point points along diagonal) from circular contour?

Hello,

I am detecting contour of circular object like this. code snippet,

`vector<vector<Point> > contours;

        vector<Vec4i> hierarchy;

    findContours(src_gray, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));          

    //Draw contours

    Mat drawing = Mat::zeros(src_gray.size(), CV_8UC3);
    vector<Point> approx;
    for (int i = 0; i < contours.size(); i++) 
    {

        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        //drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());//draw all contours

        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true) *0.02, true);
        if (approx.size() >= 8) //More than 6-8 vertices means its likely a circle
        {

               drawContours(drawing, contours, i, Scalar(0, 255, 0), 1, 8);
        }   
    }
    imshow("OUTPUT1", drawing);`

I am extracting only circular contour from all contour.

But how can I use this circular contour to define 4 extreme points (along 2 diagonals//4 end points) ?

so, I can use this points as image points as vector in SolvePnp algorithm.

Thanks.

............................................................

Now I can get 4 points using bounding rectangle along circular contour but it sis corner points. How can I get every midpoints of this rectangle 4 lines and using correct order like top left first and so on.....

bounding_rect = boundingRect(contours[i]);
            drawContours(src, contours, i, Scalar(0, 255, 0), 1, 8);
            rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
            RotatedRect bounding_rect = minAreaRect(Mat(contours[i]));
            Point2f rect_points[4];
            bounding_rect.points(rect_points);
            for (int j = 0; j < 4; j++) {
                circle(src, rect_points[j], 2, (0, 0, 255), 8, 0);
            }
            cout << "-------------------------" << endl;
            cout << rect_points[0] << endl;
            cout << rect_points[1] << endl;
            cout << rect_points[2] << endl;
            cout << rect_points[3] << endl;
            cout << "-------------------------" << endl;

How to get 4 image points(extreme points along diagonal) from circular contour?

Hello,

I am detecting contour of circular object like this. code snippet,

`vector<vector<Point> > contours;

        vector<Vec4i> hierarchy;

    findContours(src_gray, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));          

    //Draw contours

    Mat drawing = Mat::zeros(src_gray.size(), CV_8UC3);
    vector<Point> approx;
    for (int i = 0; i < contours.size(); i++) 
    {

        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        //drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());//draw all contours

        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true) *0.02, true);
        if (approx.size() >= 8) //More than 6-8 vertices means its likely a circle
        {

               drawContours(drawing, contours, i, Scalar(0, 255, 0), 1, 8);
        }   
    }
    imshow("OUTPUT1", drawing);`

I am extracting only circular contour from all contour.

But how can I use this circular contour to define 4 extreme points (along 2 diagonals//4 end points) ?

so, I can use this points as image points as vector in SolvePnp algorithm.

Thanks.

............................................................

Now I can get 4 points using bounding rectangle along circular contour but it sis corner points. How can I get every midpoints of this rectangle 4 lines and using correct order like top left first and so on.....

bounding_rect = boundingRect(contours[i]);
            drawContours(src, contours, i, Scalar(0, 255, 0), 1, 8);
            rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
            RotatedRect bounding_rect = minAreaRect(Mat(contours[i]));
            Point2f rect_points[4];
            bounding_rect.points(rect_points);
            for (int j = 0; j < 4; j++) {
                circle(src, rect_points[j], 2, (0, 0, 255), 8, 0);
            }
            cout << "-------------------------" << endl;
            cout << rect_points[0] << endl;
            cout << rect_points[1] << endl;
            cout << rect_points[2] << endl;
            cout << rect_points[3] << endl;
            cout << "-------------------------" << endl;

How to get 4 image points(extreme points along diagonal) from circular contour?

Hello,

I am detecting contour of circular object like this. code snippet,

`vector<vector<Point> > contours;

        vector<Vec4i> hierarchy;

    findContours(src_gray, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));          

    //Draw contours

    Mat drawing = Mat::zeros(src_gray.size(), CV_8UC3);
    vector<Point> approx;
    for (int i = 0; i < contours.size(); i++) 
    {

        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        //drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());//draw all contours

        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true) *0.02, true);
        if (approx.size() >= 8) //More than 6-8 vertices means its likely a circle
        {

               drawContours(drawing, contours, i, Scalar(0, 255, 0), 1, 8);
        }   
    }
    imshow("OUTPUT1", drawing);`

I am extracting only circular contour from all contour.

But how can I use this circular contour to define 4 extreme points (along 2 diagonals//4 end points) ?

so, I can use this points as image points as vector in SolvePnp algorithm.

Thanks.

............................................................

Now I can get 4 points using bounding rectangle along circular contour but it sis is corner points. How can I get every midpoints of this rectangle 4 lines and using correct order like top left first and so on.....

bounding_rect = boundingRect(contours[i]);
            drawContours(src, contours, i, Scalar(0, 255, 0), 1, 8);
            rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
            RotatedRect bounding_rect = minAreaRect(Mat(contours[i]));
            Point2f rect_points[4];
            bounding_rect.points(rect_points);
            for (int j = 0; j < 4; j++) {
                circle(src, rect_points[j], 2, (0, 0, 255), 8, 0);
            }
            cout << "-------------------------" << endl;
            cout << rect_points[0] << endl;
            cout << rect_points[1] << endl;
            cout << rect_points[2] << endl;
            cout << rect_points[3] << endl;
            cout << "-------------------------" << endl;

How to get 4 image points(extreme points along diagonal) from circular contour?

Hello,

I am detecting contour of circular object like this. code snippet,

`vector<vector<Point> > contours;

        vector<Vec4i> hierarchy;

    findContours(src_gray, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));          

    //Draw contours

    Mat drawing = Mat::zeros(src_gray.size(), CV_8UC3);
    vector<Point> approx;
    for (int i = 0; i < contours.size(); i++) 
    {

        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        //drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());//draw all contours

        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true) *0.02, true);
        if (approx.size() >= 8) //More than 6-8 vertices means its likely a circle
        {

               drawContours(drawing, contours, i, Scalar(0, 255, 0), 1, 8);
        }   
    }
    imshow("OUTPUT1", drawing);`

I am extracting only circular contour from all contour.

But how can I use this circular contour to define 4 extreme points (along 2 diagonals//4 end points) ?

so, I can use this points as image points as vector in SolvePnp algorithm.

Thanks.

............................................................

Now I can get 4 points using bounding rectangle along circular contour but it is corner points. How can I get every midpoints of this rectangle 4 lines and using correct order like top left first and so on.....

bounding_rect = boundingRect(contours[i]);
            drawContours(src, contours, i, Scalar(0, 255, 0), 1, 8);
            rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
            RotatedRect bounding_rect = minAreaRect(Mat(contours[i]));
            Point2f rect_points[4];
            bounding_rect.points(rect_points);
            for (int j = 0; j < 4; j++) {
                circle(src, rect_points[j], 2, (0, 0, 255), 8, 0);
            }
            cout << "-------------------------" << endl;
            cout << rect_points[0] << endl;
            cout << rect_points[1] << endl;
            cout << rect_points[2] << endl;
            cout << rect_points[3] << endl;
            cout << "-------------------------" << endl;