# fillPolygon API usage

**URL:** <https://forum.opencv.org/t/fillpolygon-api-usage/3899>\
**Category:** C++\
**Tags:** imgproc\
**Created:** [June 17, 2021, 6:37pm UTC](https://forum.opencv.org/t/fillpolygon-api-usage/3899 "2021-06-17T18:37:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prajith\_Warrier](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/prajith_warrier/32/2422_2.png) [@Prajith\_Warrier](https://forum.opencv.org/u/Prajith_Warrier)\
**Post date:** [June 17, 2021, 6:37pm UTC](https://forum.opencv.org/t/fillpolygon-api-usage/3899/1 "2021-06-17T18:37:02Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 17, 2021, 8:25pm UTC](https://forum.opencv.org/t/fillpolygon-api-usage/3899/2 "2021-06-17T20:25:02Z")

</div>

that’s intentional.

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

```python
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()

```

 ![poly1](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/e/ee4ae405a03beabc4d8fa9d6dcba6ee0ffa9ea16.png) ![poly1 and poly2](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/73fff1cce17bd9115ee14327f8c67c0841821fb0.png) ![poly2](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/aeb1e5c154da21e34133662e1ace6e49670f3696.png)

---

<div class="post-metadata">

**Author:** ![Prajith\_Warrier](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/prajith_warrier/32/2422_2.png) [@Prajith\_Warrier](https://forum.opencv.org/u/Prajith_Warrier)\
**Post date:** [June 18, 2021, 8:33am UTC](https://forum.opencv.org/t/fillpolygon-api-usage/3899/3 "2021-06-18T08:33:42Z")

</div>

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 “+”.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 18, 2021, 3:55pm UTC](https://forum.opencv.org/t/fillpolygon-api-usage/3899/4 "2021-06-18T15:55:21Z")

</div>

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.
