简体   繁体   English

在图像中嵌入字符

[英]embedding a character in an image

So here is what I was trying to do. 所以这就是我想做的。 I'm absolutely new to matlab. 我对Matlab绝对陌生。 It has only been a day or so that I've used it and here is a little something that my teacher had asked me to do. 我已经用了整整一天的时间,这是我的老师要求我做的一些事情。 Embed statements or group of strings within an image using the LSB Algorithm. 使用LSB算法将语句或字符串组嵌入图像中。 The string is to be read from a file. 该字符串将从文件中读取。 As of now, I've not used any file operations. 到目前为止,我还没有使用任何文件操作。 I'm trying this using one character and I don't know whats wrong. 我正在使用一个字符尝试此操作,但不知道出了什么问题。 The algo seems simple but my output ie, both the cover and the steg pixels show the same value. 该算法看起来很简单,但是我的输出(即覆盖和隐蔽像素)都显示相同的值。 :( :(

cover=imread('D:\l.jpg');
steg=cover;
l=1;
LSB=0;
height = size (cover, 1);
width = size (cover, 2);
message = 'J' ;
mdec = uint8(message);
mbin = dec2bin(mdec, 8);
mbins= mbin(:);
len=length(mbins);

for  i  = 1:height
for j = 1:width
        if(l<=len)
            LSB = mod(cover(i,j), 2);
            if(mbins(l)==LSB)
                steg(i,j) = cover(i,j);
            else if (mbins(l)~=LSB &&    LSB==1 && mbins(l)==0)
                steg(i,j) = cover(i,j)-1;
            else if (mbins(l)~=LSB &&    LSB==0 && mbins(l)==1)
                steg(i,j) = cover(i,j)+1;

                end
                end
                end
                    l=l+1;  
        end
end

end
imwrite(steg,'D:\hidden.jpg');
%imshow(steg)
cover(1, 1:8)
steg(1, 1:8)

Oh, nested loops... that's not the way to go. 哦,嵌套循环...这不是要走的路。

You want to replace the least significant bits of the first l pixels with the binary ascii representation of your input string. 您想用输入字符串的二进制ascii表示替换前l像素的最低有效位。


First thing that went wrong - converting char to binary: 第一件出错的事情-将char转换为二进制:
Converting a character to its binary representation should be done using bitget 将字符转换为二进制表示应使用bitget完成

>> bitget( uint8('J'), 1:8 )
0    1    0    1    0    0    1    0

Gives back 1-by-8 binary array , while using dec2bin : 使用dec2bin ,返回1×8二进制数组

>> dec2bin( uint8('J'), 8 ) 
01001010

Gives back 1-by-8 string : the actual numeric values of this array are 返回1×8 字符串 :此数组的实际数值为

>> uint8(dec2bin( uint8('J'), 8 ))
48   49   48   48   49   48   49   48

Can you appreciate the difference between the two methods? 您能欣赏两种方法之间的区别吗?

If you insist on using dec2bin , consider 如果您坚持使用dec2bin ,请考虑

>> dec2bin( uint8('J'), 8 ) - '0'
0     1     0     0     1     0     1     0

Second point - nested loops: 第二点-嵌套循环:
Matlab favors vector/matrix vectorized operations rather than loops. Matlab支持向量/矩阵向量化操作,而不是循环。

Here's a nice way of doing it without loops, assuming cover is a gray scale image (that is it has a single color channel rather than 3) of type uint8 . 这是一种无循环的好方法,假设coveruint8类型的灰度图像(也就是说,它具有单个颜色通道而不是3)。

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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM