利用LSB算法隐藏图片信息的MATLAB实现
2016-03-21 21:38
1351 查看
前一篇博客中介绍了利用LSB算法隐藏文字信息的MATLAB实现:
/article/9673899.html
在此基础上,下面介绍利用LSB算法隐藏图片信息的MATLAB实现。
依旧利用之前改进的算法,对 8-bitRGB 值增加一位以便于储存和作为结束标记(实验表明,对多个字符串进行 strcat 会影响速度,这是一个缺陷,也许有更高效的实现方法)
提取出来的图片矩阵为double型,一定要转化为 uint8 型保存才能正常显示出来
为了把图片的分辨率隐藏进去以便于恢复,先计算出该图片分辨率值转为二进制后的长度,把长度信息隐藏在图片的最后一个像素点内(只要图片分辨率最大值不超过 29992^{999} 就能存下),然后将分辨率值储存在最后几个像素点中,最后按照以前的方法从第一个像素点开始储存图片RGB信息
/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
相关文章推荐
- DICOM格式的图像在MATLAB中处理
- matlab:reshape
- DICOM图像格式
- Matlab的GUI参数传递方式
- Matlab投影仿真及三维曲面重构实现及演示程序
- Matlab中对矩阵使用矩阵尺寸的索引
- Matlab 实现对码功能
- Matlab 日期频次统计
- 《Computational Statistics with Matlab》硬译2
- Gamma函数相关matlab代码
- 【matlab】MATLAB程序调试方法和过程
- matlab简单应用
- matlab 文件夹操作
- MATLAB r2014a 下载+安装+激活
- MATLAB 编程风格指南
- C/C++下调用matlab函数
- matplotlib对ColorBar的处理似乎优于matlab
- 机器学习MatLab实战整理--线性回归
- matlab归一化和反归一化函数——mapminmax
- Matlab 图像处理基本操作