OpenCv : draw a white filled polygon
Asked Answered
D

2

1

I'd like to draw a white filled polygon, with arbitrary angle, in a black IplImage. I know there exists function such as createCircle, but I can't find something similar for polygons. I found this , but the use of it is awful, I mean I shouldn't have to go into this just to draw one simple white polygon on a black background...!

The example I found on the OpenCV documentation:

void MyPolygon( Mat img )
{
   int lineType = 8;

   /** Create some points */
   Point rook_points[1][20];
   rook_points[0][0] = Point( w/4.0, 7*w/8.0 );
   rook_points[0][1] = Point( 3*w/4.0, 7*w/8.0 );
   rook_points[0][2] = Point( 3*w/4.0, 13*w/16.0 );
   rook_poi /*** blablabla **/
   rook_points[0][19] = Point( w/4.0, 13*w/16.0) ;

   const Point* ppt[1] = { rook_points[0] };
   int npt[] = { 20 };

   fillPoly( img,
             ppt,
             npt,
             1,
             Scalar( 255, 255, 255 ),
             lineType );
 }

Basically, my question is, how do I put a CvBox2D into fillPoly, to get a mask out of it and finally set the "ROI with angle" that I need?

Doralynne answered 31/1, 2012 at 15:31 Comment(2)
Looks like you are looking for BoxPoints.Ardis
I use a BoxPoints. The question is : how do I use it with fillPoly??Doralynne
A
3

Like this:

#include <cv.h>

void drawBox( CvArr* img, CvBox2D box, CvScalar color )
{

    CvPoint2D32f pointsf[4];
    cvBoxPoints( box , pointsf );

    CvPoint pointsi[4];

    for(int i=0;i<4;i++)
    {
        pointsi[i]=cvPointFrom32f(pointsf[i]);
    }

    CvPoint* countours[1]={
        pointsi,
    };

    int countours_n[1]={
        4,      
    };

    cvFillPoly( img, countours, countours_n, 1, color );

}
Ardis answered 31/1, 2012 at 16:13 Comment(1)
@Banthar Say, if I have 4 coordinates,(x1,y1), (x2,y2), (x3,y3) and (x4,y4) How can I write this in the form of vector.? Please help.!!Sewer
B
1

For Drawing solid filled rectangle Use thickness in draw function == CV_FILLED which will give you a solid filled rectangle & it true for any polygon shape.....

cvRectangle(img, cvPoint(x1, y1), cvPoint(x2,y2), CV_RGB(0, 255, 0), CV_FILLED, 8, 0);

Billfish answered 19/11, 2015 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.