I am doing steganography in indexed image. One of my problems is to convert the rgb image to an indexed image without decreasing the colors. I found out rgb2ind function in matlab converts rgb to indexed using minimum variance quantization and dithering. What I want is to make this indexed image colors exactly like rgb colors. How can i do that?
For the example, in this code i have this matrix M and convert it with rgb2ind function. I'm expecting the result variable Mrgb is equal with the matrix M.
% Matrix M
M(:,:,1) = [225 230 250 243 20 3 244 247 255 255
242 252 239 0 239 224 27 252 255 255
224 239 28 243 236 231 240 11 255 255
243 252 15 224 15 12 251 0 255 255
224 11 252 227 0 11 232 251 0 255
243 4 255 228 19 4 243 224 0 255
0 255 236 251 228 251 224 243 255 0
19 236 255 224 27 12 251 236 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,2) =[255 255 255 255 0 0 255 255 255 255
255 255 255 0 255 255 0 255 255 255
255 255 0 255 255 255 255 0 255 255
255 255 0 255 0 0 255 0 255 255
255 0 255 255 0 0 255 255 0 255
255 0 255 255 0 0 255 255 0 255
0 255 255 255 255 255 255 255 255 0
0 255 255 255 0 0 255 255 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,3) = [255 255 255 255 0 0 255 255 255 255
255 255 255 0 0 0 0 255 255 255
255 255 0 0 0 0 0 0 255 255
255 255 0 0 0 0 0 0 255 255
255 0 0 0 0 0 0 0 0 255
255 0 0 0 0 0 0 0 0 255
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
255 0 0 0 0 0 0 0 0 255];
[Mind map] = rgb2ind(M,255);
Mrgb = ind2rgb(Mind, map);
Mrgb=im2uint8(Mrgb);
isequal(M, Mrgb)
UPDATE
I update the matrix M in 4-D. I tried code from Dan's answer and show the image with imshow
, but the image become purple when it supposed to be yellow. How can I fix it?
M(:,:,1,1) = [ 225 230 250 243 20 3 244 247 255 255
242 252 239 0 239 224 27 252 255 255
224 239 28 243 236 231 240 11 255 255
243 252 15 224 15 12 251 0 255 255
224 11 252 227 0 11 232 251 0 255
243 4 255 228 19 4 243 224 0 255
0 255 236 251 228 251 224 243 255 0
19 236 255 224 27 12 251 236 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,2,1) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 255 255 0 255 255 255
255 255 0 255 255 255 255 0 255 255
255 255 0 255 0 0 255 0 255 255
255 0 255 255 0 0 255 255 0 255
255 0 255 255 0 0 255 255 0 255
0 255 255 255 255 255 255 255 255 0
0 255 255 255 0 0 255 255 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,3,1) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 0 0 0 255 255 255
255 255 0 0 0 0 0 0 255 255
255 255 0 0 0 0 0 0 255 255
255 0 0 0 0 0 0 0 0 255
255 0 0 0 0 0 0 0 0 255
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,1,2) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 255 255 0 255 255 255
255 255 0 255 255 255 255 0 255 255
255 255 0 255 255 255 255 0 255 255
255 0 255 255 255 255 255 255 0 255
255 0 255 255 255 255 255 255 0 255
0 255 255 255 255 255 255 255 255 0
0 255 255 255 255 255 255 255 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,2,2) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 255 255 0 255 255 255
255 255 0 255 255 255 255 0 255 255
255 255 0 255 255 255 255 0 255 255
255 0 255 255 255 255 255 255 0 255
255 0 255 255 255 255 255 255 0 255
0 255 255 255 255 255 255 255 255 0
0 255 255 255 255 255 255 255 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,3,2) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 0 0 0 255 255 255
255 255 0 0 0 0 0 0 255 255
255 255 0 0 0 0 0 0 255 255
255 0 0 0 0 0 0 0 0 255
255 0 0 0 0 0 0 0 0 255
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
255 0 0 0 0 0 0 0 0 255];
[map, ~, Mind] = unique(reshape(M,[],3), 'rows');
[row, col, ~, frame]=size(M);
Mind = reshape(Mind, [row, col, frame]);
%%convert to rgb
Mrgb=zeros(row, col, 3, frame);
z = size(map,1);
Mrgb = cat(3, map(Mind), map(Mind + z), map(Mind + 2*z));
Mrgb = reshape(Mrgb, [row, col,3, frame]);
map=uint8(map);
map=im2double(map);
imshow(Mind(:,:,1), map)
If you really need a colour resolution of 256^3
then you can do it manually fairly easily, however, you will have to convert your image back to RGB to view it so I'm not sure this actually has any utility:
[map, ~, Mind] = unique(reshape(M,[],3), 'rows');
Mind = reshape(Mind, size(M(:,:,1)));
You can reconstruct your colour image either using sub2ind
or like this:
z = size(map,1);
Mrgb = cat(3, map(Mind), map(Mind + z), map(Mind + 2*z))
I'm not sure if this has any point but maybe it's what you want.
isequal(M, Mrgb)
returns 1
.