banner



How To Draw Polygon In Python

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

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

Requirements:

  • OpenCV 3.four+
  • Python 3.vi+
  • Numpy
  • Image, Webcam or Video input
  • Documentation Source: OpenCV Official Documentation

First, you demand to setup your Python Surroundings with OpenCV. Yous can easily practice information technology by following Life2Coding's tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to make y'all 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 just in what statement(s) it accepts.

Draws several polygonal curves.

Parameters
img Prototype.
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 terminal vertex of each curve to its get-go vertex.
color Polyline colour.
thickness Thickness of the polyline edges.
lineType Type of the line segments. See LineTypes
shift Number of partial bits in the vertex coordinates.

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Proper name of the window.
mat Image to be shown.

waitKey()

retval=cv.waitKey([, filibuster])

Waits for a pressed key.

Parameters
filibuster Delay in milliseconds. 0 is the special value that means "forever".

destroyAllWindows()

None=cv.destroyAllWindows()

Destroys all of the HighGUI windows.

Steps:

  • Kickoff nosotros will create a image array using np.zeros()
  • We will define the points to create whatever kind of shapes
  • After that we will create different polygon shapes using cv2.polylines()
  • And then display the prototype using cv2.imshow()
  • Look for keyboard push 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], Truthful, (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

  • Author
  • Recent Posts

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

Source: https://www.life2coding.com/draw-polygon-on-image-using-python-opencv/

Posted by: riversmally1961.blogspot.com

0 Response to "How To Draw Polygon In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel