Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Extract and embed data bits into image using matlab

Asked by Willam Willam on 25 Dec 2012

I got a question want to raise up here. My problem is I want to embed the 2 LSB from each of the character into 2 * 2 matrix and continuous embed until the data is successfully being embed all. But now I cant make it. Your help is really appreciated. Here is my part of the code.

fid = fopen('test.txt'); %open the text file
readText = fread(fid);
fclose(fid); %close the file function
char_in_bit = uint8(readText);
ascii_binary = dec2bin(char_in_bit,7);
size_char = size(char_in_bit); %determine the length of the text file
display(char_in_bit);
display(size_char);
display(BlockRONI);
display(ascii_binary);
for a=1 : size(ascii_binary,1)
    for b=1:6
        if blocksize==2
        n=1;
        for r=1:2
            for c=1:2
                block(r,c)=bitset(block(r,c),1,char_in_bit(n));
                n=n+1;
                block(r,c)=bitset(block(r,c),2,char_in_bit(n));
                %display(block(r,c));
            end
        end
        end
    end
    block(r,c) = bitset(block(r,c), 7, char_in_bit(n));
end
watermarked_image(y2:y2+blocksize-1, x2:x2+blocksize-1)=block;
watermarked_image_int=uint8(watermarked_image);
imwrite(watermarked_image_int,'watermarked.jpg');

The below data is my input from test.txt file 1010111 1101001 1101100 1101100 1101001 1100001 1101101 and my image that i want embed is gray-scale 8 bit image.

Kindly give me suggestion and appreciate your help

4 Comments

Willam Willam on 26 Dec 2012

From the input, I want get each input into array something like this. A=[1 1 1 1 1 1 1 1].

Walter Roberson on 26 Dec 2012

And what are you getting instead of your expected result?

Willam Willam on 29 Dec 2012

i want compare the input that i embed is same as the extracted data. But now i manage to embed the data in block size of 2x2. But the extracted data is not compatible to my input. Can sir give me some guides? Thanks

Willam Willam

Tags

No tags are associated with this question.

1 Answer

Answer by Image Analyst on 26 Dec 2012

Why is this line:

block(r,c) = bitset(block(r,c), 7, char_in_bit(n));

outside of your loops over r and c, and even outside of your b loop? Do you know what r and c are then? They're 2, so you're always setting block(2,2).

And what ia A and why should it be all 1's? In your code, what variable take on this/these value(s): "010111 1101001 1101100 1101100 1101001 1100001 1101101 "? Is that char_in_bit? Is it the whole string or just 8 0 and 1 at a time? And what about that sequence of binary numbers means that A should be all 1's?

But my main question is: are you using the debugger to help yourself? Are you stepping through code and examining variables as you go?

1 Comment

Willam Willam on 29 Dec 2012
clear all;
global x
global y
global blocksize
global block
global numblock_RONI
global N_RONI
global M_RONI
global startx
global starty
global x2_RONI
global y2_RONI
y2 = 51;
x2 = 31;
cover_object = imread('moon_surface.jpe'); %read the image file
cover_object = double(cover_object);
cover_object = uint8(cover_object);
watermarked_image = cover_object;
%block size
blocksize = 2;
%size of block
N_RONI=50; 
M_RONI=50;
%starting point of the pixel
%startx=30;
%starty=50;
%calculate number of block in the RONI
Br_RONI=floor(N_RONI/blocksize); % Blocks per row
Bc_RONI= floor(M_RONI/blocksize); % Blocks per column
numblock_RONI= Br_RONI*Bc_RONI; % number of blocks
display(numblock_RONI);
%display number of block of RONI
BlockRONI=numblock_RONI;
block=cover_object(y2:y2+blocksize-1,x2:x2+blocksize-1);
display(block);
imshow(block);
fid = fopen('test.txt'); %open the text file
readText = fread(fid);
fclose(fid); %close the file function
char_in_bit = double(readText);
display(char_in_bit);
ascii_binary = dec2bin(char_in_bit, 8);
ascii_binary = ascii_binary +0;
size_char = length(char_in_bit); %determine the length of the text file
length = length(ascii_binary);
display(BlockRONI);
%display(class(ascii_binary));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
%                           Embedding part                                %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
embed = [];%convert ascii_binary into array form
embed2 = [];
 for a1 = 1
        embed2 = bitget(ascii_binary, a1);
        embed = [embed embed2];
        %embed = reshape(embed, [], 8);
        %c = mat2cell(b, [], [1 8]);
        display(embed);
 end
for a=1 : embed
     %for b=1:1
        n=1;
        for r=1:2
            for c=1:2
                block(r,c)=bitset(block(r,c),1,embed(n));
                n=n+1;
                block(r,c)=bitset(block(r,c),2,embed(n));
                n=n+1;
            end
        end
end
watermarked_image(y2:y2+blocksize-1, x2:x2+blocksize-1)=block;
% if (x2_RONI+blocksize) > (N_RONI+startx-1)
%             if y2_RONI+blocksize < (M_RONI+starty)
%       x2_RONI=startx;
%       y2_RONI=y2_RONI+blocksize;
%             end
%         else
%             x2_RONI=x2_RONI+blocksize;
%    
% end
watermarked_image_int=uint8(watermarked_image);
imwrite(watermarked_image_int,'watermarked.jpg');
display('Done of Embeddding');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         % 
%                        Extract Algorithms                               %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cover_object = imread('watermarked.jpg'); %read the image file
cover_object = double(cover_object);
watermarked_image = cover_object;
block=cover_object(y2:y2+blocksize-1,x2:x2+blocksize-1);
retrieveblock = [];
for r=1:2
    for c=1:2             
        retrievebits=bitget(block(r,c),1);                        
        retrieveblock=[retrieveblock retrievebits];          
        retrievebits=bitget(block(r,c),2);            
        retrieveblock=[retrieveblock retrievebits];                    
    end
end  
display(retrieveblock);

this is the current stage that i do now. But i just curious why my input and my output is totally different? Can sir give me some guides?

Image Analyst

Contact us