我想在 matlab 中读取图像并将其转换为索引图像
这是我的代码:
[I map] = imread('image.tif');
I = rgb2ind(I, map);
figure(1);
imagesc(I);axis('equal');
当我刚刚阅读图像时,它看起来很好(但它是一个 rgb 图像)。然后我将其转换为索引图像,我有以下图片:
这段代码有什么问题?
您的语法略有偏差。这应该有效:
[I, map] = imread('autumn.tif');
[I, map] = rgb2ind(I, map);
figure(1);
image(I);
colormap(map);
axis('equal');
请参阅rgb2ind的文档。
您的输出是滥用 matlab 函数的结果。
%read a non-indexed image. I is your RGB image, map is empty
[I,map] = imread('board.tif');
%rgb2ind has two output arguments, get both, otherwise your unchanged code
[I2,map2] = rgb2ind(I, map);
%Now I2 is a indexed image and map2 the corresponding map
现在您在不应用颜色图的情况下显示索引图像 I2:
imagesc(I2)
您的图像包含值 1:n 并且颜色图jet
已激活,因此您会得到彩虹。
使用地图显示正确图像的可能性:
imagesc(I2)
colormap(map2)
或者显示 I,即原始 RGB 图像
imagesc(I)