imwrite的图像是黑屏,但是imshow可以正常显示

问题:

程序需要对图像进行处理,返回的值应当是图像的ndarray数值类型。按照项目要求,需要把图像存储在固定位置下,这里打算使用Imwrite。但是保存结果一直是完全黑屏图片。

效果如下,左边是imshow的结果,右边是imwrite的结果。

 

解决方法:

 在imwritre和imshow两个调用前,增加语句如下

out_img = (out_img * 255).astype(np.uint8)

 

原理: 

代码如下,按道理out_img一直没变,imshow和imwrite的结果应当是相同的。

    cv2.imwrite("out.png", out_img)
    cv2.namedWindow("out", 0);
    cv2.resizeWindow("out", 640, 480);
    cv2.imshow("out",out_img)

 查询后,用自己的话转述一下,图像数据处理后得到的数据,可能读取、处理形式对于两个函数是不一样的,正常情况下直接读取图片,显示图片、保存图片的图片数据均为整型

直接输出out_img的结果如下,显然不是整型

 文档中对于两个方法的描述如下,

imwrite:

The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see imread() for the list of extensions). Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function.

imshow:

The function may scale the image, depending on its depth:

  • If the image is 8-bit unsigned, it is displayed as is.
  • If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
  • If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].

可以看到此时的imshow会对图像的数据进行[0,255]的映射,所以imshow可以正常输出,而此时的imwrite不可以

 所以需要将数据转换为[0,255],再交给两个函数,他们才能进行正确操作,效果如下,问题解决。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这男人可真帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值