Draw a filled polygon using the OpenCV function fillPoly() - GeeksforGeeks

04 Jan, 2022
fillPoly() function of OpenCV is used to draw filled polygons like rectangle, triangle, pentagon over an image. This function takes inputs an image and endpoints of Polygon and color.
Syntax: cv2.fillpoly(Image,End_Points,Color)
Parameter:
Image: This is image on which we want draw filled polygon
End_Points: Points of polygon(for triangle 3 end points, for rectangle 4 end points will be there)
Color: It specifies the color of polygon
In this example we will draw filled polygon triangle by giving 3 endpoints such as [160,130],[350,130],[250,300] to fillPoly() function.
Input Image:
Code:
Python3
# Import necessary librariesimport cv2import numpy as np  # Read an imageimg = cv2.imread("image.png")  # Define an array of endpoints of trianglepoints = np.array([[160, 130], [350, 130], [250, 300]])  # Use fillPoly() function and give input as# image, end points,color of polygon# Here color of polygon will bluecv2.fillPoly(img, pts=[points], color=(255, 0, 0))  # Displaying the imagecv2.imshow("Triangle", img)  # wait for the user to press any key to # exit windowcv2.waitKey(0)  # Closing all open windowscv2.destroyAllWindows()
Output:
In this example we will draw a hexagon by giving 6 endpoints such as [220,120],[130,200],[130,300],[220,380],[310,300],[310,200] to fillPoly() function.
Input:
Code:
Python3
# Import necessary librariesimport cv2import numpy as np  # Read an imageimg = cv2.imread("image.png")  # Define an array of endpoints of Hexagonpoints = np.array([[220, 120], [130, 200], [130, 300],                   [220, 380], [310, 300], [310, 200]])  # Use fillPoly() function and give input as image,# end points,color of polygon# Here color of polygon will be greencv2.fillPoly(img, pts=[points], color=(0, 255, 0))  # Displaying the imagecv2.imshow("Hexagon", img)  # wait for the user to press any key to # exit windowcv2.waitKey(0)  # Closing all open windowscv2.destroyAllWindows()
Output:
Sometimes there is a requirement that we need to show photos of someone by hiding their faces. In this case, we can use this function to hide the face of a person.
Input:
Code:
Python3
# Import necessary librariesimport cv2import numpy as np  # Read an imageimg = cv2.imread("Documents/Person_Image.jpg",                  cv2.IMREAD_COLOR)  # Define an array of endpoints of Rectanglepoints = np.array([[300, 180], [400, 180],                    [400, 280], [300, 280]])  # Use fillPoly() function and give input as image,# end points,color of polygon# Here color of polygon will be redcv2.fillPoly(img, pts=[points], color=(0, 0, 255))  # Displaying the imagecv2.imshow("Rectangle", img)  # wait for the user to press any key to exit windowcv2.waitKey(0)  # Closing all open windowscv2.destroyAllWindows()
Output:
Picked
Python-OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Box Plot in Python using Matplotlib
Python | Get dictionary keys as a list
Bar Plot in Matplotlib
Multithreading in Python | Set 2 (Synchronization)
Python Dictionary keys() method
loops in python
Python - Call function from another file
Ways to filter Pandas DataFrame by column values
Python | Convert set into a list
Python program to find number of days between two given dates