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.
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!
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.