LSB隐写matlab
时间: 2023-11-03 08:06:59 浏览: 114
LSB隐写是一种常见的信息隐藏技术,可以将秘密信息嵌入到图像或音频等多媒体文件中,而不影响原始文件的质量。在Matlab中实现LSB隐写可以使用imread和imwrite函数读取和写入图像文件,使用bitget和bitset函数获取和设置像素值的二进制位,从而实现信息的嵌入和提取。
以下是一个简单的LSB隐写Matlab代码示例:
```matlab
% 读取原始图像
img = imread('lena.png');
% 将秘密信息转换为二进制序列
msg = 'Hello, world!';
msg_bin = dec2bin(msg, 8) - '0'; % 转换为数字数组
% 在图像的最低有效位(LSB)中嵌入秘密信息
idx = 1;
for i = 1:size(img, 1)
for j = 1:size(img, 2)
if idx > size(msg_bin, 1)
break;
end
img(i, j, 1) = bitset(img(i, j, 1), 1, msg_bin(idx));
idx = idx + 1;
end
if idx > size(msg_bin, 1)
break;
end
end
% 保存含有秘密信息的图像
imwrite(img, 'lena_secret.png');
% 从含有秘密信息的图像中提取秘密信息
idx = 1;
msg_bin2 = zeros(size(msg_bin));
for i = 1:size(img, 1)
for j = 1:size(img, 2)
if idx > size(msg_bin, 1)
break;
end
msg_bin2(idx) = bitget(img(i, j, 1), 1);
idx = idx + 1;
end
if idx > size(msg_bin, 1)
break;
end
end
msg2 = char(bin2dec(char(msg_bin2 + '0' )))'; % 转换为字符数组
disp(msg2); % 输出提取的秘密信息
```
阅读全文
相关推荐

















