Extracting 8 bits of binary at a time (to convert to ASCII) from a long row using a for loop?

4 views (last 30 days)
I have this script that analyses an encoded picture which has a message in it and looks for the difference in the two pictures. :
clear;
close all;
pic=imread('Cat.png');
pic1=imread('CodedCat.png');
origpic=double(pic);
cpic=double(pic1);
[rows, columns]=size(origpic);
Difference = abs(origpic - cpic);
bin_message = zeros(1, rows*columns);
n=0;
for row = 1 : rows
for col = 1 : columns
if Difference(row, columns) == 1
bin_message(n) = 1;
end
n = n + 1;
end
end
I understand that I now have a 1d array with 1s and 0s and need to grab 8 bits at a time and convert each set of 8 bits back to the original message character.
I was given this hint(this is homework): message(1) = char(bin2dec(bin_message(1:8))) message(2) = char(bin2dec(bin_message(9:16))) and I have to use a for loop. I am so stuck. I don't understand how I can pull it out 8 columns at a time and then go to ASCII?? Can anyone shed some light?

Answers (1)

Image Analyst
Image Analyst on 23 Nov 2015
Here's a snippet from my text-into-image steganography demo:
% Now extract the string.
retrievedBits = bitget(stegoImage(1:numPixelsNeededForString), 1)
numPixelsNeededForString/bitsPerLetter])
letterCount = 1;
for k = 1 : bitsPerLetter : numPixelsNeededForString
% Get the binary bits for this one character.
thisString = retrievedBits(k:(k+bitsPerLetter-1));
% Turn it from a binary string into an ASCII number (integer) and then finally into a character/letter.
thisChar = char(bin2dec(num2str(thisString)));
% Store this letter as we build up the recovered string.
recoveredString(letterCount) = thisChar;
letterCount = letterCount + 1;
end
message = sprintf('The recovered string = \n%s\n', recoveredString);
fprintf('%s\n', message); % Also print to command window.
% Display a popup message to the user with the recovered string.
uiwait(helpdlg(message));
Adapt as needed.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!