Canny算子python实现
时间: 2023-12-25 14:22:42 浏览: 28
可以使用OpenCV库来实现Canny算子的Python代码,下面是一个示例:
```
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 灰度化处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Canny边缘检测
edges = cv2.Canny(gray, 100, 200)
# 显示处理后图像
cv2.imshow('Canny', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这段代码通过OpenCV库实现了Canny算子的Python代码,将指定图像转换为灰度图后,使用Canny函数进行边缘检测,并将结果显示在屏幕上。
相关问题
Canny算子python代码实现
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.
Canny算子python代码实现并保存
下面是Canny算子的Python实现代码:
```
import cv2
import numpy as np
# 读取图片
img = cv2.imread('input.jpg', 0)
# Canny算子边缘检测
edges = cv2.Canny(img, 100, 200)
# 保存图片
cv2.imwrite('output.jpg', edges)
```
其中,`input.jpg`为输入图片的路径,`output.jpg`为输出边缘检测结果的路径。