[英]cv2.imshow and cv2.imwrite
Can someone explain why the OpenCV imshow
and imwrite
function seem to result in a completely different image? 有人可以解释为什么OpenCV
imshow
和imwrite
函数似乎会产生完全不同的图像吗?
The first picture corresponds to imshow
and the second picture corresponds to imwrite
. 第一张图片对应于
imshow
,第二张图片对应于imwrite
。 Result
is an array of floating point values between 0 and 255. Result
是一个介于0和255之间的浮点值数组。
**result = result.astype(np.uint8)**
cv2.imshow('img', result)
cv2.imwrite('img.png', result)
I used the following (c++) code with OpenCV 2.4.8: 我在OpenCV 2.4.8中使用了以下(c ++)代码:
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);
and it results in the following images: 它会产生以下图像:
with imshow
. 与
imshow
。
with imwrite
. 与
imwrite
。
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. 这是由于两个函数的范围期望不同 ,
imwrite
总是期望[0,255],而imshow
期望浮点数为[0,1],无符号字符为[0,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]. 要使用
imshow
显示正确的输出,您需要将浮点图像的范围从[0,255]缩小到[0,1]。 You can do this using convertTo
and an appropriate scaling factor, or simply by dividing your image by 255. 您可以使用
convertTo
和适当的缩放系数来完成此操作,或者只需将图像除以255即可。
since you are using python this might help : 既然你使用的是python,这可能会有所帮助:
def showimg(img):
cv2.namedWindow("test", cv2.WINDOW_NORMAL)
img = np.array(img,dtype=float)/float(255)
cv2.imshow('test',img)
cv2.resizeWindow('test',600,600)
cv2.waitKey(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.