#基础几何绘制
src=np.zeros([500,500,3],dtype=np.uint8) #创建窗口
cv.line(src,(10,10),(400,400),(255,0,0),1,cv.LINE_8,0) #线
cv.rectangle(src,(100,100),(400,400),(0,255,0),-1,cv.LINE_8,0) #正方形
cv.circle(src,(250,250),150,(0,0,255),4,cv.LINE_8,0) #圆
cv.ellipse(src,(250,250),(150,50),360,0,360,(255,234,0),3,cv.LINE_8,0) #椭圆
cv.putText(src,"Hello opencv",(50,50),cv.FONT_HERSHEY_PLAIN,1.2,(0,0,255),2,cv.LINE_8) #文字
#绘制多边形
points=[]
points.append((100,100))
points.append((150, 50))
points.append((200, 100))
points.append((200, 300))
points.append((100, 300))
index=0
for point in points:
cv.line(src, point, points[(index+1)%5], (255, 0, 0), 3, cv.LINE_8, 0)
index=index+1
cv.imshow("input",src)
有趣例程:
绘制多彩圆圈
def draw_rand_circle_demo():
src=np.zeros((500,500,3),dtype=np.uint8)
for i in range(10000):
x=np.random.rand()*500 #随机生成圆的x坐标
y=np.random.rand()*500 #随机生成圆的y坐标
c=np.random.rand()*250 #随机生成圆的半径
b=np.random.rand()*255
g=np.random.rand()*255
r=np.random.rand()*255 #随机生成BGR
w=np.random.rand()*10 #随机生成宽度
cv.circle(src, (np.int(x), np.int(y)), np.int(c), (b, g, r), np.int(w), cv.LINE_8, 0) #画圈
cv.imshow("circle", src)
c = cv.waitKey(20)
if c == 27:
break #Esc
if __name__=="__main__":
draw_rand_circle_demo()
cv.waitKey(0)
cv.destroyAllWindows()