在matlab中将两个变量图像存储在一个变量中

Storing two variables image in one variable in matlab

我有一个 RGB 图像,我使用 rgb2index 将其转换为索引图像。结果图像存储在两个变量中(作为 matlab 要求)。但我想把它放在一个变量中进行进一步处理。这是我试过的。结果是黑色的。

clc 
clear all
close all

%%

I = imread ('Daniel1_SRGB.png');

[in_I,map] = rgb2ind(I,3,'nodither');
imshow (in_I,map)

imwrite (in_I,map,'new_image.PNG','png')

new_I = imread ('new_image.png');

imshow((new_image));

但是如果做 imshow((new_image,map)) 它给了我正确的答案。我希望它独立于变量映射。

这不是最优雅的解决方案,但这个可行。

resR = reshape(map(in_I(:)+1,1), size(in_I));
resG = reshape(map(in_I(:)+1,2), size(in_I));
resB = reshape(map(in_I(:)+1,3), size(in_I));
res = cat(3, resR, resG, resB);

imshow(res);

编辑:修改答案以包括 rayryeng 的改进。

要将索引图像转换为 RGB,请使用:

new_I = ind2rgb(in_I,map)