0
votes

I have an RGB image which I converted into index image using rgb2index. The resultant image is stored in two variable (as matlab requirement). But I want to have it in one variable for further processing. This is what I have tried. The resultant is black.

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));

But if do imshow((new_image,map)) it gives me the correct answer. I want it to be independent of variable map.

2

2 Answers

3
votes

To convert a indexed image to RGB use:

new_I = ind2rgb(in_I,map)
1
votes

Not the most elegant solution but this works.

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);

Edit: Modified answer to include rayryeng's improvement.