Simple geometric images typically include points, lines, matrices, circles, ellipses, polygons, and so on. First of all, recognize OpenCV's definition of pixel points.
A pixel of an image has 1 or 3 values. Grayscale image has a gray value, the color image has 3 values to form a pixel value. They show a different color.
Then there is a little talent to make up a variety of polygons.
(i) Draw a line first
Function: Cv2.line (img,point pt1,point pt2,color,thickness=1,line_type=8 shift=0)
The representative with the value has a default value. You don't have to. Can see that this function mainly accepts the coordinates of the number of two points, the color of the line (color Image color is a 1*3 array) such as the following:
import cv2importas npfromimportas pltimg = np.zeros((512,512),np.uint8)#生成一个空灰度图像cv2.line(img,(0,0),(511,511),255,5)plt.imshow(img,‘gray‘)
import cv2importas npfromimportas pltimg = np.zeros((512,512,3),np.uint8)#生成一个空彩色图像cv2.line(img,(0,0),(511,511),(155,155,155),5)plt.imshow(img,‘brg‘)
(ii) Draw a rectangle
Functions: Cv2.rectangle (IMG, (380,0), (511,111), (255,0,0), 3), the two points of the rectangle (upper-left corner and lower-right corner) need to be determined. Color, the type of line (not set to default).
For example:
import cv2importas npfromimportas pltimg = np.zeros((512,512,3),np.uint8)#生成一个空彩色图像cv2.rectangle(img,(20,20),(411,411),(55,255,155),5)plt.imshow(img,‘brg‘)
(iii) Draw round
Drawing a circle is also very easy, just need to determine the center and radius, function:
Cv2.circle (IMG, (380,0), 63, (255,0,0), 3), for example:
import cv2importas npfromimportas pltimg = np.zeros((512,512,3),np.uint8)#生成一个空彩色图像cv2.circle(img,(200,200),50,(55,255,155),1)#改动最后一个參数plt.imshow(img,‘brg‘)
import cv2importas npfromimportas pltimg = np.zeros((512,512,3),np.uint8)#生成一个空彩色图像cv2.circle(img,(200,200),50,(55,255,155),8)#改动最后一个參数plt.imshow(img,‘brg‘)
(d) Draw an ellipse
The
Ellipse is more complex, involving the long axis short axis, ellipse center, rotation angle, and so on, see the introduction of OPENCV reference manual:
Image for example:
Example:
import cv2import numpy as npfrom Matplotlib import pyplot as pltimg = Np.zeros (( Span class= "Hljs-number" >512 , 512 , 3 ), np.uint8) #生成一个空彩色图像 cv2.ellipse (img, 256 , Span class= "Hljs-number" >256 ), (150 , 100 ), 0 , 0 , 180 , Span class= "Hljs-number" >250 ,-1 ) #注意最后一个參数-1, Indicates that the image is populated and is not populated by default, assuming that the ellipse outlines only the plt.imshow (img,)
Python OpenCV using Notes (ii) (Simple geometric image drawing)