哦,嵌套循环...这不是正确的方法。
您想要替换第一个的最低有效位l
具有输入字符串的二进制 ascii 表示形式的像素。
第一件事出错了 - 将 char 转换为二进制:
应使用以下命令将字符转换为其二进制表示形式bitget http://www.mathworks.com/help/matlab/ref/bitget.html
>> bitget( uint8('J'), 1:8 )
0 1 0 1 0 0 1 0
返回 1×8 二进制array,同时使用dec2bin http://www.mathworks.com/help/matlab/ref/dec2bin.html:
>> dec2bin( uint8('J'), 8 )
01001010
返回 1×8string:该数组的实际数值是
>> uint8(dec2bin( uint8('J'), 8 ))
48 49 48 48 49 48 49 48
您能理解这两种方法之间的区别吗?
如果你坚持使用dec2bin
, 考虑
>> dec2bin( uint8('J'), 8 ) - '0'
0 1 0 0 1 0 1 0
第二点-嵌套循环:
Matlab 更喜欢向量/矩阵向量化运算而不是循环。
这是一个没有循环的好方法,假设cover
是类型的灰度图像(即它具有单个颜色通道而不是 3 个)uint8
.
NoLsb = bitshift( bitshift( cover, -1 ), 1 ); %// use shift operation to set lsb to zero
lsb = cover - NoLsb; %// get the lsb values of ALL pixels
lsb( 1:l ) = mbins; %// set lsb of first l pixels to bits of message
steg = NoLsb + lsb; %// this is it!