【OpenCV】⚠️高手勿入! 半小时学会基本操作 19⚠️
概述
OpenCV 是一个跨平台的计算机视觉库, 支持多语言, 功能强大. 今天小白就带大家一起携手走进 OpenCV 的世界. (第 19 课)
图像轮廓
cv2.findContours
可以帮助我们查找轮廓.
格式:
cv2.findContours(image, mode, method, contours=None, hierarchy=None, offset=None)
参数:
- image: 需要查找轮廓的图片
- mode: 模式
- RETR_EXTERNAL: 只检测最外面的轮廓
- RETR_LIST: 检测所有的轮廓, 并将其保存到一条链表中
- RETR_CCOMP: 检索所有的轮廓, 将他们组织为两层: 顶部是各分部法外部边界, 第二层是空洞的边界
- RRTR_TREE: 检索所有的轮廓, 并重构嵌套轮廓的整个层次
- method: 轮廓逼近的方法
- CHAIN_APPROX_NONE: 以 Freeman 链码的方式输出轮廓, 所有其他方法输出多边形 (定点的序列)
- CHAIN_APPROX_SIMPLE: 压缩水平的, 垂直的和斜的部分, 只保留他们的终点部分
返回值:
- contours:轮廓本身
- hierarchy: 轮廓的对应编号
原图:
绘制轮廓
cv2.drawContours
可以实现轮廓绘制.
格式:
cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None):
参数:
- image: 需要绘制轮廓的图片
- contours: 轮廓
- color: 颜色
- thickness: 轮廓粗细
绘制所有轮廓:
img = cv2.imread("contours.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2)
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出结果:
绘制单个轮廓:
img = cv2.imread("contours.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, 0, (0, 0, 255), 2)
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出结果:
轮廓特征
cnt = contours[0] # 取第一个轮廓
area = cv2.contourArea(cnt)
print("轮廓面积:", area)
perimeter = cv2.arcLength(cnt, True)
print("轮廓周长:", perimeter)
输出结果:
轮廓面积: 8500.5
轮廓周长: 437.9482651948929
轮廓近似
原图:
代码:
img = cv2.imread("contours2.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hieratchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, 0, (0, 0, 255), 2)
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()
cnt = contours[0]
epsilon = 0.1 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
draw_img = img.copy()
res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 2)
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出结果:
直接绘制轮廓:
轮廓近似:
边界矩形
cv2.boundingRect
可以帮助我们得到边界矩形的位置和长宽.
例子:
img = cv2.imread("contours.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
x, y, w, h = cv2.boundingRect(cnt)
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
area = cv2.contourArea(cnt)
rect_area = w * h
extent = area / rect_area
print('轮廓面积与边界矩形比:', extent)
输出结果:
轮廓面积与边界矩形比: 0.5154317244724715
外接圆
cv2.minEnclosingCircle
可以帮助我们得到外接圆的位置和半径.
例子:
img = cv2.imread("contours.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
(x, y), radius = cv2.minEnclosingCircle(cnt)
img = cv2.circle(img, (int(x), int(y)), int(radius), (255, 100, 0), 2)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出结果: