一、实验目的

了解调色板图像的特点,掌握基于调色板图像的信息隐藏原理,设计并实现一种基于调色板图像的信息隐藏算法。

二、实验环境

  • Windows XP或Vista操作系统;
  • Matlab 7.1版本软件;
  • 调色板图像。

三、原理简介

调色板图像含有一个不超过256种颜色的调色板,并对应每种颜色的R、G、B三种分量的值,图像内容中的每个像素存储的是一个不超过8比特的索引值,其指向调色板中的对应颜色就是图像的真实色彩。大部分图像用到的颜色数小于256种,甚至小于128种,因此可以在调色板中隐藏秘密信息。
调色板图像的调色板中最多包含256种颜色,若改变这些颜色的排列顺序并相应地改变图像像素数据,不会对图像内容造成任何影响。因此隐藏秘密信息最简单的方法就是重新排列调色板中的这些颜色,同时修改索引值,但是许多软件生成的调色板都是按照亮度和使用频率排列的,如果在调色板中打乱颜色会使其毫无隐藏可言。如果调色板颜色小于128种,可复制一个调色板,得到一个扩展的周色板,隐藏0使用原始索引值,隐藏1使用扩展索引值,但是重复颜色的调色板会引起使用者怀疑。
本节中使用另外一种方法来隐藏信息,对于调色板中的每-种颜色,可以通过修改颜色的蓝色分量(人眼对于绿色分量最为敏感,对蓝色分量最不敏感)来增加一种接近的但不完全相同的颜色,这样就形成一个扩展的调色板。嵌人算法如下:对调色板中所有颜色,把像素点的蓝色分量增加,然后将这种颜色添加到调色板中形成一个扩展调色板。根据需要嵌人的消息长度,随机选取隐藏的位置,当嵌人0时,使用原调色板中的颜色,当嵌人1时,使用扩展后调色板中的颜色。
提取时,需要知道原始调色板的长度、随机选择的种子数和隐藏的信息数量,对于选中的像素点,如果使用原调色板中的颜色,提取信息0,如果使用扩展调色板中的颜色,提取信息1。

四、实验步骤

待加密信息为 you find me!

1、嵌入秘密信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
clc;
clear;

[x,map] = imread('lenaindex.bmp','bmp');
wx = x;
[row col] = size(wx);
wmap = map;

msgfid = fopen('hidden.txt','r');
[msg,count] = fread(msgfid);
fclose(msgfid);
count = count * 8;
msg = str2bit(msg);
msg = msg';

%构造扩展map
oplength = 0;
for i = 1:256
if map(i,1) ~= 0&&map(i,2) ~= 0&&map(i,3) ~= 0
oplength = oplength + 1;
end
end
for i = oplength + 1:oplength * 2
wmap(i,1) = map(i - oplength, 1);
wmap(i,2) = map(i - oplength, 2);
%蓝色分量加0.0001
wmap(i,3) = map(i - oplength, 3) + 0.0001;
end

%确定隐藏位置
key = 1234;
[row col] = randselect(x,count,key);

for i = 1:count
if str2num(msg(i,1)) == 1
wx(row(i),col(i)) = x(row(i),col(i)) + oplength;
end
end
imwrite(wx,map,'lenaindex1.bmp','bmp');
figure;
subplot(1,2,1);imshow('lenaindex.bmp');title('原始图像');
subplot(1,2,2);imshow('lenaindex1.bmp');title('携密图像');

2、提取秘密信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
clc;
clear;

%读入载体图像
[x,map] = imread('lenaindex1.bmp','bmp');
key = 1234;
count = 96;
for i = 1:count
msg(i, 1) = 0;
end

% 找到扩展起始颜色位置
oplength = 64;

[row col] = randselect(x,count,key);
for i = 1:count
if x(row(i),col(i))>oplength
msg(i,1) = 1;
else
msg(i,1) = 0;
end
end
out = bit2str(msg);
fid = fopen('message.txt','wt');
fwrite(fid,out);
fclose(fid);

3、随机位置函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [row col] = randselect(x,count,key)
[m,n] = size(x);
distance1 = ceil(m*n/count);
distance2 = distance1 - 2;
if distance2 == 2
error('载体太小');
end
rand('state',key);
a = rand(1,count);
row = zeros([1 count]);
col = zeros([1 count]);
r = 1;
c = 1;
row(1,1) = r;
col(1,1) = c;
for i=2:count
if a(i)>=0.5
c=c+distance1;
else
c=c+distance2;
end
if c>n
r=r+1;
if r>m
error('载体太小');
end
c = mod(c,n);
if c==0
c=1;
end
end
row(1,i)=r;
col(1,i)=c;
end

4、bit2str

1
2
3
4
5
6
7
function [msg] = bit2str(bin)
[row col] = size(bin);
bin = reshape(bin,[8 row/8]);
bin = bin';
bin = num2str(bin);
msg = bin2dec(bin);
end

5、str2bit

1
2
3
4
5
function [bin] = str2bit(msg)
bin = dec2base(msg,2,8);
[row col] = size(bin);
bin = reshape(bin.',[1 row*col]);
end