简体   繁体   English

相当于python OpenCv的MATLAB imshow(I,[]);

[英]Equivalent of MATLAB imshow(I,[ ]) for python OpenCv;

I am converting an image processing code from MATLAB to Python using OpenCv(Python). 我正在使用OpenCv(Python)将图像处理代码从MATLAB转换为Python。 My original image (our friend Lena) shown below read using the following command shows fine using imshow: 下面显示的我的原始图像(我们的朋友Lena)使用以下命令读取,使用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. 但是,类似的MATLAB命令imshow(image,[])似乎可以正常工作。

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. 感谢您对Miki的评论,这有助于我解决了问题。 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. 问题是image_with_noise数据的类型。 When I do image_with_noise.dtype , it returns a float64. 当我执行image_with_noise.dtype ,它返回一个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). 由于浮动图像显示在[0,1]范围内,因此任何超过1的值都将显示为白色(正好是这种情况)。

The solution was to convert the image to uint8 where the display range is [0,255]. 解决方案是将图像转换为uint8,其中显示范围为[0,255]。 This can be done using the following one liner in cv2 可以在cv2中使用以下一根衬管来完成此操作

image_with_noise_uint8=cv2.convertScaleAbs(image_with_noise)

With this, the noisy image displays as expected! 这样,嘈杂的图像将按预期显示!

noisy_image

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM