【发布时间】:2018-09-26 13:39:34
【问题描述】:
我有一个来自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
它也可以用其他格式重现,例如 png:
imwrite(uint16(randi([0 4000], 200)), colorcube(4001), 'p.png')
使用 writepng 时出错(第 76 行)
索引图像的位深度无效;必须是 1、2、4 或 8。
这是我们要保存的图像示例:
我们想要的是获得一个带有标签的每个分离区域的索引图像。我们如何保存这张图片?
由于我们知道这是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
【问题讨论】:
-
您可以使用哪些图像格式?是否需要 .tiff 或 .png?
-
你真的要写indexed图片吗?如果您首先将其转换为 RGB 图像,它似乎可以工作:
x = uint16(randi([0 4000], 200)); cmap = colorcube(4001); cmap1 = cmap(:,1); cmap2 = cmap(:,2); cmap3 = cmap(:,3); x_rgb = cat(3, cmap1(x+1), cmap2(x+1), cmap3(x+1)); imwrite(x_rgb, 'p.png') -
@gnovice 不,任何常规格式都不会丢失信息
-
@LuisMendo 谢谢,它确实可以正确导出,但是当您打开图像时,它是 RGB 文件,而不是索引图像。
标签: image matlab image-processing