简体   繁体   English

将RGB图像转换为索引图像而无需在Matlab中减少颜色

[英]Convert rgb image to an indexed image without decreasing the colors in Matlab

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. 我的问题之一是在不降低颜色的情况下将rgb图像转换为索引图像。 I found out rgb2ind function in matlab converts rgb to indexed using minimum variance quantization and dithering. 我发现在matlab中的rgb2ind函数使用最小方差量化和抖动将rgb转换为索引。 What I want is to make this indexed image colors exactly like rgb colors. 我想要的是使此索引图像的颜色与rgb颜色完全相同。 How can i do that? 我怎样才能做到这一点?

For the example, in this code i have this matrix M and convert it with rgb2ind function. 例如,在此代码中,我拥有此矩阵M并使用rgb2ind函数对其进行转换。 I'm expecting the result variable Mrgb is equal with the matrix M. 我期望结果变量Mrgb与矩阵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 UPDATE

I update the matrix M in 4-D. 我在4-D中更新矩阵M。 I tried code from Dan's answer and show the image with imshow , but the image become purple when it supposed to be yellow. 我从Dan的答案中尝试了代码,并使用imshow显示了图像,但是当图像显示为黄色时,图像变为紫色。 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: 如果您确实需要256^3的颜色分辨率,则可以相当容易地手动进行操作,但是,您必须将图像转换回RGB才能查看它,因此我不确定它是否具有任何实用程序:

[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: 您可以使用sub2ind或类似方式重建彩色图像:

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 . 返回1

EDIT It seems that still, rgb2ind discretizes the colors to a lower number. 编辑似乎, rgb2ind颜色离散rgb2ind一个较小的数字。 The valuers are very close (max 4) but still not the same. 估值师非常接近(最多4位),但仍不相同。 Another approach (such as @Dan's) is needed. 需要另一种方法(例如@Dan的方法)。


The second argument of rgb2ind is the amount of colors, with a possible maximum of 65,536. rgb2ind的第二个参数是颜色的数量,最大可能为65,536。 Just change your code to 只需将代码更改为

[Mind map] = rgb2ind(uint8(M),65536,'nodither');

You possibly wont have that many colors. 您可能不会有那么多颜色。

NOTE Your M is a double in your code, thus rgb2ind doesn't really understand colors above the value 1 . 注意您的M是代码中的double,因此rgb2ind不能真正理解值1以上的颜色。 Cast it to uint8 and you'll get a map. 将其投放到uint8 ,您会得到一张地图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM