opencv之画图(Drawing Functions in OpenCV )

来源:互联网 发布:九阴绝学金身升级数据 编辑:程序博客网 时间:2024/05/21 18:47

目标:
- 学习用opencv画不同的几何图形
- 将能学习到直线line(),圆circle(),长方形rectangle(),椭圆ellipse(),文字输出putText()等函数。
代码:
1. img :the image where you want to draw the shapes
2. color:color of the shape
3. thickness:thickness of the line or circle
4. lineType:type of line

画线:
需要给出起始点和中止点,最开始给出一个黑色背景,画一条蓝色线,从左上到右下。

import numpy as npimport cv2# Create a black imageimg = np.zeros((512,512,3), np.uint8)# Draw a diagonal blue line with thickness of 5 pxcv2.line(img,(0,0),(511,511),(255,0,0),5)#第一个参数是线画在哪里,第二个是起始点坐标,第三个是中止点坐标,第四个是颜色,第五个是宽度。

画长方形:

cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)#给出左上角和右下角,画一个绿色的长方形,宽度为3

画圆形:
需要给出圆心坐标和半径长度:

cv2.circle(img,(447,63), 63, (0,0,255), -1)

画椭圆:需要给出圆心位置,给出长短轴长度,还要给出偏转角度,下面代码只画了一半椭圆。

cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

画多边形:

pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)pts = pts.reshape((-1,1,2))cv2.polylines(img,[pts],True,(0,255,255))

全代码:

import numpy as npimport cv2# Create a black imageimg = np.zeros((512,512,3), np.uint8)   #创建一个背景# Draw a diagonal blue line with thickness of 5 pxcv2.line(img,(0,0),(511,511),(255,0,0),5)   #在img里画图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,180,255,-1)pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)pts = pts.reshape((-1,1,2))cv2.polylines(img,[pts],True,(122,122,255))font = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(img,'OpenCV',(10,500), font, 4,(122,122,122),2,cv2.LINE_AA)cv2.imshow('kktest',img)

效果图:
这里写图片描述

原创粉丝点击