OpenCV 绘制点线

2019-11-12  本文已影响0人  vcoolor

1. 点和圆

利用 opencv 里自带的 circle() 函数绘制以一个点为圆心特定半径的圆,画点实际上就是画半径很小的实心圆
其函数的声明如下:

cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
import numpy as np
import cv2
 
img = np.zeros((320, 320, 3), np.uint8) #生成一个空灰度图像
print img.shape # 输出:(480, 480, 3)

point_size = 1
point_color = (0, 0, 255) # BGR
thickness = 4 # 可以为 0 、4、8

# 要画的点的坐标
points_list = [(160, 160), (136, 160), (150, 200), (200, 180), (120, 150), (145, 180)]

for point in points_list:
    cv2.circle(img, point, point_size, point_color, thickness)

# 画圆,圆心为:(160, 160),半径为:60,颜色为:point_color,实心线
cv2.circle(img, (160, 160), 60, point_color, 0)

cv2.namedWindow("image")
cv2.imshow('image', img)
cv2.waitKey (10000) # 显示 10000 ms 即 10s 后消失
cv2.destroyAllWindows()

2. 直线

利用 OpenCV 自带的 line() 函数画直线
其函数声明如下:

cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
import numpy as np
import cv2
 
img = np.zeros((320, 320, 3), np.uint8) #生成一个空灰度图像
print img.shape # 输出:(320, 320, 3)

# 起点和终点的坐标
ptStart = (60, 60)
ptEnd = (260, 260)
point_color = (0, 255, 0) # BGR
thickness = 1 
lineType = 4
cv2.line(img, ptStart, ptEnd, point_color, thickness, lineType)

ptStart = (260, 60)
ptEnd = (60, 260)
point_color = (0, 0, 255) # BGR
thickness = 1
lineType = 8
cv2.line(img, ptStart, ptEnd, point_color, thickness, lineType)

cv2.namedWindow("image")
cv2.imshow('image', img)
cv2.waitKey (10000) # 显示 10000 ms 即 10s 后消失
cv2.destroyAllWindows()

thanks to Alan Wang's blog
Python 用 OpenCV 画点和圆 (2)
Python 用 OpenCV 画直线 (3)

上一篇下一篇

猜你喜欢

热点阅读