This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

The environment

  • windows 10 64bit
  • Opencv 4.5.2

The test image

Prepare a test picture

Draw a straight line

Import cv2 image = cv2.imread('test.png') # Draw a blue line from (50, 50) to (300, 100) with thickness of 3. The color is BGR sequence. Line (image, (50, 50), (300, 100), (255, 0, 0), 3) cv2.imshow('Draw line', Image) cv2.waitKey(0) # Delete all Windows cv2.DestroyallWindows ()Copy the code

Draw a rectangle

Import cv2 image = cv2.imread('test.png') # import cv2 image = cv2.imread('test.png') # import cv2 image = cv2.imread('test.png') # import cv2 image = cv2.imread('test.png') # Cv2. Rectangle (image, (50, 50), (300, 100), (255, 0, 0), 3) cv2. Imshow ('Draw rectangle', Image) cv2.waitKey(0) # Delete all Windows cv2.DestroyallWindows ()Copy the code

A circle

Import cv2 image = cv2.imread('test.png') # cv2 image = cv2.imread('test.png') # cv2 image = cv2.imread('test.png') # Cv2. circle(image, (80, 80), 50, (255, 0, 0), 2) cv2.imshow('Draw circle', Image) cv2.waitKey(0) # Delete all Windows cv2.DestroyallWindows ()Copy the code

If you set the last parameter to -1, you fill the circle with the corresponding color, i.e. draw a solid circle

cv2.circle(image, (80, 80), 50, (255, 0, 0), -1)
Copy the code

Draw the ellipse

Import cv2 image = cv2.imread('test.png') # the center coordinates of the ellipse (200, 30) and the tuples (100,10) represent the lengths of the major and minor axes respectively, followed by the 0 argument which is the rotation Angle of the ellipse in degrees, followed by the start and end angles of the ellipse, 0 and 50, the color of the border is blue, and the thickness of the final border, Cv2. Ellipse (image, (200, 30), (100, 10), 0, 0, 50, (255, 0, 0), -1) Image) cv2.waitKey(0) # Delete all Windows cv2.DestroyallWindows ()Copy the code

Draw a polygon

Import cv2 import numpy as NP image = cv2.imread('test.png') # PTS = np.array([[20, 20], [40, 10], [70, 50], [60, 80], [30, 100]], np.int32) # the first parameter is -1, indicating that the length of this dimension is calculated based on the following dimensions PTS = PTS. 0 Cv2. polylines(image, [PTS], True, (255, 0, 0)) cv2.imshow('Draw polylines', Image) cv2.waitKey(0) # Delete all Windows cv2.DestroyallWindows ()Copy the code

If you change the third argument in cv2.polylines from True to False, the polygon is not closed, as follows