以下是一个基本的Python代码示例,用于查找图像的轮廓:

import cv2

# 读入图像
img = cv2.imread('image.jpg')

# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, 0)

# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# 绘制轮廓
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)

# 显示图像
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

在此示例中,我们首先读取图像,将其转换为灰度图像,然后进行二值化。接下来,我们使用cv2.findContours函数查找轮廓,并使用cv2.drawContours函数将其绘制在原始图像上。最后,我们显示图像并等待用户按下任意键关闭窗口。

请注意,cv2.findContours函数返回两个值:轮廓列表和层次结构。在此示例中,我们只使用了轮廓列表

查找轮廓python代码

原文地址: https://www.cveoy.top/t/topic/e8aQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录