Opencv+Python cv2.imwrite()和简历imshow两个图像显示不同的输出

2024-06-28 11:29:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Opencv(3.0)和python2.7进行图像处理,但是cv2.imwrite()和{}有问题。它们产生不同的输出,我的代码如下:

tfinal = (255)*(nir_img-red_img)/(nir_img+red_img)
cv2.imwrite(_db_img1+'_NDVI'+_ext_img,tfinal)
cv2.imshow('NDVI',tfinal)

第一个图像是cv2.imshow()的输出

cv2_imshow_op

第二个图像是cv2.imwrite()的输出

Cv2_imwrite_op


Tags: 代码图像imgdbredcv2opencvext
2条回答

感谢梅尔尼科夫·谢尔盖简要介绍了我的解决方案。 我已经解决了这个问题使用以下语法修改在我的程序。 当我写我的图像乘以255,我得到了同样的结果。在

tfinal = (255)*(nir_img-red_img)/(nir_img+red_img)
cv2.imwrite(_db_img1+'_NDVI'+_ext_img,tfinal*255)
cv2.imshow('NDVI',tfinal)

这可能是因为您的数据类型。imwriteimshow根据数据类型自动决定如何处理数据。如imwriteimshow的文档所述:

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
    1. That is, the value range [0,1] is mapped to [0,255].

因此,您的底层数据类型似乎不是无符号字符,而是float或32位整数。在

此外,由于操作优先级,您可能会遇到以下问题:

(255)*(nir_img-red_img)/(nir_img+red_img)

你可能会溢出。最好将值设置在[0;1]的范围内,而不是将其相乘:

(255) * ( (nir_img-red_img)/(nir_img+red_img) )

相关问题 更多 >