如何将分段图像存储为专题图像而不是连续图像

How to store a segmented image as thematic image as opposed to continuous image

我将图像分割图像数组的结果作为图像(预测值 class)。当我保存此图像时,我得到连续层类型。但我想要的是专题图层类型。 我应该使用什么函数或参数来将此图像保存为主题(.tif 格式) 例如。一张 3x3 的图片看起来像

[2,3,1

0,1,2

3,1,2]

而不是像

这样的像素值

[255,192,64

0,64,128

128,192,64]

我希望直方图从 0-3 读取。相反,直方图在第一个 3x3 示例图像的范围 (0-256) 内。 我正在使用 tifffile 以 .tif 格式写入

pred = numpy.argmax(ypreds, axis = 2)
tifffile.imwrite("pred1.tif", pred)

此代码似乎按照您的要求保存值:

import numpy as np
import tifffile

# Create representative image
y = np.random.randint(0,4,size=(3,3),dtype=np.uint8)

# Mine looks like this
# array([[1, 3, 3],
#        [0, 2, 3],
#        [1, 0, 0]], dtype=uint8)

# Save as TIFF
tifffile.imsave("pred1.tif",y)  

现在用 ImageMagick 检查内容,值和直方图看起来与数据匹配:

magick identify -verbose pred1.tif 

Image: pred1.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 3x3+0+0
  Resolution: 1x1
  Print size: 3x3
  Units: Undefined
  Colorspace: Gray
  Type: Grayscale
  Endianess: LSB
  Depth: 8-bit
  Channel depth:
    Gray: 8-bit
  Channel statistics:
    Pixels: 9
    Gray:
      min: 0  (0)                              <--- matches image
      max: 3 (0.0117647)                       <--- matches image
      mean: 1.44444 (0.00566449)
      standard deviation: 1.33333 (0.00522876)
      kurtosis: -1.91725
      skewness: 0.105324
      entropy: 0.945531
  Colors: 4
  Histogram:                                   <--- matches image
         3: (  0,  0,  0) #000000 gray(0)
         2: (  1,  1,  1) #010101 gray(1)
         1: (  2,  2,  2) #020202 gray(2)
         3: (  3,  3,  3) #030303 gray(3)