Life2Coding
How to Draw Polygon on Image using Python OpenCV

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions. We’re going to discuss how to draw opencv shapes on images. By following the tutorial you will be able to draw any kind of polygon shapes on images. As a result, you can draw the perfect polygon shape that you want using the opencv python coding.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to make you understand how to draw polygons on image using Python OpenCV.

Documentation:

polylines()

img=cv.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]])
img=cv.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]])

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Draws several polygonal curves.

Parameters
img Image.
pts Array of polygonal curves.
isClosed Flag indicating whether the drawn polylines are closed or not. If they are closed, the function draws a line from the last vertex of each curve to its first vertex.
color Polyline color.
thickness Thickness of the polyline edges.
lineType Type of the line segments. See LineTypes
shift Number of fractional bits in the vertex coordinates.

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

waitKey()

retval=cv.waitKey([, delay])

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means “forever”.

destroyAllWindows()

None=cv.destroyAllWindows()

Destroys all of the HighGUI windows.

Steps:

  • First we will create a image array using np.zeros()
  • We will define the points to create any kind of shapes
  • After that we will create different polygon shapes using cv2.polylines()
  • Then display the image using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import numpy as np
import cv2

img = np.zeros((512, 512, 3), dtype = "uint8")

penta = np.array([[[40,160],[120,100],[200,160],[160,240],[80,240]]], np.int32)
triangle = np.array([[[240, 130], [380, 230], [190, 280]]], np.int32)
cv2.polylines(img, [triangle], True, (0,255,0), thickness=3)

img_mod = cv2.polylines(img, [penta], True, (255,120,255),3)

cv2.imshow('Shapes', img_mod)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

5cb403f457da8 How to Draw Polygon on Image using Python OpenCV
life2coding_icon [] How to Draw Polygon on Image using Python OpenCV

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.