用python写一个给轮廓标记函数
以下是使用OpenCV库实现轮廓标记的Python函数示例:
import cv2
def mark_contours(image_path):
# 读取图像
image = cv2.imread(image_path)
# 转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 进行二值化处理
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 标记轮廓
marked_image = cv2.drawContours(image, contours, -1, (0, 0, 255), 2)
# 显示标记后的图像
cv2.imshow('Marked Image', marked_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
此函数接受一个图像路径作为参数,读取图像后将其转换为灰度图像并进行二值化处理,然后使用OpenCV库的findContours函数查找图像中的轮廓。最后,函数使用drawContours函数标记轮廓,并显示标记后的图像。
原文地址: https://www.cveoy.top/t/topic/bqaE 著作权归作者所有。请勿转载和采集!