简体   繁体   English

将图像转换并索引为RGB并返回,而不会丢失数据

[英]Convert and indexed image to RGB and back without losing data

I am having trouble converting an indexed image to RGB and then back from RGB to an indexed image. 我在将索引图像转换为RGB,然后从RGB转换为索引图像时遇到麻烦。 For some reason, the result is different from the original. 由于某些原因,结果与原始结果有所不同。 I am doing steganography so it can't work if the data is changed. 我正在进行隐写术,因此如果更改数据,它将无法正常工作。

This is my code and this is the sample image: 这是我的代码,这是示例图像:

在此处输入图片说明

[J map]=imread('expert.gif');
Jrgb=ind2rgb(J,map);
Jind=rgb2ind(Jrgb,map);
isequal(J,Jind)

Variables J and Jind are supposed to be equal. 变量JJind应该相等。 Why are they being detected as being different? 为什么会检测到它们不同?

First of all, I'm certain that this is related to this question . 首先,我确定这与这个问题有关

The issue is happening because if you actually look at the colormap of your loaded image: 之所以发生此问题,是因为如果您实际查看已加载图像的颜色图:

map = 
       0         0         0
  0.6275    0.3216    0.1765
  0.4902    0.4902    0.4902
  0.8039    0.5216    0.2471
  0.7451    0.7451    0.7451
  0.8627    0.8627    0.8627
  0.9020    0.9020    0.9804
       0         0         0

You'll see that the color black (0,0,0) actually exists in there twice so both index = 0 and index = 7 will resolve to black in the RGB image. 您会看到黑色(0,0,0)实际上在那里存在两次,因此index = 0和index = 7都将在RGB图像中解析为黑色。

When you do the conversion back to an indexed image, MATLAB is going to use the same index for both of those (because they are obviously the same color) even if the colormap that you pass to rgb2ind is the same colormap. 当您转换回索引图像时,即使传递给rgb2ind色图是相同的色图,MATLAB也会为这两个色标使用相同的索引(因为它们显然是相同的颜色)。

That explains why the differences that you're seeing are where the transparent pixels are (around the periphery). 这就解释了为什么您看到的差异是透明像素所在的位置(周围)。

在此处输入图片说明

As far as dealing with this, I think it's a little tricky. 至于处理这个问题,我认为这有些棘手。 Unfortunately the transparency (3rd output) output of imread is an empty array. 不幸的是, imread的透明性(第三输出)输出是一个空数组。

You could potentially change the input colormap so that the first and last rows aren't the same (set the last row to 1's) and then you should get back something comparable. 你可能改变输入颜色映射,使得第一行和最后一行是不一样的(设定的最后一排1的),然后你应当得到的东西相媲美。

map(end,:) = 1;
rgb = ind2rgb(J, map);
ind = rgb2ind(rgb, map);
isequal(J, ind);

In general, due to MATLAB's limitations, GIFs with transparency may not be the best test case for playing with stenography. 通常,由于MATLAB的限制,透明的GIF可能不是玩速记的最佳测试用例。

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

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