写入具有256个以上标签的图像

时间:2018-09-26 13:39:35

标签: image matlab image-processing

我有一个来自bwlabel的二维标记矩阵,但是它可能具有索引大于4000的标签。很容易通过imshow显示256种以上的颜色:

img = zeros(1000, 1000);
%Put 4000 seeds
for numCentroid = 1:4000
    x = randi([1 size(img, 1)]);
    y = randi([1 size(img, 2)]);
    img(x, y) = 1;
end
D = bwdist(img);
% Create the 'cell' regions
L_img = watershed(D); % We obtain a labelled image
imshow(L_img, repmat(colorcube(256), 20, 1));

但是,当我们执行相同操作时,使用imwrite

imwrite(L_img, repmat(colorcube(256), 20, 1), 'p.tif');

并获取此错误:

  

使用wtifc时出错
  8位图像的无效颜色图,必须具有n X 3的尺寸(n <= 256)。

它也可以用其他格式重现,例如png:

 imwrite(uint16(randi([0 4000], 200)), colorcube(4001), 'p.png')
  

使用writepng时出错(第76行)
  索引图片的位深无效;必须为1、2、4或8。

这是我们要保存的图像的示例:

image to write

我们想要的是获得一个索引图像,其中每个分离的区域都标记有标签。我们如何保存这张图片?

由于我们知道这是imwrite本身的问题,因为它将双精度矩阵转换为uint8,因此我们尝试放置与uint16相同的矩阵。这也不起作用。

编辑1:

在Matlab中显示图像的位深是正确的16位。

imfinfo('p.tif')

但是,如果您检查相同的内容,但是在Windows:right click on the file -> Properties -> Details中,它具有8位深度。这与:

help imwrite
  

TIFF文件的BitDepth允许值
灰度图像:1、2、4、8或16
  具有Alpha通道的灰度图像:8或16
  索引图像:1、2、4或8
  真彩色图像:8或16

2 个答案:

答案 0 :(得分:3)

Adriaan pointed out in his answer一样,您可以写入16位TIFF文件。如果文件需要编制索引(即具有单独的颜色图)并具有16位索引,则TIFF是我所知道的唯一标准文件格式,可以完成这项工作。

给出16位索引图像和一个4001元素的颜色图:

data = uint16(randi([0 4000], 200));
cm = colorcube(4001);

然后:

imwrite(data,cm,'p.tif')

将其正确写入文件:

cris@paella> tiffinfo p.tif 
TIFF Directory at offset 0x13ba0 (80800)
  Image Width: 200 Image Length: 200
  Resolution: 72, 72 pixels/inch
  Bits/Sample: 16
  Compression Scheme: PackBits
  Photometric Interpretation: palette color (RGB from colormap)
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 1
  Rows/Strip: 40
  Planar Configuration: single image plane
  Color Map: (present)

({tiffinfo是LibTIFF附带的程序,通常默认情况下安装在Unix发行版上,我已经使用Homebrew在Mac上安装了LibTIFF)。

请注意“位/样本:16”行和“光度学解释:调色板颜色(来自颜色图的RGB)”。现在,此TIFF文件具有包含65536个条目的颜色图。

我们也可以读回文件:

[data2,cm2] = imread('p.tif');

isequal(data,data2)返回true,而class(data2)返回'uint16'cm2cm不同,因为它包含更多条目(全为0)。但是cm2(1:4001,:)cm几乎相同,区别在于cm是浮点值,它们乘以65535并四舍五入以保存到文件中。

正如我在Adriaan的答案下方的评论中所表达的那样,问题在于大多数软件将不会读取16位索引的TIFF文件。例如,Photoshop Elements将不会打开我们在此处创建的p.tif文件,Apple的Preview程序也不会打开。原因是基准TIFF(所有TIFF读取器必须遵守的最低通用分母标准)仅指定4位和8位索引图像。 16位索引图像是扩展。

因此,如果要保存16位索引图像,请首先确保目标软件将能够读取它。

答案 1 :(得分:2)

根据文档,如果文件格式支持以下内容,则输入uint16文件会写入uint16文件。

  

如果A的数据类型为uint16,并且输出文件格式支持16位数据(JPEG,PNG和TIFF),则imwrite将输出16位值。如果输出文件格式不支持16位数据,则​​imwrite返回错误。

documentation on imwrite

img = rand(1e3);
L_img = bwlabel(img);
imshow(L_img, repmat(colorcube(256), 20, 1));
Warning: Image is too big to fit on screen; displaying at 67% 
> In images.internal.initSize (line 71)
  In imshow (line 305) 

imwrite(L_img, repmat(colorcube(256), 20, 1), 'p.tif');
Error using wtifc
Invalid colormap for 8 bit image, must have dimensions n X 3 (n<=256).

Error in writetif (line 118)
wtifc(data, map, filename, writemode, colorspace, required_tags);

Error in imwrite (line 472)
        feval(fmt_s.write, data, map, filename, paramPairs{:});

tmp = uint16(L_img);
imwrite(tmp, repmat(colorcube(256), 20, 1), 'p.tif'); %Writes the file to my disk