python - 为什么我的 3D numpy 数组中的值在我将其写入文件时会发生变化?

标签 python arrays opencv numpy scipy

奇怪的问题是我有一个充满标签的 3D 数组(比如说 1-36),称为 labelled_stack。这些只是数组中的值等于给定标签的区域。一个带有 5 个标签的快速 2D 示例如下:

labelled_stack = (0 0 0 0 0 0 0 0 0 0)
                 (0 1 1 0 0 0 2 2 2 0)
                 (0 1 0 0 3 0 0 2 2 0)
                 (0 0 0 3 3 0 0 0 0 0)
                 (0 4 0 0 3 0 0 0 5 0)
                 (0 4 0 0 0 0 5 5 5 0)
                 (0 0 0 0 0 0 0 0 0 0)

但想象一下它是一个 numpy 数组......

我曾尝试使用 cv2.imwrite 和 scipy.misc.imsave 来保存堆栈,但是当我这样做然后打开它们时,它们的值已经改变,因此值 = 5,现在等于 255,并且值等于到 1,等于 51 等。这是 Not Acceptable 。我需要这些值保持原样。以 1 为步长增加整数值。我知道 cv2 和 scipy.misc 都写为 8 位图像,但这并不意味着我会收到此错误。

我什至将图像重新读回 python 以检查是否正在发生。

我的 labelled_stack 是 np.uint32 类型

编辑以包含保存命令:
for j in np.arange(0,l,1):
     misc.imsave(save_path+Folder+'/Labelled/slice_{:04d}of{:}.tif'.format(j+1,l),labelled_stack[:,:,j])

或者...

cv2.imwrite(save_path+Folder+'/Labelled/slice_{:04d}of{:}.tif'.format(j+1,l),labelled_stack[:,:,j])

最佳答案

imwrite 函数以 8 位格式保存数据。当该函数将您的 32 位数据转换为 8 位时,它会缩放数据以使用 8 位的完整位深度,因此例如您的数据的最大值被缩放到 255。参见 scipy imsave saves wrong values再举个例子。

为避免这种情况,请在保存数据之前将其转换为 numpy.uint8。在以下示例中,a 的数据类型为 numpy.uint32

In [98]: from scipy.misc import imsave, imread

In [99]: a
Out[99]: 
array([[0, 3, 2, 2, 4, 0, 3, 0],
       [2, 0, 0, 3, 3, 1, 3, 0],
       [2, 4, 4, 0, 2, 3, 1, 3],
       [0, 1, 3, 1, 0, 0, 0, 4],
       [2, 1, 1, 2, 1, 1, 3, 1],
       [0, 4, 0, 1, 0, 0, 2, 3],
       [3, 1, 3, 3, 3, 2, 3, 4],
       [0, 4, 1, 4, 2, 2, 0, 2]], dtype=uint32)

astype 方法用于将数组转换为 numpy.uint8,然后再将其提供给 imsave 函数。

In [100]: imsave("a.tif", a.astype(np.uint8))  # Convert to 8 bit before saving

scipy.misc.imread读回:

In [101]: b = imread("a.tif")

In [102]: b
Out[102]: 
array([[0, 3, 2, 2, 4, 0, 3, 0],
       [2, 0, 0, 3, 3, 1, 3, 0],
       [2, 4, 4, 0, 2, 3, 1, 3],
       [0, 1, 3, 1, 0, 0, 0, 4],
       [2, 1, 1, 2, 1, 1, 3, 1],
       [0, 4, 0, 1, 0, 0, 2, 3],
       [3, 1, 3, 3, 3, 2, 3, 4],
       [0, 4, 1, 4, 2, 2, 0, 2]], dtype=uint8)

请注意,当使用scipy.misc.imread 读取数据时,结果的数据类型为numpy.uint8


另一个推荐的选项是使用 tifffile,尤其是当您尝试保留需要 16 位或 32 位的数据时。 .

在此示例中,a 的数据类型为 numpy.uint32,其中一些值大于无符号 8 位数字所能容纳的值。

In [152]: from tifffile import imsave, imread

In [153]: a
Out[153]: 
array([[  0,   5,  10,  15,  20,  25,  30,  35,  40,  45],
       [ 50,  55,  60,  65,  70,  75,  80,  85,  90,  95],
       [100, 105, 110, 115, 120, 125, 130, 135, 140, 145],
       [150, 155, 160, 165, 170, 175, 180, 185, 190, 195],
       [200, 205, 210, 215, 220, 225, 230, 235, 240, 245],
       [250, 255, 260, 265, 270, 275, 280, 285, 290, 295],
       [300, 305, 310, 315, 320, 325, 330, 335, 340, 345],
       [350, 355, 360, 365, 370, 375, 380, 385, 390, 395],
       [400, 405, 410, 415, 420, 425, 430, 435, 440, 445],
       [450, 455, 460, 465, 470, 475, 480, 485, 490, 495]], dtype=uint32)

In [154]: imsave("a.tif", a)

In [155]: b = imread("a.tif")

In [156]: b
Out[156]: 
array([[  0,   5,  10,  15,  20,  25,  30,  35,  40,  45],
       [ 50,  55,  60,  65,  70,  75,  80,  85,  90,  95],
       [100, 105, 110, 115, 120, 125, 130, 135, 140, 145],
       [150, 155, 160, 165, 170, 175, 180, 185, 190, 195],
       [200, 205, 210, 215, 220, 225, 230, 235, 240, 245],
       [250, 255, 260, 265, 270, 275, 280, 285, 290, 295],
       [300, 305, 310, 315, 320, 325, 330, 335, 340, 345],
       [350, 355, 360, 365, 370, 375, 380, 385, 390, 395],
       [400, 405, 410, 415, 420, 425, 430, 435, 440, 445],
       [450, 455, 460, 465, 470, 475, 480, 485, 490, 495]], dtype=uint32)

关于python - 为什么我的 3D numpy 数组中的值在我将其写入文件时会发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37484254/

相关文章:

javascript - Javascript 对象数组中的 Canvas 图像类型

android - Android中带有HOG描述符的OpenCL

python - 从源代码 : ModuleNotFoundError: No module named 'cv2' 构建 opencv

c - 如何使用指针确定 C 中数组的大小?

javascript - 如何计算数组中低于/高于给定数字的元素数量(javascript)

c++ - OpenCV 中多维 cv::Mat 的多维 ROI

python - 我如何计算这张图片中的颗粒数

python - 无循环的3x3矩阵转换(RGB颜色转换)

python - OpenCV Python 和网络摄像头

python - 在不计算整个句子的情况下估计给定句子的标记概率/logits