1
votes

I am converting an image processing code from MATLAB to Python using OpenCv(Python). My original image (our friend Lena) shown below read using the following command shows fine using imshow:

image = cv2.imread('Lena.bmp',0)

I add some Gaussian noise to the image

image_with_noise = image + 20*np.random.randn(256,256)

When I do imshow (as shown below), I don't see the image with noise as I expect.

cv2.imshow('image',image_with_noise)

cv2.waitKey(0)

cv2.destroyAllWindows()

However, the analogous MATLAB command imshow(image, [ ] ) seems to work fine.

Is there any way to understand or fix this? Your inputs are appreciated. Thank you.

Original image

enter image description here

1
what's the type of image_with_noise? You probably need to convert back to uint8 to display it properly - Miki
BTW, that's not the correct Lena image :D - Miki
The type is float64 when I do Image_with_noise.dtype. Thank you, trying your suggestion now. Haha, true, the prof seems to like this lena better than the old lena - felasfa
uint8 images are displayed in the range [0,255], while float images are displayed in the range [0,1]. So, what you saw is that every value above 1 was displayed as white. - Miki
This post may also be helpful: stackoverflow.com/questions/29100722/… - rayryeng

1 Answers

2
votes

Thank you for the comment by Miki which helped me resolve the issue. I am posting the answer here in case others run into a similar problem.

The issue was the type of the image_with_noise data. When I do image_with_noise.dtype, it returns a float64. Since float images are displayed in the range [0,1], any value exceeding 1 is shown as white(which is exactly what was happening).

The solution was to convert the image to uint8 where the display range is [0,255]. This can be done using the following one liner in cv2

image_with_noise_uint8=cv2.convertScaleAbs(image_with_noise)

With this, the noisy image displays as expected!

noisy_image