Mister Overlord
Mister Overlord

Reputation: 23

ValueError: convolve2d inputs must be both 2D arrays

I'm working on a program that takes in a video, splits the video into a sequence of images, applies a cleaning filter to the images (denoise/deblur/etc.), then puts it back together into a video.

I wanted to use Scikit Image's "Unsupervised_wiener" restoration on the images to de-blur them, but I haven't been able to get it and I don't understand the documentation.

This is what I have, copied from the documentation:

frame = color.rgb2gray
frame = convolve2d(frame, psf, 'same')
frame += 0.1 * frame.std() * np.random.standard_normal(frame.shape)
fixed = skimage.restoration.unsupervised_wiener(frame, psf)

Where "frame" is a frame from the input, which I captured with OpenCV2:

ret, frame = vid.read()

But I get the following error:

Traceback (most recent call last):
line 1025, in convolve2d
ValueError: convolve2d inputs must both be 2D arrays

How do I convert the input frames into a 2D array so that Scikit Image can process them?

Any help would be great, or if there's a better method I can use, I'm open to that as well. Thanks!

Edit: pst shape is: (5,5), frame shape is: (1080, 1920, 3)

Upvotes: 1

Views: 7786

Answers (1)

Ollin Boer Bohan
Ollin Boer Bohan

Reputation: 2401

Your input frames are RGB, but convolve2d and unsupervised_wiener expect 2D (grayscale) arrays.

You can resolve this by applying the operators to each channel individually.

Here is a fixed version of the original code that does per-channel operations:

# convolve each channel with the kernel
for i in range(frame.shape[-1]):
    frame[:,:,i] = convolve2d(frame[:,:,i], psf, mode="same")

# add gaussian noise
frame += 0.1 * frame.std() * np.random.standard_normal(frame.shape)

# wiener deconvolution
fixed = np.zeros(frame.shape)
for i in range(frame.shape[-1]):
    fixed[:,:,i], _ = restoration.unsupervised_wiener(frame[:,:,i], psf)

The result is stored in fixed, and the input and output are assumed to be numpy float arrays with values in the range [0,1].

Upvotes: 1

Related Questions