3-Drawing+Functions+in+OpenCV

import numpy as np
import cv2 as cv

1、画直线

第一个参数是画布,即直线画在哪里;第二个参数为之前的起始位置;第三个参数为终止位置;第四个参数为直线的颜色;第五个参数为直线的粗细度。

# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv.line(img,(0,0),(511,511),(255,0,0),5)

2、画矩形

cv.rectangle(img,(384,0),(510,128),(0,255,0),3)

3、画圆形

第一个参数为画布;第二个参数为圆心坐标;第三个参数为半径;第四个参数为颜色;第五个参数为负数时表示是实心圆,为正数时表示线条粗细度。

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

4、画椭圆

img = cv.ellipse( img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]] )
img为画布;center为中心点坐标;axes为长短轴;angle为角度(逆时针);startAngle为起始角度;endAngle为终止角度;color为颜色;thincknessWie线条粗细度。

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

5、画多边形

需要多边形顶点坐标,reshape为ROWS*1*2,ROWS为顶点数量。
img = cv.polylines( img, pts, isClosed, color[, thickness[, lineType[, shift]]] )

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

6、向图像中添加文字

img = cv.putText( img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]] )
org参数:要放置的位置坐标(即数据开始的左下角)

font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv.LINE_AA)
cv.imshow('image', img)
cv.waitKey(0)
cv.destroyAllWindows()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Yeah_snow/article/details/79705302