Ask Your Question
1

Convert point vector to Mat

asked 2013-09-29 17:01:06 -0600

Jorge Pereira gravatar image

updated 2013-09-30 10:51:30 -0600

Hi, my objective is to obtain the right side of this image.

I thought I'd see the image as a polygon because I have the coordinates of the vertices, and then transform the vector that has the vertices in a matrix (cvMat).

My thought is correct or is there a simpler way to get this submatrix?

With the following code I can draw the polygon but I need to convert it to a matrix.

Point normImgTop, normImgEyebrowInner, normImgCNoseTip, normImgCNoseBase, normImgTipOfChin, normImgTopLip, normImgDownLip, normImgBottom, normImgTopRight, normImgBottomRight;

(...)

        std::vector<Point> contour;
        Mat roi(poseNormImg.rows, poseNormImg.cols, CV_8UC1);

        contour.push_back(normImgTop);
        contour.push_back(normImgEyebrowInner);
        contour.push_back(normImgCNoseTip);
        contour.push_back(normImgCNoseBase);
        contour.push_back(normImgTopLip);
        contour.push_back(normImgDownLip);
        contour.push_back(normImgTipOfChin);
        contour.push_back(normImgBottom);
        contour.push_back(normImgBottomRight);
        contour.push_back(normImgTopRight);

        const cv::Point *pts = (const cv::Point*) Mat(contour).data;
        int npts = Mat(contour).rows;
        std::cout << "Number of polygon vertices: " << npts << std::endl;

        // draw the polygon
        polylines(roi, &pts,&npts, 1,
                            true,           // draw closed contour (i.e. joint end to start)
                            Scalar(0,255,0),// colour RGB ordering (here = green)
                            3,              // line thickness
                            CV_AA, 0);

I'm using OpenCV 2.4.6.1 and C++.

Thank you

edit retag flag offensive close merge delete

Comments

hmm, the error seems to come from cvConvertImage, you don't show enough code ?

berak gravatar imageberak ( 2013-09-30 02:57:49 -0600 )edit

Hi, thank you for your answer. I rephrased my question and added some code.

Jorge Pereira gravatar imageJorge Pereira ( 2013-09-30 10:53:00 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-09-30 12:11:10 -0600

jecs89 gravatar image

I can recommend you.

Mat image_src;
image_src = imread( "000092.jpg", 1);
Mat lung_left( image_src, Rect( 55, 100, 195, 290)); // Rect( , , , ) crop image from x,y to x+i, y+j

Tell me, how it works for you.

edit flag offensive delete link more

Comments

Hi, I already have submatrixs obtained like that, the problem is that this submatrix is not a rectangle, its a polygon.

Jorge Pereira gravatar imageJorge Pereira ( 2013-09-30 13:25:33 -0600 )edit
0

answered 2013-09-30 13:12:31 -0600

updated 2013-09-30 13:15:00 -0600

You have to change your code to below.

       std::vector<Point> contour;

        contour.push_back(normImgTop);
        contour.push_back(normImgEyebrowInner);
        contour.push_back(normImgCNoseTip);
        contour.push_back(normImgCNoseBase);
        contour.push_back(normImgTopLip);
        contour.push_back(normImgDownLip);
        contour.push_back(normImgTipOfChin);
        contour.push_back(normImgBottom);
        contour.push_back(normImgBottomRight);
        contour.push_back(normImgTopRight);

        vector<vector<Point>> contours;
         contours.push_back(contour):
        // draw the polygon
        polylines(roi, contours, true, Scalar(255), CV_AA);
edit flag offensive delete link more

Comments

Hi, thank you for your answer. This codes gives me the same result, "just" the draw. Do you know how can I convert it to a Mat?

Jorge Pereira gravatar imageJorge Pereira ( 2013-09-30 14:17:35 -0600 )edit

With this code I was able to get the left side! How can I obtain the right one?...

    std::vector&lt;Point&gt; contour;
    Mat roi(poseNormImg.rows, poseNormImg.cols, CV_8UC1);

    contour.push_back(normImgTop);
    contour.push_back(normImgEyebrowInner);
    contour.push_back(normImgCNoseTip);
    contour.push_back(normImgCNoseBase);
    contour.push_back(normImgTopLip);
    contour.push_back(normImgDownLip);
    contour.push_back(normImgTipOfChin);
    contour.push_back(normImgBottom);
    contour.push_back(normImgBottomRight);
    contour.push_back(normImgTopRight);

    vector&lt; vector&lt;Point&gt; &gt; contours;

    contours.push_back(contour);

    // draw the polygon
    polylines(roi, contours, true, Scalar(255), CV_AA);

    Mat mask = c
Jorge Pereira gravatar imageJorge Pereira ( 2013-09-30 14:37:07 -0600 )edit

Question Tools

Stats

Asked: 2013-09-29 17:01:06 -0600

Seen: 5,160 times

Last updated: Sep 30 '13