在Python中填充Canny检测到的边缘可以通过以下步骤实现:
import cv2
import numpy as np
image = cv2.imread('image.jpg', 0) # 读取图像,灰度模式
edges = cv2.Canny(image, threshold1, threshold2) # 边缘检测
其中,threshold1
和threshold2
是Canny算法的两个阈值参数,可以根据具体情况进行调整。
filled_image = np.zeros_like(image)
cv2.drawContours()
填充边缘:contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 寻找边缘轮廓
cv2.drawContours(filled_image, contours, -1, (255, 255, 255), thickness=cv2.FILLED) # 填充轮廓
其中,cv2.RETR_EXTERNAL
表示只检测最外层轮廓,cv2.CHAIN_APPROX_SIMPLE
表示使用简化的轮廓表示方法。
cv2.imshow('Filled Image', filled_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
这样就可以在Python中填充Canny检测到的边缘。请注意,以上代码中使用的是OpenCV库进行图像处理,如果尚未安装,请先使用pip install opencv-python
命令进行安装。
推荐的腾讯云相关产品:腾讯云图像处理(https://cloud.tencent.com/product/tci)、腾讯云视觉智能(https://cloud.tencent.com/product/visionai)。
领取专属 10元无门槛券
手把手带您无忧上云