フィルターのクリア

Can anyone please explain the lsb embedding part of this coding? I'm unable to comprehend how it works. .

1 回表示 (過去 30 日間)
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)

採用された回答

Guillaume
Guillaume 2016 年 11 月 30 日
In a very convoluted way, this set the least significant bit of the pixels of the image to the corresponding bit of the message characters, along the rows of the image.
Here is some comments
steg=cover;
the original image has been copied into the output.
%...
mdec = uint8(message);
mbin = dec2bin(mdec, 8);
mbins= mbin(:);
converts the message to a column vector of bits. Probably not intended by the author is that this vector is all the first bits of each letter of the message, followed by all the second bits of each letter, followed by all the 3rd bits of each letter, etc. due to the way (:) works. That last line was probably meant to be:
mbins = reshape(mbin', 1, []); %transpose mbin before reshaping!
then,
%...
for i = 1:height
for j = 1:width
for more efficient indexing, it would actually be better to have the outer loop iterating over the columns. Of course, that would write the message down the columns instead of across the rows.
if(l<=len)
only modify pixel while there are still bits in the message
LSB = mod(cover(i,j), 2);
gets the lSB of the image. LSB = bitget(cover(i,j), 1); would be more explicit.
Now we get to the convoluted code
if(mbins(l)==LSB)
steg(i,j) = cover(i,j);
if the message bit and the lsb are the same, just copy the pixel since it does not need to change. Note that the () around the test serve no purpose. Moreover, as pointed out earlier, since the image has already been copied over into steg, this actually copy a value that's already there. In effect it does nothing.
else if (mbins(l)~=LSB && LSB==1 && mbins(l)==0)
steg(i,j) = cover(i,j)-1;
otherwise, if the message bit and the lsb is different (we already know that, so that test is redundant) and the lsb is one and the message bit is 0 (and since we're in a case where the two are different, since the lsb is one, the message bit is obviously 0, that test is also redundant), subtract 1 from the lsb (which is 1 as a reminder). Subtracting 1 from 1, sets it 0, so this simply sets the lsb to 0. elseif should have been used instead of else if as well. In effect the line above could be simply written as:
elseif LSB == 1
steg(i,j) = bitset(cover(i,j), 1, 1);
The next else is the same lot of redundant tests
else if (mbins(l)~=LSB && LSB==0 && mbins(l)==1)
steg(i,j) = cover(i,j)+1;
we already know that the message bit is different than the lsb if we're in the else. And since they're different, if the LSB is 0 then the message has to be 1, so this is simpler
elseif LSB == 0
steg(i,j) = bitset(cover(i, j), 1, 0);
then, because else if has been used we've got three end to close all these if, if elseif had been used, only one end would have been required.
end
end
end
The whole lot of if can be simplified to
bitset(steg(i,j), 1, mbins(l)); %not a single if needed!
And as a result you could simplify the whole code to these few lines
cover=imread('D:\l.jpg');
message = 'JKL';
mbins = reshape(dec2bin(message, 8), 1, []); %does the same as the original code.
steg = cover.'; %transpose to replicate the by-row layout of the message
bitset(steg(1:min(numel(mbins), numel(steg))), 1, mbins); %sets the lsb of steg to the corresponding message bit. replaces all the loops and ifs
steg = steg.'; %and transpose back
imwrite(steg,'D:\hidden.jpg');

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Processing and Computer Vision についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by