Ask Your Question
1

[Solved] FillConvexPoly too convex

asked 2015-12-08 05:10:04 -0600

francoisdtf gravatar image

updated 2015-12-10 04:04:28 -0600

Hi,

I'm trying to draw a filled rotated rectangle-like shape (so not exactly a rectangle, because of no square angle). I'm thus calling the function FillConvexPoly, and giving the 4 vertices of my polygon, but the result is the following :

image description

Instead, I'd like to have straight lines going from each vertice to the other. I tried adding points in the middle of the line but it still tries to make it as convex as possible.

Is there a function or a way or tuning this one allowing me to draw a filled shape with straight lines?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2015-12-08 07:35:24 -0600

LorenaGdL gravatar image

The problem is not with the function but with how you're passing the parameters. The points defining the vertices must be in the appropriate order to achieve your desired result (clockwise or counter-clockwise, but not skipping middle points).

Some examples. If the points are passed in order:

Mat image(50, 50, CV_8U, Scalar(0));
vector<Point> pts;
pts.push_back(Point(5, 10));    //top-left
pts.push_back(Point(45, 5));    //top-right
pts.push_back(Point(45, 45));   //bottom-right
pts.push_back(Point(5, 40));    //bottom-left

fillConvexPoly(image, pts, 255);

image description

If the points are not in order:

Mat image(50, 50, CV_8U, Scalar(0));
vector<Point> pts;
pts.push_back(Point(5, 10));    //top-left
pts.push_back(Point(45, 45));   //bottom-right
pts.push_back(Point(5, 40));    //bottom-left
pts.push_back(Point(45, 5));    //top-right

fillConvexPoly(image, pts, 255);

image description

So, you have to input the points in the correct order. For simple rectangle-shaped figures it might be simple enough to do it manually. However, you can do it automatically using function convexHull():

Mat image(50, 50, CV_8U, Scalar(0));
vector<Point> pts;
pts.push_back(Point(5, 10));    //top-left
pts.push_back(Point(45, 45));   //bottom-right
pts.push_back(Point(5, 40));    //bottom-left
pts.push_back(Point(45, 5));    //top-right

convexHull(pts, pts);    //reorder the points
fillConvexPoly(image, pts, 255);

image description

edit flag offensive delete link more

Comments

This definitely answers the question, thanks a lot :)

francoisdtf gravatar imagefrancoisdtf ( 2015-12-10 04:03:25 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-08 05:10:04 -0600

Seen: 4,836 times

Last updated: Dec 10 '15