Scipy卷积2d不接受2d数组



我遇到了一个令人沮丧的问题,我试图将边缘过滤器应用于图像以进行类分配。当我运行代码时,我收到错误"0";ValueError Traceback(最近一次通话(

在12 sobel_horiz=sobel_vert.T13--->14 d_horiz=卷积2d(平均值,sobel_horiz,边界="symm",模式="me",填充值=0(15 d_vert=卷积2d(平均值,sobel_vert,模式="me",边界="symm",填充值=0(16edgel=np.sqrt(np.square(d_horiz(+np.squart(d_vert((

/卷积2d中的usr/local/lib/python3.7/dist-packages/scipy/signal/signaltools.py(in1,in2,模式,边界,填充值(16941695,如果不是in1.ndim==in2.ndim==2:->1696引发ValueError("已解析的2d输入必须都是2-D数组"(16971698 if _inputs_swap_needed(mode,in1.shape,in2.shape(:

ValueError:卷积2d输入必须都是2-D阵列";

我知道我传递给卷积二维的数组实际上是二维数组,但卷积二维似乎没有注册,有什么方法可以解决这个问题吗?这是代码:

import numpy as np
import cv2 
import math
import random
from matplotlib import pyplot as plt
from scipy.signal import convolve2d
#mount drive
from google.colab import drive
drive.mount('/content/drive')
#from google.colab.patches import cv2_imshow
def in_circle(x,y, center_x, center_y, radius):
distance = math.sqrt(math.pow(x-center_x,2)+math.pow(y-center_y,2))
return (distance < radius)
def in_disk(x,y,center_x,center_y,inner_radius,outer_radius):
return not in_circle(x,y,center_x,center_y,inner_radius) and in_circle(x,y,center_x,center_y,outer_radius)
img = cv2.imread('/content/mydata/circles.jpg')
# apply average filter
average_kernel = np.array(
[[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01]]   
)
average = cv2.filter2D(img,-1,average_kernel)
#cv2.imshow('first_average',average)
plt.figure()
plt.title('first AVR')
plt.imshow(average,cmap='gray', vmin=0, vmax=255)
# apply edge filter
l_kern2 = np.array([
[-1.0,  -1.0, -1.0]
,[-1.0, 8.0, -1.0]
,[-1.0,  -1.0, -1.0]
])
sobel_vert = np.array([
[-1.0, 0.0, 1.0]
,[-2.0, 0.0, 2.0]
,[-1.0, 0.0, 1.0]
])
sobel_horiz = sobel_vert.T
d_horiz = convolve2d(average, sobel_horiz,  boundary = 'symm', mode='same', fillvalue=0)
d_vert = convolve2d(average, sobel_vert, mode='same', boundary = 'symm', fillvalue=0)
edgel=np.sqrt(np.square(d_horiz) + np.square(d_vert))
#edgel = cv2.filter2D(average, -1, l_kern2) 
#edgel = convolve2d(average, l_kern2, mode='same', boundary = 'symm', fillvalue=0)
#edgel= np.absolute(edgel)
edgel *= 255.0 / np.max(edgel)
plt.figure()
plt.title('Edge')
plt.imshow(edgel,cmap='gray', vmin=0, vmax=255) 

相关代码位于#applyedgefilter注释下方。非常感谢。

我发现我在哪里搞砸了,我需要添加一个0作为该段参数的一部分:

img = cv2.imread('/content/mydata/circles.jpg',0)

最新更新