这里用到了三个函数:cv2.line();cv2.rectangle();cv2.circle();cv2.ellipse();cv2.putText(); cv2.polylines(); cv2.fillConvexPoly();cv2.fillPoly()
下面看一下示例代码:
import cv2
import numpy as np
img = np.zeros((512,512,3),np.uint8)
#画线
cv2.line(img,(0,0),(512,512),(255,0,0),5)
#画矩形
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
#画圆
cv2.circle(img,(447,63),63,(0,0,255),-1)
#画椭圆
cv2.ellipse(img,(256,256),(100,50),0,0,360,255,-1)
#画多边形
pts=np.array([[10,5],[300,256],[70,20],[50,10]], np.int32)
pts=pts.reshape((-1,1,2))
cv2.polylines(img,[pts],False,(0,255,0),3)#False:开口即多条线
#在图片上添加文字
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'opencv',(10,500),font,4,(255,255,255),2)
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = np.zeros((512,512,3),np.uint8)
# #填充单个多边形
rect1 = np.array([[400,400],[400,500],[500,500],[500,400]])
cv2.fillConvexPoly(img,rect1,(0,255,0))
# #填充多个多边形
triangle = np.array([[440,100 ], [200, 300], [500, 400]])
rect = np.array([[20,20],[20,400],[400,400],[400,20]])
cv2.fillPoly(img,[triangle,rect],(255,255,0))
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()