我遇到了一个令人沮丧的问题,我试图将一个边缘过滤器应用到一个图像的类分配。当我运行代码时,我收到错误“ValueError Traceback(most recent call last)
在12中,标准水平=标准垂直.T 13 ---〉14 d_水平=卷积2d(平均,标准水平,边界=“对称”,模式=“相同”,填充值=0)15 d_垂直=卷积2d(平均,标准垂直,模式=“相同”,边界=“对称”,填充值=0)16边缘=np.平方(np.平方(d_水平)+ np.平方(d_垂直))
/usr/local/lib/python3.7/dist-packages/scipy/signal/signaltools.py在convolve 2d(输入1,输入2,模式,边界,填充值)中1694 1695如果不在输入1中。ndim ==输入2。ndim == 2:- 〉1696 raise ValueError(“卷积2d输入必须都是二维数组”)1697 1698 if _inputs_swap_needed(mode,in1.shape,in2.shape):
ValueError:convolve 2d输入必须都是2-D数组”
我知道我传递给convolve 2d的数组实际上是二维数组,但是convolve 2d似乎没有注册这一点,有什么方法可以修复这个问题吗?代码如下:
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)
相关代码在#apply edge filter注解下,谢谢!
1条答案
按热度按时间dzjeubhm1#
我发现了我搞砸的地方,我需要添加一个
0
作为此段的参数的一部分: