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.

DistValue=CalImDist(OriImg,DistImg,MaskSize,NUBN)
function DistValue=CalImDist(OriImg,DistImg,MaskSize,NUBN)
%OriImg is the file path for the original image
%DistImg is the file path for the distorted image
%Masksize is the size for the mask used, please set it to 5 as default
%If NUBN is not calculated, then pass NUBN=0 as a flag.
%DistValue is the distortion value calculated.
%An example call: distortion=CalImDist('origimg.tif','distimg.tif',5,0)

%Read Images
img0=double(imread(OriImg));
img1=double(imread(DistImg));

%calculate NUBN
if NUBN==0
    NUBN=CalNUBN(img0);
end

%Find Distorted Points
[rowNum,colNum]=size(img0);
RecordMetri=img1-img0;
[rows,cols]=find(RecordMetri~=0);
S_num=max(size(rows));

%Calculate Normalized WeiTable
CenX=double(floor(MaskSize/2))+1;
CenY=double(floor(MaskSize/2))+1;
WeiTable=zeros(MaskSize,MaskSize);
InterWei=0;
for i=1:MaskSize
    for j=1:MaskSize
        if (i==CenX)&(j==CenY)
            InterWei=0;
        else
            InterWei=1/sqrt((i-CenX)^2+(j-CenY)^2);
        end
        WeiTable(i,j)=InterWei;
    end
end
Total=sum(sum(WeiTable));
WeiTable=WeiTable/Total;

%Calculate Distortion Values
TotalDist=0;
tabwid=double((MaskSize-1)/2);
left_m=1+tabwid;
right_m=rowNum-tabwid;
upper_n=1+tabwid;
lower_n=colNum-tabwid;
for count=1:S_num
    this_row=rows(count);
    this_col=cols(count);
    
    left_m_img=this_row-tabwid;
    right_m_img=this_row+tabwid;
    upper_n_img=this_col-tabwid;
    lower_n_img=this_col+tabwid;
    if this_row<left_m || this_row>right_m || this_col<upper_n || this_col>lower_n
        if this_row<left_m, left_m_img=1; end;
        if this_row>right_m, right_m_img=rowNum; end;
        if this_col<upper_n, upper_n_img=1;end;
        if this_col>lower_n, lower_n_img=colNum;end;
        DifBlk=ones(MaskSize);
        left_m_corner=left_m_img-(this_row-tabwid)+1;
        right_m_corner=right_m_img-(this_row+tabwid)+MaskSize;
        upper_n_corner=upper_n_img-(this_col-tabwid)+1;
        lower_n_corner=lower_n_img-(this_col+tabwid)+MaskSize;
        DifBlk(left_m_corner:right_m_corner,upper_n_corner:lower_n_corner)=xor(img0(left_m_img:right_m_img,upper_n_img:lower_n_img),img1(this_row,this_col));
    else
        DifBlk=xor(img0(left_m_img:right_m_img,upper_n_img:lower_n_img),img1(this_row,this_col));
        %if img(m,n)==0, DifBlk=~DifBlk; end; %If 0 is flipped to 1, Difference Matrix is NOT
    end        
    DRD_blk=sum(sum(DifBlk.*WeiTable));
    TotalDist=TotalDist+DRD_blk;
end
DistValue=TotalDist/NUBN;


function NUBN=CalNUBN(ImIn)
blksize=8;
[inRows, inCols]=size(ImIn);
RowsNum=double(floor(inRows/8));
ColsNum=double(floor(inCols/8));

Rows=blksize*RowsNum;
Cols=blksize*ColsNum;
in_matrix=ImIn(1:Rows,1:Cols);

rows = 1:8;
cols = rows;
[m n]=size(in_matrix);

k=0;
for i=0:(RowsNum-1)
  for j=0:(ColsNum-1)
    x = in_matrix(i*blksize+rows,j*blksize+cols);    

    if max(max(x))==min(min(x)),k=k+1;end
  end
end

NUBN=(RowsNum*ColsNum-k);

Contact us