【问题标题】:Scipy convolve2d is not accepting 2d arraysScipy convolve2d 不接受二维数组
【发布时间】:2022-09-29 10:46:54
【问题描述】:

我遇到了一个令人沮丧的问题,我试图将边缘过滤器应用于图像以进行课堂作业。当我运行代码时,我收到错误 \"ValueError Traceback (most recent call last)

在 12 sobel_horiz = sobel_vert.T 13 ---> 14 d_horiz = convolve2d(平均, sobel_horiz, 边界 = \'symm\', mode=\'same\', fillvalue=0) 15 d_vert = convolve2d(平均,sobel_vert,模式=\'same\',边界=\'symm\',填充值=0) 16 edgel=np.sqrt(np.square(d_horiz) + np.square(d_vert))

/usr/local/lib/python3.7/dist-packages/scipy/signal/signaltools.py in convolve2d(in1, in2, mode, boundary, fillvalue) 1694 1695 if not in1.ndim == in2.ndim == 2: -> 1696 raise ValueError(\'convolve2d inputs must both be 2-D arrays\') 1697 1698 if _inputs_swap_needed(mode, in1.shape, in2.shape):

ValueError: convolve2d 输入必须都是二维数组\"

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

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 边缘过滤器注释下。 谢谢!

    标签: python arrays numpy scipy


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 2016-08-13
      • 2015-09-17
      • 2017-03-07
      • 1970-01-01
      • 2017-06-16
      • 2012-01-07
      相关资源
      最近更新 更多