ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
使用工具Python3.5, 使用库numpy;opencv 涉及的函数:cv2.line() , cv2.cicle() , cv2.rectangle() , cv2.ellipse() , cv2.putText()等 需要设置的参数: img 你想要绘制的图形的那副图像 color 形状的颜色,以RGB为例,需要传入的元组,例(255,0,0)代表蓝色,对于灰度图只需传入灰度值 thickness 线条的粗细,如果给一个闭合图形设置为-1,那么这个图形就会被填充,默认值为1 linetype 线条的类型,8连接,抗锯齿等。默认是8连接。cv2.LINE_AA为抗锯齿 1.画线 需要告诉函数这条线的起点和终点。 ~~~ import numpy as np import cv2 #Create a black image img = np.zeros((512,512,3),np.uint8) #draw a diagonal blue line with thickness of 5 px cv2.line(img,(0,0),(260,260),(255,0,0),5) #为了演示,建窗口显示出来 cv2.namedWindow('image',cv2.WINDOW_NORMAL) cv2.resizeWindow('image',1000,1000)#定义frame的大小 cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() ~~~ 2.画矩形 需要告诉函数左上角顶点和右下角顶点的坐标 ~~~ import numpy as np import cv2 #Create a black image img = np.zeros((512,512,3),np.uint8) cv2.rectangle(img,(350,0),(500,128),(0,255,0),3) cv2.namedWindow('image',cv2.WINDOW_NORMAL) cv2.resizeWindow('image',1000,1000)#定义frame的大小 cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() ~~~ 3.画圆 需要指定圆心坐标和半径大小,可以在上面矩形中画个圆 ~~~ import numpy as np import cv2 #Create a black image img = np.zeros((512,512,3),np.uint8) cv2.rectangle(img,(350,0),(500,128),(0,255,0),3)#矩形 cv2.circle(img,(425,63),63,(0,0,255),-1)#圆,-1为向内填充 cv2.namedWindow('image',cv2.WINDOW_NORMAL) cv2.resizeWindow('image',1000,1000)#定义frame的大小 cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() ~~~ 4.画椭圆 一个参数是中心点的位置坐标。 下一个参数是和短的度。椭圆沿时方向旋的度。椭圆弧演 时方向始的度和结束度如果是 0 很 360就是整个椭圆。查看 cv2.ellipse() 可以得到更多信息。 ~~~ import numpy as np import cv2 #Create a black image img = np.zeros((512,512,3),np.uint8) cv2.ellipse(img,(256,256),(100,50),0,0,360,255,-1) cv2.namedWindow('image',cv2.WINDOW_NORMAL) cv2.resizeWindow('image',1000,1000)#定义frame的大小 cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() ~~~ 5.画多边形 需要指定每个顶点的坐标,构建一个大小相等于行数X1X2的数组,行数就是点的数目,这个数组必须为int32。 ~~~ import numpy as np import cv2 #Create a black image img = np.zeros((512,512,3),np.uint8) pts=np.array([[10,5],[20,30],[70,20],[50,10]],np.int32) pts = pts.reshape((-1,1,2)) #这里reshape的第一个参数为-1,表明这一维度的长度是根据后面的维度计算出来的 cv2.polylines(img,[pts],True,(0,255,255)) #注意第三个参数若是False,我们得到的是不闭合的线 #为了演示,建窗口显示出来 cv2.namedWindow('image',cv2.WINDOW_NORMAL) cv2.resizeWindow('image',1000,1000)#定义frame的大小 cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() ~~~ cv2.polylines() 可以用来画很多条线。只把想画的线放在一 个列中将个列传给函数就可以了。每条线会独立绘制。会比用 cv2.line() 一条一条的绘制快一些。 6.在图片上添加文字 需要设置,文字内容,绘制的位置,字体类型、大小、颜色、粗细、类型等,这里推荐linetype=cv2.LINE_AA ~~~ import numpy as np import cv2 #Create a black image img = np.zeros((512,512,3),np.uint8) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA) #为了演示,建窗口显示出来 cv2.namedWindow('image',cv2.WINDOW_NORMAL) cv2.resizeWindow('image',1000,1000)#定义frame的大小 cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() ~~~ 7.显示结果可以用以下代码 ~~~ winname='example' cv2.namedWindow(winname) cv2.imshow(winname,img) cv2.waitKey(0) cv2.destroyAllWindow(winname) ~~~