cv2.imshow和cv2.imwrite [英] cv2.imshow and cv2.imwrite
问题描述
有人可以解释为什么OpenCV imshow
和imwrite
函数似乎导致完全不同的图像吗?
Can someone explain why the OpenCV imshow
and imwrite
function seem to result in a completely different image?
第一张图片对应于imshow
,第二张图片对应于imwrite
.
Result
是一个介于0到255之间的浮点值的数组.
The first picture corresponds to imshow
and the second picture corresponds to imwrite
.
Result
is an array of floating point values between 0 and 255.
**result = result.astype(np.uint8)**
cv2.imshow('img', result)
cv2.imwrite('img.png', result)
推荐答案
我在OpenCV 2.4.8中使用了以下(c ++)代码:
I used the following (c++) code with OpenCV 2.4.8:
cv::Mat_<float> img(300,300);
cv::theRNG().fill(img,cv::RNG::UNIFORM,0,255);
cv::imshow("Img",img);
cv::waitKey();
cv::imwrite("test.png",img);
并生成以下图像:
和imshow
.
和imwrite
.
这是由于两个函数的范围期望不同,imwrite
总是期望[0,255],而imshow
期望浮点为[0,1],而期望[0,255]未签名的字符.
This is due to the different range expectation of the two functions, imwrite
always expects [0,255], whereas imshow
expects [0,1] for floating point and [0,255] for unsigned chars.
为了用imshow
显示正确的输出,您需要将浮点图像的范围从[0,255]减小到[0,1].您可以使用convertTo
和适当的缩放比例,或简单地将图片除以255来完成此操作.
In order to display the correct output with imshow
, you need to reduce the range of your floating point image from [0,255] to [0,1]. You can do this using convertTo
and an appropriate scaling factor, or simply by dividing your image by 255.
这篇关于cv2.imshow和cv2.imwrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!