初识OpenCV-Python - 002: Drawing functions

使用OpenCV-Python 的画图函数画图。

本次的图形函数有: cv2.line(), cv2.circle(), cv2.rectangle(), cv2.ellipse(), cv2.putText().

以上函数都包含以下参数:

img: 你需要画图形的图片

color: 图形的颜色, 对于BGR, 使用tuple, 如蓝色是(255,0,0)。对于灰度图,只需要传入数值。

thinkness: 线或者圆的厚度。默认为1。-1一般使用在封闭的图形中。

lineType: 线的类型。

Code:

import numpy as np
import cv2

# create a black image
img = np.zeros((512, 512, 3), np.uint8) #uint8表示无符号整数类型(0到255),一般会将图片转换成该格式的数组

# Draw a diagonal blue line with thickness of 5 px
img = cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)


# Draw a green rectangle with thickness of 3
img = cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)

# Draw a red circle
img = cv2.circle(img, (477, 63), 63, (0, 0, 255), -1)

# Draw a blue ellipse
img = cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 360, 360, -1)

# 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))

# Add white text to img
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'OpenCV', (10, 500), font, 4, (255, 255, 255), 2, cv2.LINE_AA)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Code运行结果展示:
初识OpenCV-Python - 002: Drawing functions_第1张图片

 

 

 

你可能感兴趣的:(初识OpenCV-Python - 002: Drawing functions)