0

I want to preprocess such an image dataset using an unsupervised wiener algorithm. But it doesn't work properly. when I run the code, it shows me a value attribute error. For convenience, my code is given below -

import cv2
import glob
from matplotlib import pyplot as plt
from skimage import io, restoration, img_as_float
import scipy.stats as st
import numpy as np


dataset = glob.glob('input/train/*.png')
directory = 'output/train/'

for img_id, img_path in enumerate(dataset):

    img = img_as_float(io.imread(img_path))

    def gkern(kernlen=21, nsig=2):    #Returns a 2D Gaussian kernel.
        lim = kernlen//2 + (kernlen % 2)/2
        x = np.linspace(-lim, lim, kernlen+1)
        kern1d = np.diff(st.norm.cdf(x))
        kern2d = np.outer(kern1d, kern1d)
        return kern2d/kern2d.sum()

    psf = gkern(5,3)   #Kernel length and sigma

    deconvolved, _ = restoration.unsupervised_wiener(img, psf)

    cl2 = cv2.resize(deconvolved, (512,512), interpolation = cv2.INTER_CUBIC)
    plt.imsave(f"output/unsupervised_{img_id}.png", cl2, cmap='gray')

I am getting the error :

File "C:\Users\Junaed\.spyder-py3\unsupervised_wiener.py", line 33, in <module>
deconvolved, _ = restoration.unsupervised_wiener(img, psf)
ValueError: could not broadcast input array from shape (5,5) into shape (5,5,4)

How could I fix this issue, Can someone help me here?

Jacob
  • 27
  • 1
  • 7
  • line_no please? – hacker315 May 08 '20 at 10:53
  • @hacker315, In this line i am getting an error `deconvolved, _ = restoration.unsupervised_wiener(img, psf)` – Jacob May 08 '20 at 11:01
  • Does this answer your question? [ValueError: could not broadcast input array from shape (20,590) into shape (20)](https://stackoverflow.com/questions/48001383/valueerror-could-not-broadcast-input-array-from-shape-20-590-into-shape-20) – Joe May 08 '20 at 11:56
  • https://stackoverflow.com/search?q=ValueError%3A+could+not+broadcast+input+array+from+shape – Joe May 08 '20 at 11:56
  • @joe, yes that my question. but I can't understand that solution. can you help me to fix this issue? – Jacob May 08 '20 at 12:15
  • @joe, `deconvolved, _ = restoration.unsupervised_wiener(img, psf)` in this line, it shows me an error . – Jacob May 08 '20 at 12:16
  • Please read https://numpy.org/devdocs/user/theory.broadcasting.html and https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html – Joe May 08 '20 at 12:16
  • These pages mention your error message and explain the reason – Joe May 08 '20 at 12:21
  • @joe, I checked this, but I don't understand, what kind of issue I could change. plz tell me the specific steps, to fix this error, I'll be very grateful to you. – Jacob May 08 '20 at 12:57
  • You could flip the two 5 axes or use :,:,np.newaxis on the (5, 5) array so it is broadcast-able. – Joe May 08 '20 at 13:32
  • @joe, if you fix this code and place the fix code as an answer, I think it's very helpful to me. – Jacob May 08 '20 at 14:01
  • It's even more helpful to you if you do that yourself :-) Look at what broadcasting and np.newaxis do. – Joe May 08 '20 at 14:08
  • Actually, I don't know how it works, I just copy from the scikit image website and run the variation of preprocessed images, that's why I don't understand where I used np.axis and where can I apply? – Jacob May 08 '20 at 14:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213434/discussion-between-jacob-and-joe). – Jacob May 08 '20 at 16:59

0 Answers0