要使用OpenCV实现Canny边缘检测算法,可以按照以下步骤进行操作:

  1. 导入OpenCV库:
import cv2
  1. 读取图像:
img = cv2.imread('image.jpg', 0)  # 以灰度模式读取图像
  1. 对图像进行高斯模糊:
blur = cv2.GaussianBlur(img, (5, 5), 0)
  1. 运用Canny边缘检测算法:
edges = cv2.Canny(blur, 50, 150)

其中,50和150是Canny算法中的阈值,可以根据需要进行调整。

  1. 显示结果:
cv2.imshow('Canny Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

完整代码示例:

import cv2

img = cv2.imread('image.jpg', 0)
blur = cv2.GaussianBlur(img, (5, 5), 0)
edges = cv2.Canny(blur, 50, 150)

cv2.imshow('Canny Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

注意:在运行代码之前,请确保已经安装了OpenCV库,并将图像文件放在正确的路径下

opencv实现canny边缘检测算法

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

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