簡體   English   中英

在圖像中嵌入字符

[英]embedding a character in an image

所以這就是我想做的。 我對Matlab絕對陌生。 我已經用了整整一天的時間,這是我的老師要求我做的一些事情。 使用LSB算法將語句或字符串組嵌入圖像中。 該字符串將從文件中讀取。 到目前為止,我還沒有使用任何文件操作。 我正在使用一個字符嘗試此操作,但不知道出了什么問題。 該算法看起來很簡單,但是我的輸出(即覆蓋和隱蔽像素)都顯示相同的值。 :(

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)

哦,嵌套循環...這不是要走的路。

您想用輸入字符串的二進制ascii表示替換前l像素的最低有效位。


第一件出錯的事情-將char轉換為二進制:
將字符轉換為二進制表示應使用bitget完成

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

使用dec2bin ,返回1×8二進制數組

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

返回1×8 字符串 :此數組的實際數值為

>> 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支持向量/矩陣向量化操作,而不是循環。

這是一種無循環的好方法,假設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