OpenCV学习笔记3:GUI特性 -- 绘图

坐标的0点为左上角。

线的宽度为-1时,为填充

1.直线

cv2.line()

arg1:图片,arg2:线的起点,arg3:线的终点,arg4:颜色,arg5:线的宽度(像素)

2.矩形

cv2.rectangle()

arg1:图片,arg2:顶点,arg3:顶点的对立点, arg4:颜色,arg5:线的宽度,arg6:线的类型,arg7:shift,作用待确认

3.画圆

cv2.circle()

arg1:图片,arg2:圆心坐标,arg3:半径,arg4:颜色,arg5:线的宽度

4.画椭圆

cv2.ellipse()

arg1:图片,arg2:中心坐标,arg3:轴的长度(长轴,短轴),arg4:旋转的角度,arg5:起点的角度,arg6:终点的角度, arg7:颜色,arg8:线的宽度

5.画多边形

cv2.polylines()

arg1:图片,arg2:多边形的顶点数组,arg3:各个点是否连接,arg4:颜色, arg4:线的宽度

6.添加文字

cv2.putText()

arg1:图片,arg2:文字,arg3:文字左下角位置坐标,arg4:字体, arg5:字的大小,arg6:颜色,arg7:粗体,arg8:线的类型

----- sample coding -----

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 5px

img = cv2.line(img, (0, 0), (512, 512), (255, 0, 0), 5, 1, 0)

# Draw rectangle

img = cv2.rectangle(img, (384, 0), (510, 129), (0, 255, 0), 3)

#draw cicle

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

#draw ellipse

img = cv2.ellipse(img, (256, 256), (100, 50), 10, 0, 360, 255, 2)

#draw polygon

pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32)

# pts = pts.reshape((-1, 1,2))

img = cv2.polylines(img, [pts], True, (0, 255, 255), 3)

# add text

font = cv2.FONT_HERSHEY_SIMPLEX

cv2.putText(img, 'OpenCV', (10, 500), font, 4, (255, 255, 255), 2, cv2.LINE_AA)

cv2.imshow('Drawing', img)

cv2.waitKey(0)

cv2.destroyAllWindows()

-----

结果:


 

-- End --

猜你喜欢

转载自stef.iteye.com/blog/2406152