LSBアルゴリズムで画像情報を隠すMATLABを使って実現した.
12261 ワード
前のブログでは、LSBアルゴリズムで文字情報を隠しているMATLABを紹介しました.http://blog.csdn.net/csdn_moming/articale/details/50936687をベースに、LSBアルゴリズムで画像情報を隠すMATLABを紹介します.
補足説明ピクチャのデータ量が大きく、1920.×1080 1920× 1080の写真は2073600のRGB値があります. を保存する必要があります.は依然として前の改善されたアルゴリズムを利用して、8−bit RGB値に対して、1ビットを追加して、保存と終了の標識として使用することができます(実験で示されているように、複数の文字列に対してstracatを行うと、速度に影響があります.これは欠陥です.より効率的な実現方法があるかもしれません.) で抽出されたピクチャー・マトリクスはdouble型であり、必ずuint 8型の保存に変換してこそ、正常に表示されます. は、復元のためにピクチャの解像度を隠すために、まず、このピクチャの解像度値をバイナリにした後の長さを計算して、画像の最後のピクセルポイントに長さ情報を隠します.最後に、以前の方法に従って、最初のピクセルポイントから画像RGB情報 の格納を開始する.
コード
GitHub:https://github.com/kemingy/Cryptography/tree/master/LSB%20steganography/LSBforPicture
埋め込みアルゴリズム
補足説明
コード
GitHub:https://github.com/kemingy/Cryptography/tree/master/LSB%20steganography/LSBforPicture
埋め込みアルゴリズム
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