gpt4 book ai didi

python - Matplotlib - 如何重新调整 RGB 图像的像素强度

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:04 26 4
gpt4 key购买 nike

我对 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。

标准化步骤如果处于事件状态,应该可以正常工作。 docs告诉我们,如果输入图像的类型为 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

enter image description here

分析

第一行显示错误的用法,因为输入是 int 类型,因此不会使用标准化。

第二行显示了正确的用法!

编辑:

sascha 在评论中正确指出,重新缩放不适用于 RGB 图像,并且输入必须确保在 [0,1] 范围内。

关于python - Matplotlib - 如何重新调整 RGB 图像的像素强度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39677581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com