Python OpenCV Contour 编号详解:如何理解和使用
在 Python 中,使用 OpenCV 库进行图像处理时,'contour' 编号是指每个轮廓的唯一标识符。当你使用 cv2.findContours() 函数查找图像中的轮廓时,返回的结果是一个包含所有轮廓的列表。每个轮廓都有一个编号,从 0 开始递增。这个编号可以用来访问和操作特定的轮廓。
例如,如果你想画出 contour 列表中的第 3 个 contour,可以使用以下代码:
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 查找 contours
contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 画出第 3 个 contour
cv2.drawContours(img, contours, 2, (0, 255, 0), 2)
# 显示图像
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
在这个例子中,我们使用 cv2.drawContours() 函数来画出第 3 个 contour。注意,编号从 0 开始,所以第 3 个 contour 的编号是 2。
原文地址: https://www.cveoy.top/t/topic/mTrG 著作权归作者所有。请勿转载和采集!