使用opencv来画图,主要有这几种常见的画图方法:
参数说明:
设置空白画布
import numpy as np
import cv2
img = np.zeros((500,500,3),np.uint8)
本质是创建一个500x500x3的0矩阵,用于存储500x500的3通道图像,数据类型为uint8
参数:cv2.line(img,pt1,pt2,color,thinkness,lineType,shift)
pt1,pt2分别代表起点和终点的坐标
import cv2
import numpy as np
img = np.zeros((500,500,3),np.uint8)
cv2.line(img,(0,0),(500,500),(123,231,0),2) # 颜色自己可以定义
cv2.line(img,(500,0),(0,500),(222,100,0),2)
cv2.line(img,(0,250),(500,250),(5,25,80),2)
cv2.line(img,(250,0),(250,500),(255,255,255),2)
cv2.imshow('line',img)
cv2.waitKey()
cv2.circle(img,center,radius,color,thinkness,lineType,shift)
center为圆心 radius为半径
import cv2
import numpy as np
img = np.zeros((500,500,3),np.uint8)
cv2.circle(img,(255,255),100,(21,123,0),-1)
cv2.circle(img,(100,100),100,(32,43,232),2,shift = 1)
cv2.imshow('circle',img)
cv2.waitKey()
cv2.rectangle(img,pt1,pt2,color,thinkness,lineType,shift)
pt1左上点坐标 pt2右下点坐标
import cv2
import numpy as np
img = np.zeros((500,500,3),np.uint8)
cv2.rectangle(img,(200,200),(250,350),(231,33,0),2)
cv2.rectangle(img,(250,250),(350,300),(32,240,121),-1) # -1表示按照本颜色填充
cv2.imshow('rectangle',img)
cv2.waitKey()
cv2.ellipse(img,center,axes,angle,startAngle,endAngle,color,thinkness,lineType,shift)
center:椭圆中心
axes:(长轴,短轴)
angle:椭圆绕中心顺时针旋转度数
startAngle:绕中心绘制起始角
endAngle:绕中心绘制结束角
import cv2
import numpy as np
img = np.ones((500,500,3),np.uint8)
cv2.ellipse(img,(250,250),(100,200),0,0,360,(255,32,0),2)
cv2.imshow('ellipse',img)
cv2.waitKey()
cv2.polylines(img,pts,isClosed,color,thinkness,lineType,shift)
cv2.fillPoly(img,pts,color,thinkness,lineType,shift,offset) 填充
import numpy as np
import cv2
img = np.zeros((500,500,3),np.uint8)
rect = np.array([[[200,200],[300,400],[100,400]]],np.int32)
# cv2.fillPoly(img,rect,(233,43,0)) # 可以填充
cv2.polylines(img,rect,1,(233,43,0)) # 无法填充
cv2.imshow('duobianxing',img)
cv2.waitKey()
import numpy as np
import cv2
img = np.zeros((500,500,3),np.uint8)
line1 = np.array([[324, 60], [254, 300]], np.int32)
line2 = np.array([[170, 70], [250, 350]], np.int32)
line3 = np.array([[110, 90], [176, 380]], np.int32)
img = cv2.polylines(img, [line1, line2, line3], False, (0, 255, 255))
cv2.imshow('lines',img)
cv2.waitKey()
cv2.putText(img, text, org, fontFace, fontScale, color, thinkness, lineType, bottomLeftOrigin)
text:文本
org:位置
fontFace:字体
fontScale:字体大小,该值和基础大小相乘得到字体大小
bottomLeftOrigin:默认0或False,1表示反转
关于字体可以使用下面这个方法查看对应的所有字体:
import cv2
flags = [i for i in dir(cv2) if i.startswith('FONT_')]
print(flags)
print(len(flags))
['FONT_HERSHEY_COMPLEX', 'FONT_HERSHEY_COMPLEX_SMALL', 'FONT_HERSHEY_DUPLEX', 'FONT_HERSHEY_PLAIN', 'FONT_HERSHEY_SCRIPT_COMPLEX', 'FONT_HERSHEY_SCRIPT_SIMPLEX', 'FONT_HERSHEY_SIMPLEX', 'FONT_HERSHEY_TRIPLEX', 'FONT_ITALIC']
9
import numpy as np
import cv2
img = np.zeros((500, 500, 3), dtype=np.uint8) # 设置窗口尺寸和数据类型
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'enen love majiali hahaha', (60, 200), font, 1, (0, 255, 0), 2, cv2.LINE_AA, 0)
cv2.putText(img,'2 0 2 0 0 2 0 2',(120,250),font, 1, (0, 0, 255))
cv2.putText(img, 'enen love majiali hahaha', (60, 300), font, 1, (255, 0, 0), 2, cv2.LINE_AA, 1)
cv2.imshow('txt',img)
cv2.waitKey(0)
需要创建一个鼠标的回调函数cv2.setMouseCallback(),当鼠标进行相关操作的时候就可以调用该函数就行相关画图操作
触发事件:
项目 | Value | 说明 |
---|---|---|
CV_EVENT_MOUSEMOVE | 0 | 滑动 |
CV_EVENT_LBUTTONDOWN | 1 | 左键点击 |
CV_EVENT_RBUTTONDOWN | 2 | 右键点击 |
CV_EVENT_MBUTTONDOWN | 3 | 中间点击 |
CV_EVENT_LBUTTONUP | 4 | 左键释放 |
CV_EVENT_RBUTTONUP | 5 | 右键释放 |
CV_EVENT_MBUTTONUP | 6 | 中间释放 |
CV_EVENT_LBUTTONDBLCLK | 7 | 左键双击 |
CV_EVENT_RBUTTONDBLCLK | 8 | 右键双击 |
CV_EVENT_MBUTTONDBLCLK | 9 | 中间释放 |
import numpy as np
import cv2
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
img = np.zeros((512,512,3),np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
if cv2.waitKey(-1) == ord('a'):
break
cv2.destroyAllWindows()