您的位置:首页 > 编程语言 > MATLAB

利用LSB算法隐藏图片信息的MATLAB实现

2016-03-21 21:38 1351 查看
前一篇博客中介绍了利用LSB算法隐藏文字信息的MATLAB实现:

/article/9673899.html

在此基础上,下面介绍利用LSB算法隐藏图片信息的MATLAB实现。

补充说明

图片数据量较大,一个1920×10801920 \times 1080 的图片就有 2073600 个RGB值需要储存

依旧利用之前改进的算法,对 8-bitRGB 值增加一位以便于储存和作为结束标记(实验表明,对多个字符串进行 strcat 会影响速度,这是一个缺陷,也许有更高效的实现方法)

提取出来的图片矩阵为double型,一定要转化为 uint8 型保存才能正常显示出来

为了把图片的分辨率隐藏进去以便于恢复,先计算出该图片分辨率值转为二进制后的长度,把长度信息隐藏在图片的最后一个像素点内(只要图片分辨率最大值不超过 29992^{999} 就能存下),然后将分辨率值储存在最后几个像素点中,最后按照以前的方法从第一个像素点开始储存图片RGB信息

代码

GitHub:https://github.com/Momingcoder/Mathematical-Modeling/tree/master/LSB%20steganography/LSBforPictur

嵌入算法

function [] = LSB_embed(host, data)
% [] = LSB_embed(host, data)
% host: the host picture's path and name
% data: the data picture's path and name
% LSB in steganography (embed)
%
% Author: Moming
% 2016-03-21

lsb = 3;
host_image = imread(host);
data_image = imread(data);

len = length(dec2bin(max(size(data_image))));
len = dec2bin(len, 9);  % the length of max(height, width) < 2^999
image_info = dec2bin(size(data_image));
info = strjoin(cellstr(image_info)', '');

info_len = length(info) / lsb;

% is host picture big enough?
if numel(data_image) * 3 + info_len + 1 > numel(host_image)
warning('The host picture is too small to hide the data picture!');
return;
end

msg_bin = dec2bin(data_image, 8);  % convert to binary
msg = blanks(9);
for i = 1 : size(msg_bin, 1)
msg(i, :) = strcat(msg_bin(i, :), char(mod(i, 2) + '0'));
end
msg = strjoin(cellstr(msg)', '');
% change the last bit as the data end tag
msg(end) = char(mod(size(msg_bin, 1) + 1, 2) + '0');

tmp_len = blanks(3);
for i = 1 : 3
% convert to decimal (len)
tmp_len(i) = char(bin2dec(len((i - 1) * lsb + 1 : i * lsb)) + '0');
end

tmp_info = blanks(info_len);
for i = 1 : info_len
% convert to decimal (info)
tmp_info(i) = char(bin2dec(info((i - 1) * lsb + 1 : i * lsb)) + '0');
end

data_len = length(msg) / lsb;
tmp_data = blanks(data_len);
for i = 1 : data_len
% convert to decimal (data)
tmp_data(i) = char(bin2dec(msg((i - 1) * lsb + 1 : i * lsb)) + '0');
end

result = host_image;
rgb = 1;
[len_R, len_G, len_B] = size(result);

% hide len
for i = 1 : 3
result(len_R, len_G, i) = result(len_R, len_G, i) - ...
mod(result(len_R, len_G, i), 2^lsb) + double(tmp_len(i) - '0');
end

% hide info
for R = len_R : -1 : 1
for G = len_G : -1 : 1
if R == len_R && G == len_G
continue;
end
for B = len_B : -1 : 1
if rgb <= info_len
result(R, G, B) = result(R, G, B) - mod(result(R, G, B),...
2^lsb) + double(tmp_info(rgb) - '0');
rgb = rgb + 1;
end
end
end
end

% hide data
rgb = 1;
for R = 1 : len_R
for G = 1 : len_G
for B = 1 : len_B
if rgb <= data_len
% only to be consistent with front: '0'
result(R, G, B) = result(R, G, B) - mod(result(R, G, B),...
2^lsb) + double(tmp_data(rgb) - '0');
rgb = rgb + 1;
end
end
end
end

imshow(result);
imwrite(result, 'result.png');  % do not use jpg

end


提取算法

function [] = LSB_extract(name)
% LSB_extract(name)
% name: the picture's path and name
% LSB in steganography (extract)
%
% Author: Moming
% 2016-03-21

image = imread(name);

lsb = 3;
[len_R, len_G, len_B] = size(image);
flag = char('0');

tmp_len = blanks(3);
for i = 1 : 3
tmp_len = strcat(tmp_len, mod(image(len_R, len_G, i), 2^lsb) + '0');
end
len = sum(bin2dec(strjoin(cellstr(dec2bin(tmp_len - '0', 3))', '')));

% get the size of hide picture
index = 1;
tmp_info = blanks(len);
for R = len_R : -1 : 1
if index > len
break;
end
for G = len_G : -1 : 1
if index > len
break;
end
if R == len_R && G == len_G
continue;
end
for B = len_B : -1 : 1
if index > len
break;
end
tmp_info(index) = mod(image(R, G, B), 2^lsb) + '0';
index = index + 1;
end
end
end
cmd = strjoin(cellstr(dec2bin(tmp_info - '0'))', '');
image_size = zeros(1, 3);
for i = 1 : 3
image_size(i) = bin2dec(cmd((i - 1) * len + 1 : i * len));
end

% get the hide picture
index = 1;
picture = zeros(image_size);
for R = 1 : len_R
for G = 1 : len_G
tmp = blanks(0);
for B = 1 : len_B
% '0' is useful!!! Placeholder...
tmp = strcat(tmp, mod(image(R, G, B), 2^lsb) + '0');
end
tmp_bin = dec2bin(tmp - '0', 3)';
picture(index) = bin2dec(tmp_bin(1 : 8));
if flag + tmp_bin(9) ~= 97  % '0'/'1' is the end tag
% recover the picture
picture = uint8(picture);  % !!! very important !!!
imwrite(picture, 'recover.png');
imshow(picture);
return;
end
index = index + 1;
flag = tmp_bin(9);
end
end

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: