Vishnu Dasu
Vishnu Dasu

Reputation: 543

Drawing Trapezium shaped ROI OpenCV C++

I'm working on a lane detection problem using OpenCV in C++. To select the ROI, I'm planning to use a trapezium shape mask to filter the region. However, I'm unable to figure out how to draw a trapezium. After choosing the four vertices, a differently shaped polygon is being drawn. Here's my code.

void select_roi(Mat &frame){
    int rows = frame.rows;
    int cols = frame.cols;

    Point points[1][4];
    points[0][0] = Point(cols*0.05, rows);
    points[0][1] = Point(cols*0.4, rows*0.4);
    points[0][2] = Point(cols*0.95, rows);
    points[0][3] =  Point(cols*0.6, rows*0.4);

    Mat img = empty_image(frame); //User defined function that returns empty image of frame dimensions
    const Point* ppt[1] = {points[0]};
    int npt[] = {4};
    fillPoly(img, ppt, npt, 1, Scalar(255,0,0), 8);
    imshow("Poly",img);
}

Upvotes: 1

Views: 2423

Answers (1)

alkasm
alkasm

Reputation: 23012

Really simple error: your points are not in the right order. Currently you're drawing the bottom left, then the top left, then the bottom right, and then the top right, and connecting back to the first. Imagine if you traced it out with those coordinates in order---you'd get the same shape that you have in error. So all you need to do is order them so that if you drew lines from each point to the next, you'd create the shape.

points[0][0] = Point(cols*0.05, rows);
points[0][1] = Point(cols*0.4, rows*0.4);
points[0][2] = Point(cols*0.6, rows*0.4);
points[0][3] = Point(cols*0.95, rows);

Upvotes: 1

Related Questions