# Getting distorted image when trying one of the sites tutorials

**URL:** <https://forum.opencv.org/t/getting-distorted-image-when-trying-one-of-the-sites-tutorials/17946>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [June 15, 2024, 1:38pm UTC](https://forum.opencv.org/t/getting-distorted-image-when-trying-one-of-the-sites-tutorials/17946 "2024-06-15T13:38:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Master](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/master/32/928_2.png) [@Master](https://forum.opencv.org/u/Master)\
**Post date:** [June 15, 2024, 1:38pm UTC](https://forum.opencv.org/t/getting-distorted-image-when-trying-one-of-the-sites-tutorials/17946/1 "2024-06-15T13:38:21Z")

</div>

Hello everyone, I hope you are doing great.  
I have recently been reading some of the tutorials and experimenting with them until I hit a wall when I got to : [OpenCV: Out-of-focus Deblur Filter](https://docs.opencv.org/4.x/de/d3c/tutorial_out_of_focus_deblur_filter.html)  
no matter how I tried I cant get the same output! could someone kindly point out where the issue is?

The problem should be fairly easy, create a `PSF`, shift and take its `dft` and calculate the Wiener deconvolution, and then apply it on the input image! However, I get a bad result, nowhere close to what is shown in the tutorial!

This is my code by the way:

```python
def calculate_psf(size, r):
    # create an array of zeros
    kernel = np.zeros((size,size),dtype=np.float32) if not hasattr(size,' __iter__') else np.zeros(shape=tuple(size), dtype=np.float32)
    cx,cy = kernel.shape[0]//2, kernel.shape[1]//2
    # draw a circle at the center
    cv2.circle(kernel, center=(cy,cx), radius=r, color=255, thickness=-1, lineType=cv2.LINE_8)
    # now normalize the kernel 
    kernel /= np.sum(kernel)
    return kernel

def calculate_weiner(psf, snr):
    psf = np.fft.fftshift(psf)
    # note the tutorial seems to use the psf real values only, 
    # while infact it should be the conjugate of psf! I however
    # am following the tutorial anyway here 
    psf_mag = psf.real/(np.abs(psf)**2 + 1/snr)
    # psf_mag = np.conj(psf)/(np.abs(psf)**2 + 1/snr)
    return psf_mag

def filter2DFreq(img, H):
    fft = np.fft.fftn(img.astype(np.float32))
    result = np.multiply(fft,H)
    ift = np.fft.ifftn(result)
    return ift

img = cv2.imread('./img/blurred_letter.jpg',0)
# this is a bitwise operation in C++ that rounds down 
# the number of rows and columns in the image to the nearest even number.
# its the same as doing 
# img_size = (img.shape[0]//2 *2, img.shape[1]//2 * 2)
img_size = tuple(d&-2 for d in img.shape[:2])
# use the reported values in the tutorial which are r=53 and snr=5200
psf = calculate_psf(size=img_size, r=53)
fft_mag = calculate_weiner(psf, snr=5200)
img_out = filter2DFreq(img, fft_mag)

cv2.imshow('img',img)
cv2.imshow('img_out.real', img_out.real.clip(0,255).astype(np.uint8))
cv2.imshow('img_out normalized', cv2.normalize(img_out.real, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U))
cv2.imshow('kernel-psf', cv2.normalize(psf,None, 0,255,cv2.NORM_MINMAX,dtype=cv2.CV_8U))

```

and this is what I get:

 ![Screenshot from 2024-06-15 18-02-20](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/c5d51237e21ef74da27e10769b1b271990d7d990.png)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 15, 2024, 2:37pm UTC](https://forum.opencv.org/t/getting-distorted-image-when-trying-one-of-the-sites-tutorials/17946/2 "2024-06-15T14:37:42Z")

</div>

source data please.

this might interest you:

> <https://stackoverflow.com/questions/78607642/how-to-remove-motion-blur-from-a-given-image-in-frequency-domain-deconvolution>

also the dude (and the sausage) is called Wiener, not Weiner, but I digress

---

<div class="post-metadata">

**Author:** ![Master](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/master/32/928_2.png) [@Master](https://forum.opencv.org/u/Master)\
**Post date:** [June 15, 2024, 2:44pm UTC](https://forum.opencv.org/t/getting-distorted-image-when-trying-one-of-the-sites-tutorials/17946/3 "2024-06-15T14:44:49Z")

</div>

Here is the image I used, the one used in the tutorial :  
 ![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/d31316ca01211049e5b33173127514e01d79feca.jpeg)  
I couldnt add it to the original post due to my account limitation

---

<div class="post-metadata">

**Author:** ![Master](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/master/32/928_2.png) [@Master](https://forum.opencv.org/u/Master)\
**Post date:** [June 16, 2024, 6:27am UTC](https://forum.opencv.org/t/getting-distorted-image-when-trying-one-of-the-sites-tutorials/17946/4 "2024-06-16T06:27:08Z")

</div>

OK , I found the issue, the image posted on the tutorial page(the 640x480 one) is not the original image. this is the original image (6,016x4,000)!:

 ![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/a36912bf5cf636d8d0bbb36b1683a73501a09c21.jpeg)  
The image size has a direct impact on the parameters. the 640x480 image on the tutorial page seems too small and has lost too much information, so I couldnt get muct out of it, the best result I could get was using r=6 and snr around 100 or 200! I guess whereas for the original image r=15 and snr=5200 works well.
