Using opencv in Python (2) (simple geometric image rendering)
Simple geometric images generally include vertices, straight lines, matrices, circles, decimals, and polygon. First, let's take a look at the definition of pixels in opencv. One pixel of an image has one or three values. A gray image has a gray value, and a color image has three values to form a pixel value, which shows different colors.
Points can form various polygon.
(1) draw a straight line first
Function: cv2.line (img, Point pt1, Point pt2, color, thickness = 1, line_type = 8 shift = 0)
A value indicates a default value. We can see that this function mainly accepts the coordinates of the two points. The line color (the color image is a 1*3 array) is as follows:
Import cv2import numpy as npfrom matplotlib import pyplot as pltimg = np. zeros (512,512), np. uint8) # generate an empty grayscale image cv2.line (img, (511,511), (), 255) plt. imshow (img, 'Gray ')
Import cv2import numpy as npfrom matplotlib import pyplot as pltimg = np. zeros (512,512, 3), np. uint8) # generate an empty color image cv2.line (img, (511,511), (155,155,155), (), 5) plt. imshow (img, 'brg ')
(2) Draw a rectangle
Function: cv2.rectangle (img, (random, 0), (511,111), (, 0, 0), 3). You must determine the two points of the rectangle (upper left and lower right corner), color, line type (default if not set ).
For example:
Import cv2import numpy as npfrom matplotlib import pyplot as pltimg = np. zeros (512,512, 3), np. uint8) # generate an empty color image cv2.rectangle (img, (20, 20), (411,411), (55,255,155), 5) plt. imshow (img, 'brg ')
(3) Draw a circle
It is easy to draw a circle. You only need to determine the center and radius. function:
Cv2.circle (img, (random, 0), 63, (, 0), 3), for example:
Import cv2import numpy as npfrom matplotlib import pyplot as pltimg = np. zeros (512,512, 3), np. uint8) # generate an empty color image cv2.circle (img, (200,200), 50, (55,255,155), 1) # Modify the last parameter plt. imshow (img, 'brg ')
Import cv2import numpy as npfrom matplotlib import pyplot as pltimg = np. zeros (512,512, 3), np. uint8) # generate an empty color image cv2.circle (img, (200,200), 50, (55,255,155), 8) # Modify the last parameter plt. imshow (img, 'brg ')
(4) draw an ellipse
The elliptic is complex. It involves the long axis, the short axis, the center of the elliptical circle, and the rotation angle. Let's take a look at the introduction in the opencv reference manual:
The image is as follows:
An example is as follows:
Import cv2import numpy as npfrom matplotlib import pyplot as pltimg = np. zeros (512,512, 3), np. uint8) # generate an empty color image cv2.ellipse (img, (256,256), (150,100), 180,250,-1) # Pay Attention to the last parameter-1, fill the image. By default, the image is not filled. If removed, only the plt of the elliptical contour is replaced. imshow (img, 'brg ')