【发布时间】:2019-02-06 20:18:49
【问题描述】:
我有一个 12 位相机,它以数组的形式拍摄图像(值是整数)。当我通过 matplotlib 将数组保存为 .png,然后将其读回时,这些值在 RGBA 中(如预期的那样)。通过读取 .png,我需要能够将其转换回原来的整数值。
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
# simulate some data
x = np.arange(0,100.1,1)
y1 = norm.pdf(x, loc=50, scale=20)
y2 = norm.pdf(x, loc=40, scale=10)
scaler = 1024/np.max(np.outer(y1,y2)) # img is 12 bits
img = np.outer(y1,y2)*scaler
img = img.astype(np.uint16) # force to be 16 bit as there is no 12 bit in np
print(np.max(img), np.min(img), img.shape)
plt.imshow(img)
plt.show()
plt.imsave(r"../img/sim.png", img, vmin=0, vmax=2**12, cmap='viridis')
img2 = plt.imread(r"../img/sim.png")
img2 # can we convert these RGBA values back to the original integers?
我不知道如何(有效地)将这些转换回原始整数。我相信这是可能的,因为我读过 .png 使用无损压缩。基本上我需要确定 img2 等于 img。
我觉得我在这里肯定缺少一些基本的东西......
【问题讨论】:
-
通过提供一个最小的工作示例,请更加简洁和全面:stackoverflow.com/help/mcve 也许您可以向我们展示当前和所需格式是什么样的?您的标题也令人困惑:您想转换为 RGBA 浮点数还是单色整数?
-
我对综合部分感到困惑,因为我说我需要确定 img2 等于 img。我知道标题应该说整数(尽管浮点数就足够了,因为我们可以通过反转 matplotlib 所做的规范化将 0 到 1 浮点数映射到整数)。
-
我不知道你的工作流程是什么样的,但我认为使用 numpy 并保存
img会更容易。然后你可以加载它,它会有正确的类型和数字 -
是的,有一个不同的应用程序基本上需要 .png 文件。试图通过多次保存来减少数据蔓延。认为反转颜色图很容易......
-
@CJStevens 并不是每个人都熟悉您使用的数据格式,因此数据格式的评论或摘录可能会有所帮助(并且更全面;-))。示例:
my_rgba = initialize_rgba_value() # format (uints with max 255): [255, 0, 100, 50]。我想我们中的许多人都知道 RGBA 值是什么,但我以前从未听说过颜色值的单整数表示(尽管想到了一个明显的实现,因为 4*8bits = 32 位 ...)
标签: python numpy matplotlib scipy