简体   繁体   中英

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. When I run the code, I receive the error "ValueError Traceback (most recent call last)

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))

/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 inputs must both be 2-D arrays"

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? 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. Thank you!

I found where I was messing up, I needed to add a "0" as part of the parameters for this segment:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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