简体   繁体   English

Scipy convolve2d 不接受 2d arrays

[英]Scipy convolve2d is not accepting 2d arrays

I'm running into a frustrating issue where I'm attempting to apply an edge filter to an image for a class assignment.我遇到了一个令人沮丧的问题,我试图将边缘过滤器应用于图像以进行 class 分配。 When I run the code, I receive the error "ValueError Traceback (most recent call last)当我运行代码时,我收到错误“ValueError Traceback(最近一次调用最后一次)

in 12 sobel_horiz = sobel_vert.T 13 ---> 14 d_horiz = convolve2d(average, sobel_horiz, boundary = 'symm', mode='same', fillvalue=0) 15 d_vert = convolve2d(average, sobel_vert, mode='same', boundary = 'symm', fillvalue=0) 16 edgel=np.sqrt(np.square(d_horiz) + np.square(d_vert))在 12 sobel_horiz = sobel_vert.T 13 ---> 14 d_horiz = convolve2d(平均,sobel_horiz,边界 = 'symm',mode='same',fillvalue=0) 15 d_vert = convolve2d(平均,sobel_vert,mode='same ', 边界 = 'symm', fillvalue=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): /usr/local/lib/python3.7/dist-packages/scipy/signal/signaltools.py in convolve2d(in1, in2, mode, boundary, fillvalue) 1694 1695 如果不是 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 inputs must both be 2-D arrays" ValueError:convolve2d 输入必须都是二维数组”

I know that the arrays I'm passing to convolve2d are in fact 2d arrays, but convolve2d doesn't seem to register that, is there any way I can fix this?我知道我传递给 convolve2d 的 arrays 实际上是 2d arrays,但是 convolve2d 似乎没有注册,有什么办法可以解决这个问题吗? Here's the code:这是代码:

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) 

The relevant code is under the #apply edge filter comment.相关代码位于#apply 边缘过滤器注释下。 Thank you!谢谢!

I found where I was messing up, I needed to add a "0" as part of the parameters for this segment:我发现我搞砸了,我需要添加一个“0”作为该段参数的一部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM