[英]Matplotlib - how to rescale pixel intensities for RGB image
我对 matplotlib 如何处理 fp32 像素强度感到困惑。 据我了解,它会重新调整图像最大值和最小值之间的值。 但是,当我尝试通过使用 imshow() 将像素强度重新缩放到 [-1,1](通过 im*2-1)来查看最初在 [0,1] 中的图像时,图像的颜色会有所不同。 如何重新缩放以使图像不不同?
PS:我需要将此作为输出 [-1,1] 中这些值的程序的一部分
以下是用于此的代码:
img = np.float32(misc.face(gray=False))
fig,ax = plt.subplots(1,2)
img = img/255 # Convert to 0,1 range
print (np.max(img), np.min(img))
img0 = ax[0].imshow(img)
plt.colorbar(img0,ax=ax[0])
print (np.max(2*img-1), np.min(2*img-1))
img1 = ax[1].imshow(2*img-1) # Convert to -1,1 range
plt.colorbar(img1,ax=ax[1])
plt.show()
最大、最小输出为:
(1.0, 0.0)
(1.0, -1.0)
您可能在这里错误地使用了 matplotlib。
如果正常化步骤处于活动状态,它应该可以正常工作。 文档告诉我们,如果 input-image 是 float 类型,那只有在默认情况下才处于活动状态!
import numpy as np
import matplotlib.pyplot as plt
from scipy import misc
fig, ax = plt.subplots(2,2)
# This usage shows different colors because there is no normalization
# FIRST ROW
f = misc.face(gray=True)
print(f.dtype)
g = f*2 # just some operation to show the difference between usages
ax[0,0].imshow(f)
ax[0,1].imshow(g)
# This usage makes sure that the input-image is of type float
# -> automatic normalization is used!
# SECOND ROW
f = np.asarray(misc.face(gray=True), dtype=float) # TYPE!
print(f.dtype)
g = f*2 # just some operation to show the difference between usages
ax[1,0].imshow(f)
ax[1,1].imshow(g)
plt.show()
uint8
float64
第一行显示了错误的用法,因为输入是 int 类型,因此不会使用规范化。
第二行显示正确用法!
编辑:
sascha 在评论中正确指出重新缩放不适用于 RGB 图像,并且必须确保输入在 [0,1] 范围内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.