我正在尝试使用以下代码实现 Sobel 过滤器:
def Calculating_gradients(img):
Sobel_mag=[]
Sobel_theta=[]
for i in img:
dx = ndimage.sobel(i, 0) # horizontal derivative
dy = ndimage.sobel(i, 1) # vertical derivative
mag = math.sqrt(dx**2+dy**2) # magnitude
mag *= 255.0 / np.max(mag)
theta=math.atan(dy/dx) #slope
Sobel_mag.append(mag)
Sobel_theta.append(theta)
return Sobel_mag, Sobel_theta
mag,theta=Calculating_gradients(denoised_img)
我遇到了这个错误:
TypeError Traceback (most recent call last)
<ipython-input-23-f9db160f1948> in <module>
31 return Sobel_mag, Sobel_theta
32
---> 33 mag,theta=Calculating_gradients(denoised_img)
34
35 #def Canny_detection_algorithm(plates):
<ipython-input-23-f9db160f1948> in Calculating_gradients(img)
23 dx = ndimage.sobel(i, 0) # horizontal derivative
24 dy = ndimage.sobel(i, 1) # vertical derivative
---> 25 mag = math.sqrt(dx**2+dy**2) # magnitude
26 mag *= 255.0 / np.max(mag)
27 theta=math.atan(dy/dx) #slope
TypeError: only size-1 arrays can be converted to Python scalars
这个错误很可能是因为我 sqrt 采用单个值,但我使用了两个数组......我试图通过矢量化 dx 和 dy 然后使用 sqrt 来修复它,但这也不起作用......