Code covered by the BSD License  

Highlights from
Binary Image Watermarking/Data Hiding: Data, Algorithms, and Distortion Measure

Binary Image Watermarking/Data Hiding: Data, Algorithms, and Distortion Measure

by

 

26 Apr 2012 (Updated )

A package on binary image watermarking/data hiding, including data, algorithms & distortion measure.

extract_attack.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function encode_t1(scaling,bias) %Embedding(winsize,sigma,scaling,bias)

clear all
% blksize=8;
% MaskSize=5;
% testimg='french-4';
load SecureResults\paras
%ImIn = imread(['SecureResults\',testimg,'-wm-m',num2str(MaskSize),'.tif']);
ImIn = imread(['SecureResults\',testimg,'-wmp21-m',num2str(MaskSize),'.tif']);
[inRows, inCols]=size(ImIn);
RowsNum=double(floor(inRows/blksize));
ColsNum=double(floor(inCols/blksize));
Rows=blksize*RowsNum;
Cols=blksize*ColsNum;
Im0=zeros(Rows,Cols);%ignore rest if blocks don't fit well. Testing purpose only. Zero-padding in practice.
Im0=ImIn(1:Rows,1:Cols);

clear ImIn 

Im0=double(Im0);

%shift key generation
sblksize=blksize;%2^skwid;
sklen_row=ceil(Rows/sblksize);
sklen_col=ceil(Cols/sblksize);
sklen=skwid*(sklen_row+sklen_col);
load SecureResults\skey
if max(size(skey))~=sklen
    error('shift key sizes do not match!');
end
 
%2-D Shifting%%%%%%%%%%%%%%%%%%%
blkrows = 1:sblksize;
blkcols = blkrows;
srIm0=Im0;
%row shift first, shift to left side
for i=1:sklen_row
    this_rskey=skey(((i-1)*skwid+1):(i*skwid));
    this_rswid=num2str(this_rskey(1));
    for j=2:skwid
        this_rswid=[this_rswid,num2str(this_rskey(j))];
    end
    this_rswid=bin2dec(this_rswid);
    srIm0(((i-1)*sblksize+1):(i*sblksize),1:(Cols-this_rswid))=Im0(((i-1)*sblksize+1):(i*sblksize),(this_rswid+1):Cols);
    srIm0(((i-1)*sblksize+1):(i*sblksize),(Cols-this_rswid+1):Cols)=Im0(((i-1)*sblksize+1):(i*sblksize),1:this_rswid);
end
%Shift upwards.
s2Im0=srIm0;
for i=1:sklen_col
    this_cskey=skey(((sklen_row+i-1)*skwid+1):((sklen_row+i)*skwid));
    this_cswid=num2str(this_cskey(1));
    for j=2:skwid
        this_cswid=[this_cswid,num2str(this_cskey(j))];
    end
    this_cswid=bin2dec(this_cswid);
    s2Im0(1:(Rows-this_cswid),((i-1)*sblksize+1):(i*sblksize))=srIm0((this_cswid+1):Rows,((i-1)*sblksize+1):(i*sblksize));
    s2Im0((Rows-this_cswid+1):Rows,((i-1)*sblksize+1):(i*sblksize))=srIm0(1:this_cswid,((i-1)*sblksize+1):(i*sblksize));
end
%s2Im0 is the 2D shifted version in which the odd-even feature is used to embed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

nubn_blksize=mark_length(s2Im0,blksize,RowsNum,ColsNum,1);
disp(['There are ', num2str(nubn_blksize),' non-uniform ', num2str(blksize), 'x', num2str(blksize),' blocks out of ',num2str(RowsNum*ColsNum)]);

%calculate flipping scores (DRDM)
%im_scores=CalImDRDMs(Im0,MaskSize);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dataextractedp21=zeros(nubn_blksize,1);
load SecureResults\datahidden
blkrows = 1:blksize;
blkcols = blkrows;
k=0;
Im0_show=Im0*0.3+0.7;
for i=0:(RowsNum-1)
    for j=0:(ColsNum-1)
        blk0=s2Im0(i*blksize+blkrows, j*blksize+blkcols);
        if max(max(blk0))~=min(min(blk0))
            k=k+1;
            sum_blk=sum(sum(blk0));
            dataextractedp21(k)=mod(sum_blk,2);            
            if dataextractedp21(k)~=datahidden(k)                
                for blki=blkrows
                    for blkj=blkcols
                        %Do Inverse shifting in effect!
                        this_sj=floor((j*blksize+blkj-1)/sblksize);
                        this_cskey=skey(((sklen_row+this_sj)*skwid+1):((sklen_row+this_sj+1)*skwid));
                        this_cswid=num2str(this_cskey(1));
                        for blkk=2:skwid
                            this_cswid=[this_cswid,num2str(this_cskey(blkk))];
                        end
                        this_cswid=bin2dec(this_cswid);                        
                        srIm0i=blki+i*blksize;
                        if srIm0i<Rows-this_cswid+1
                            blkiIm0=srIm0i+this_cswid;
                        else
                            blkiIm0=srIm0i+this_cswid-Rows;
                        end
                        
                        this_si=floor((blkiIm0-1)/sblksize);
                        this_rskey=skey((this_si*skwid+1):((this_si+1)*skwid));
                        this_rswid=num2str(this_rskey(1));
                        for blkk=2:skwid
                            this_rswid=[this_rswid,num2str(this_rskey(blkk))];
                        end
                        this_rswid=bin2dec(this_rswid);
                        s0Im0j=blkj+j*blksize;
                        if s0Im0j<Cols-this_rswid+1
                            blkjIm0=s0Im0j+this_rswid;
                        else
                            blkjIm0=s0Im0j+this_rswid-Cols;
                        end                        
                        Im0_show((blkiIm0-1):(blkiIm0+1),(blkjIm0-1):(blkjIm0+1))=zeros(3);
                    end
                end                
            end
        end
    end
end
save SecureResults\dataextractedp21 dataextractedp21
disp('Data Hidden Extracted!');
diff=xor(datahidden,dataextractedp21);
error=sum(sum(diff));
disp(['Error in data extracted is ',num2str(error),' bits']);

imwrite(mat2gray(Im0_show), ['SecureResults\',testimg,'-wmp21showatt-m',num2str(MaskSize),'.tif']);
fid=fopen(['SecureResults\',testimg,'-hiding_LOG-m',num2str(MaskSize),'.tex'],'a');
fprintf(fid,'\n\nDATA HIDDEN EXTRACTION AFTER ATTACK:');
fprintf(fid,'\nThere are %d non-uniform  %d by %d, blocks out of %d blocks.',nubn_blksize,blksize,blksize,RowsNum*ColsNum);
fprintf(fid,'\nError in the hidden data extracted is %d bits.',error);
fclose(fid);

Contact us