-4

I want to try extracting message from images that have been inserted messages before..but there is an error with the description bellow..

this code extract :

% Read Image Stego
IS = imread('imagestego.bmp');

% Extract RedChannel
RedChannel = IS(:,:,1);

% Convert RedChannel to biner
bitstego = uint8(reshape(dec2bin(RedChannel,8)',1,[]) = '0');
nBitstego = length(bitstego);

% Extraction
extBits = bitget(RedChannel(1:end),1).';
extMessage = char(bin2dec(reshape(int2str(extBits),8,[]).').');

and this error from extract code :

>> latihanextract
Error: File: latihanextract.m Line: 8 Column: 55
The expression to the left of the equals sign is not a valid target for an assignment.

and this is embedding code before..it's work!

coverImage = imread('foto1.jpg');
message = 'IMRON';

%EMBEDDING
RedChannel = coverImage(:,:,1);
GreenChannel = coverImage(:,:,2);
BlueChannel = coverImage(:,:,3);
bits = uint8(reshape(dec2bin(message,8)',1,[]) - '0');
nBits = length(bits);
RedChannel(1:nBits) = bitset(RedChannel(1:nBits),1,bits);
Imageresults = cat(3,RedChannel,GreenChannel,BlueChannel);
imwrite(Imageresults,'imagestego.bmp');

so what's the problem ?

  • 1
    I have no idea what you are trying to do, but in the code where you have the error instead of `message` you have `RedChannel` on line 8 – Vahe Tshitoyan Jun 07 '17 at 21:02
  • @VaheTshitoyan That's because the code getting the error is trying to **extract** the bits from `RedChannel` instead of **inserting** the bits of `message` into the image. – beaker Jun 07 '17 at 21:26
  • Please include a [mcve] as text not as a screenshot. Also, try to read and understand your code and use the matlab debugger to analyse why the error occurs, i.e. inspect your variables and check why you are asking an index that exceeds matrix dimensions. – m7913d Jun 07 '17 at 22:02

1 Answers1

6

Here's your problem...

% Read Image Stego
IS = imread('stegosaurus.bmp');

% Extract RedChannel
RedChannel = IS(:,:,1);

% Convert RedChannel to binary
bitstego = uint8(reshape(dec2bin(RedChannel,8)',1,[]) - '0');
nBitstego = length(bitstego);
% the previous 2 lines are actually unnecessary and can be deleted...
% see explanation in text below

% Extrication
extBits = bitget(RedChannel(1:end),1).';   % (1:end) gives you all of the elements
extMessage = char(bin2dec(reshape(int2str(extBits),8,[]).').')

You were trying to loop over the number of bits in the image instead of the number of bytes. bitstego is the binary representation of all of the bytes in bitstego, so bitstego is 8 times as long as RedChannel.

It's much easier in this case just to take the number of elements in RedChannel using the special index end.

beaker
  • 16,331
  • 3
  • 32
  • 49
  • Ah, that was a typo. If you look at line 8 you'll see that my code originally had a `=` where yours had a `-`. The `-` was correct. And as I said, lines 7 & 8 are not even needed with the update to line 12, so you could even just remove them. – beaker Jun 07 '17 at 22:55
  • Note that you're still going have a problem in the last line if the number of pixels in the image (rows*columns) is not evenly divisible by 8. – beaker Jun 07 '17 at 22:59
  • So the code should be like this? bitstego = uint8(reshape(dec2bin(RedChannel,8)',1,[]) - '0'); – Aldi Sudarto Nugraha Jun 07 '17 at 23:00
  • but i get the new error message : this new error message : Error using reshape Product of known dimensions, 8, not divisible into total number of elements, 100. Error in latihanextract (line 13) extMessage = char(bin2dec(reshape(int2str(extBits),8,[]).').') – Aldi Sudarto Nugraha Jun 07 '17 at 23:06
  • See my comment about "divisible by 8". – beaker Jun 07 '17 at 23:06
  • so in the last line, 8 should change to (rows*columns) or how ? sorry..i'm new user in matlab.. – Aldi Sudarto Nugraha Jun 07 '17 at 23:12
  • It depends on what you want to happen with that last (incomplete) byte. Probably the easiest is to pad it with zeros since it's unlikely to be part of the message, right? – beaker Jun 07 '17 at 23:13
  • i try delete code for line 7 and 8..and then run program latihanextract..but get error message like this : >> latihanextract Error using reshape Product of known dimensions, 8, not divisible into total number of elements, 100. Error in latihanextract (line 13) extMessage = char(bin2dec(reshape(int2str(extBits),8,[]).').'); – Aldi Sudarto Nugraha Jun 07 '17 at 23:18
  • Yes. That's the error we've been discussing that happens because the number of bytes is not divisible by 8. You need to do something to `extBits` to make its size divisible by 8. Then that last line will work. I'm not going to tell you how to do that because 1) I don't know for sure *how* you want to handle this case, 2) you probably need to figure this one out for yourself, and 3) we've fixed the error that you originally asked about. Try solving this problem on your own and if you are still having trouble, post a new question. – beaker Jun 07 '17 at 23:22