不要使用opencv库用python代码写一个canny边缘检测算法
时间: 2023-05-17 08:04:39 浏览: 112
好的,以下是使用Python编写的Canny边缘检测算法,不使用OpenCV库:
```python
import numpy as np
from scipy import ndimage
def gaussian_kernel(size, sigma=1):
x, y = np.mgrid[-size:size+1, -size:size+1]
g = np.exp(-(x**2+y**2)/(2*sigma**2))
return g/g.sum()
def sobel_filters(image):
kernel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
kernel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
G_x = ndimage.convolve(image, kernel_x)
G_y = ndimage.convolve(image, kernel_y)
G = np.sqrt(G_x**2 + G_y**2)
theta = np.arctan2(G_y, G_x)
return G, theta
def non_maximum_suppression(image, theta):
M, N = image.shape
Z = np.zeros((M,N), dtype=np.int32)
angle = theta * 180. / np.pi
angle[angle < 0] += 180
for i in range(1, M-1):
for j in range(1, N-1):
q = 255
r = 255
if (0 <= angle[i,j] < 22.5) or (157.5 <= angle[i,j] <= 180):
q = image[i, j+1]
r = image[i, j-1]
elif (22.5 <= angle[i,j] < 67.5):
q = image[i+1, j-1]
r = image[i-1, j+1]
elif (67.5 <= angle[i,j] < 112.5):
q = image[i+1, j]
r = image[i-1, j]
elif (112.5 <= angle[i,j] < 157.5):
q = image[i-1, j-1]
r = image[i+1, j+1]
if (image[i,j] >= q) and (image[i,j] >= r):
Z[i,j] = image[i,j]
else:
Z[i,j] = 0
return Z
def hysteresis_threshold(image, low_threshold, high_threshold):
M, N = image.shape
res = np.zeros((M,N), dtype=np.int32)
weak = np.int32(25)
strong = np.int32(255)
strong_i, strong_j = np.where(image >= high_threshold)
zeros_i, zeros_j = np.where(image < low_threshold)
weak_i, weak_j = np.where((image <= high_threshold) & (image >= low_threshold))
res[strong_i, strong_j] = strong
res[weak_i, weak_j] = weak
for i in range(1, M-1):
for j in range(1, N-1):
if (res[i,j] == weak):
if ((res[i+1, j-1] == strong) or (res[i+1, j] == strong) or (res[i+1, j+1] == strong)
or (res[i, j-1] == strong) or (res[i, j+1] == strong)
or (res[i-1, j-1] == strong) or (res[i-1, j] == strong) or (res[i-1, j+1] == strong)):
res[i,j] = strong
else:
res[i,j] = 0
return res
def canny_edge_detection(image, sigma=1, low_threshold=20, high_threshold=50):
image = image.astype(np.float32)
kernel = gaussian_kernel(5, sigma)
image_smoothed = ndimage.convolve(image, kernel)
G, theta = sobel_filters(image_smoothed)
G_suppressed = non_maximum_suppression(G, theta)
G_thresholded = hysteresis_threshold(G_suppressed, low_threshold, high_threshold)
return G_thresholded
```
请注意,这只是一个简单的实现,可能不如OpenCV的实现效果好。