Thursday 25 July 2013

Image Watermarking Matlab code

Here We Give the code of Image watermarking.


K=8;                                      
Q=50;                                     
W=im2bw(imread('s1.jpg'));                
Mm=size(W,1);                             
Nm=size(W,2);  
  
  
figure(1);  
subplot(321);  
imshow(W);  
title('the orginal watermark');  
  
I=imread('s12.jpg');  
subplot(322);  
imshow(I);  
title('the cover image');  
  
  
II=I;                   % to save the original image(I)  
  
blockrow=Mm;  
blockcol=Nm;  
  
for i=1:blockrow  
    for j=1:blockcol  
        x=(i-1)*K+1;  
        y=(j-1)*K+1;  
        BLOCK=II(x:x+K-1,y:y+K-1);  
        [U,S,V]=svd(double(BLOCK));  
  
        bit=W(i,j);                             %get the one bit wateramrk=bit  
        remainder=rem(S(1,1),Q);  
        if (bit==1)                             %embedding bit '1'  
            if (remainder<=Q/4)  
                S(1,1)=S(1,1)-remainder-Q/4;  
            else   
                S(1,1)=S(1,1)-remainder+3*Q/4;  
            end  
        else                                    %embedding bit '0'  
            if (remainder>=3*Q/4)  
                S(1,1)=S(1,1)-remainder+5*Q/4;  
            else  
                S(1,1)=S(1,1)-remainder+Q/4;  
            end  
        end  
  
        BLOCKW=U*S*V';                    
          
        II(x:x+K-1,y:y+K-1)=uint8(round(BLOCKW));  
          
    end  
end  
  
subplot(324);  
imshow(II);  
title('the watermarked image');  
  
% psnr1=PSNR(I,II)                            
  
A=double(II)-double(I);  
rsm=0;  
for i=1:size(A,1)  
    for j=1:size(A,2)  
        rsm=rsm+A(i,j)*A(i,j);  
    end  
end  
rsm=sqrt(rsm)/(size(A,1)*size(A,2))       
  
% to extracting the wateramrk from image(II)  
for i=1:blockrow  
    for j=1:blockcol  
        x=(i-1)*K+1;  
        y=(j-1)*K+1;  
        BLOCK=II(x:x+K-1,y:y+K-1);  
        [U,S,V]=svd(double(BLOCK));  
          
        remainder=rem(S(1,1),Q);  
        if (remainder>Q/2)  
            EW(i,j)=1;  
        else  
            EW(i,j)=0;  
        end  
    end  
end  
subplot(323);  
imshow(EW);  
title('the extracted wateramrk');  
  
% the watermarked image is attacked   
imwrite(uint8(II),'attack.jpg','jpeg','Quality',70);           %
II1=imread('attack.jpg');  
  

subplot(326);  
imshow(II1);  
title('the attacked image');  
  
for i=1:blockrow  
    for j=1:blockcol  
        x=(i-1)*K+1;  
        y=(j-1)*K+1;  
        BLOCK=II1(x:x+K-1,y:y+K-1);  
        [U,S,V]=svd(double(BLOCK));  
          
        remainder=rem(S(1,1),Q);  
        if (remainder>Q/2)  
            EW(i,j)=1;  
        else  
            EW(i,j)=0;  
        end  
    end  
end  
subplot(325);  
imshow(EW);  
title('the extracted wateramrk');  
  
corr2=NC(double(W),double(EW))              
psnr2=PSNR(I,II1)                           

Kindly Bookmark and Share it:

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...