Search code examples
opencvroi

OpenCv : draw a white filled polygon


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?


Solution

  • 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 );
    
    }