1, Line segment drawing
cv2.line(dst,(100,100),(400,400),(0,0,255),2,cv2.LINE_AA)
Parameter 1: target picture data
Parameter 2: the starting position of the current line segment (that is, two points determine a straight line)
Parameter 3: the ending position of the current line segment (that is, two points determine a straight line)
Parameter 4: current line segment color (B,G,R)
Parameter 5: width of current line segment
Parameter 6: set the type of current line segment
import cv2 import numpy as np newImageInfo = (500,500,3)#The size of the target picture dst = np.zeros(newImageInfo,np.uint8)#Create a target picture #Draw line segments cv2.line(dst,(100,100),(400,400),(0,0,255)) cv2.line(dst,(100,200),(400,200),(0,255,0),5) cv2.line(dst,(100,300),(400,300),(255,0,0),20,cv2.LINE_AA) #Of course, you can also draw triangles, rectangles and other shapes, just a few more line segments cv2.imshow('dst',dst) cv2.waitKey(0)
The renderings are as follows:
2, Rectangle, circle and freeform polygon drawing
cv2.rectangle(dst,(50,100),(200,300),(255,0,0),-1)
Parameter 1: current target image
Parameter 2: starting point of the upper left corner of the rectangle
Parameter 3: lower right corner of rectangle
Parameter 4: rectangle color
Parameter 5: fill- 1. Filling is required; Other positive values are the width of the rectangular edge
cv2.circle(dst,(250,250),(50),(0,255,0),2)
Parameter 1: current target image
Parameter 2: position of circle center
Parameter 3: radius of circle
Parameter 4: line color of drawing circle
Parameter 5: fill; If - 1 indicates filling; For other positive values, surface line width
cv2.ellipse(dst,(256,256),(150,100),0,0,180,(255,255,0),-1)
Parameter 1: current target image
Parameter 2: Oval circle
Parameter 3: the length of the axis. Because the ellipse has two axes, it has two values
Parameter 4: deflection angle
Parameter 5: starting angle of arc
Parameter 6: end angle of arc
Parameter 7: line color
Parameter 8: fill; If - 1 indicates filling; For other positive values, surface line width
import cv2 import numpy as np newImageInfo = (500,500,3) dst = np.zeros(newImageInfo,np.uint8) #draw rectangle cv2.rectangle(dst,(50,100),(200,300),(255,0,0),-1) #Draw circle cv2.circle(dst,(250,250),(50),(0,255,0),2) #Oval, sector and arc cv2.ellipse(dst,(256,256),(150,100),0,0,180,(255,255,0),-1) #Freeform polygon points = np.array([[150,50],[140,140],[200,170],[250,250],[150,50]],np.int32) #print(points.shape) points = points.reshape((-1,1,2)) #print(points.shape) cv2.polylines(dst,[points],True,(0,255,255)) cv2.imshow('dst',dst) cv2.waitKey(0)
The renderings are as follows:
3, Text and picture drawing
cv2.rectangle(img,(20,20),(250,250),(0,255,0),3)
Parameter 1: original image
Parameter 2: starting position of the box
Parameter 3: end position of box
Parameter 4: box color
Parameter 5: fill; If - 1 indicates filling; For other positive values, surface line width
cv2.putText(img,'this is a cat',(45,45),font,1,(0,0,255),2,cv2.LINE_AA)
Parameter 1: target image
Parameter 2: text content
Parameter 3: written coordinates
Parameter 4: font type
Parameter 5: font size
Parameter 6: font color
Parameter 7: font weight
Parameter 8: font line type
import cv2 import numpy as np img = cv2.imread('E:\Jupyter_workspace\study\data/cat.png',1) font = cv2.FONT_HERSHEY_SIMPLEX#Select font cv2.rectangle(img,(20,20),(250,250),(0,255,0),3)#Draw a small box cv2.putText(img,'this is a cat',(45,45),font,1,(0,0,255),2,cv2.LINE_AA) cv2.imshow('src',img) cv2.waitKey(0)
The renderings are as follows:
cv2.resize(img,(width,height))
Parameter 1: selected photos
Parameter 2: set the width and height of the photo
import cv2 img = cv2.imread('E:\Jupyter_workspace\study\data/cat.png',1) height = int(img.shape[0]*0.2) width = int(img.shape[1]*0.2) imgResize = cv2.resize(img,(width,height)) for i in range(0,height): for j in range(0,width): img[i+10,j+20] = imgResize[i,j] cv2.imshow('src',img) cv2.waitKey(0)
The renderings are as follows: