fillPolygon API usage

Hello All,

I am fairly new to OpenCV library.

I observed a behavior of the fillPoly opencv function.

When I draw to polygons with the exact same co-ordinates twice, the fillPoly function clears the filled polygon.

I would like to know whether it is an expected beahvior of the fillPoly function

Below is the sample snippet of my code

void CreateTestImage(){

    cv::Mat image_buffer = cv::Mat::zeros( 200, 200, CV_8UC3 );
    std::vector< std::vector< cv::Point > > polygons;
    std::vector< cv::Point > poly1 {    cv::Point(155,155),
                                                              cv::Point(155,195),  
                                                              cv::Point(195,195),
                                                              cv::Point(195,155),
                                                              cv::Point(155,155),
                                    };

    polygons.push_back(poly1);
    // on addition of this line the polygon is non-filled.
   // when I comment this line, I get a filled polygon
    polygons.push_back(poly1);  

    auto ncolor = cv::Scalar(0,255,255);
    fillPoly( image_buffer,polygons,ncolor,cv::LINE_8 );
    
    std::string imageFile("test-1.png");
    cv::imwrite(imageFile,image_buffer);

}

Thanks in advance

that’s intentional.

the idea is that one can draw “holes” inside of other polygons, and also arbitrary overlapping polygons.

import numpy as np
import cv2 as cv

canvas = np.zeros((50, 50), np.float32)

poly1 = np.array([[10,20], [40,20], [40,30], [10,30]]).reshape((-1, 1, 2))
poly2 = np.array([[20,10], [30,10], [30,40], [20,40]]).reshape((-1, 1, 2))

cv.fillPoly(canvas, [poly1, poly2], 1.0, cv.LINE_8)

cv.namedWindow("canvas", cv.WINDOW_NORMAL)
cv.imshow("canvas", canvas)
cv.waitKey(-1)
cv.destroyAllWindows()

Thanks for immediate response.

The example shows there is hole created when we merge horizontal bar and vertical bar.
Is there a way to merge both without creating holes and keeping the polygon color as white using OpenCV. So the resulting polygon is filled “+”.

draw them individually.

either draw one on top of another (same picture), or draw both into separate pictures and then merge using arithmetic or binary operations.