Canny算子python代码实现
时间: 2023-09-23 18:12:27 浏览: 45
Sure! Here is a sample code for implementing the Canny edge detection algorithm in Python using OpenCV:
```
import cv2
# Read image
img = cv2.imread('input_image.jpg')
# Convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to reduce noise
blur = cv2.GaussianBlur(gray, (5,5), 0)
# Apply Canny edge detection
edges = cv2.Canny(blur, 50, 150)
# Display the result
cv2.imshow('Canny Edge Detection', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
In this code, we first read an input image and convert it to grayscale. Then, we apply a Gaussian blur filter to reduce noise in the image. Finally, we apply the Canny edge detection algorithm with a threshold range of 50 to 150 to detect edges in the image.