ICAIAME Image Tamper Detection Source Code

Watermark image

clc; 
%% Tanimlamalar
imge='d:\hbmPhd\IDCard.png';
I = imread(imge); % Kapak goruntu
R = I(:,:,1); % Kirmizi
G = I(:,:,2); % Yesil
B = I(:,:,3); % Mavi 

%% Resmi bloklara ayirma islemi
RBlocks = cell(8,8);
GBlocks = cell(8,8);
BBlocks = cell(8,8);
counti = 0;
for i = 1:256:257
   counti = counti + 1;
   countj = 0;
   for j = 1:256:257
        countj = countj + 1;
        RBlocks{counti,countj} = R(i:i+255,j:j+255);    
        GBlocks{counti,countj} = G(i:i+255,j:j+255); 
        BBlocks{counti,countj} = B(i:i+255,j:j+255); 
   end
end

%% MSB leri LSB lere yazma islemi
for i=1:1:256
    for j=1:1:256
        % 1. blogun MSB lerini gonder
        % R pikseller
        source=dec2bin(RBlocks{1,1}(i,j),8);
        dest=dec2bin(RBlocks{1,2}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        RBlocks{1,2}(i,j)=bin2dec(dest);    
        % G pikseller
        source=dec2bin(GBlocks{1,1}(i,j),8);
        dest=dec2bin(GBlocks{2,1}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        GBlocks{2,1}(i,j)=bin2dec(dest);
        % B pikseller
        source=dec2bin(BBlocks{1,1}(i,j),8);
        dest=dec2bin(BBlocks{2,2}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        BBlocks{2,2}(i,j)=bin2dec(dest);   
        % 2. blogun MSB lerini gonder
        % R pikseller
        source=dec2bin(RBlocks{1,2}(i,j),8);
        dest=dec2bin(RBlocks{2,1}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        RBlocks{2,1}(i,j)=bin2dec(dest);    
        % G pikseller
        source=dec2bin(GBlocks{1,2}(i,j),8);
        dest=dec2bin(GBlocks{2,2}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        GBlocks{2,2}(i,j)=bin2dec(dest);
        % B pikseller
        source=dec2bin(BBlocks{1,2}(i,j),8);
        dest=dec2bin(BBlocks{1,1}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        BBlocks{1,1}(i,j)=bin2dec(dest);
        % 3. blogun MSB lerini gonder
        % R pikseller
        source=dec2bin(RBlocks{2,1}(i,j),8);
        dest=dec2bin(RBlocks{2,2}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        RBlocks{2,2}(i,j)=bin2dec(dest);    
        % G pikseller
        source=dec2bin(GBlocks{2,1}(i,j),8);
        dest=dec2bin(GBlocks{1,1}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        GBlocks{1,1}(i,j)=bin2dec(dest);
        % B pikseller
        source=dec2bin(BBlocks{2,1}(i,j),8);
        dest=dec2bin(BBlocks{1,2}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        BBlocks{1,2}(i,j)=bin2dec(dest);      
        % 4. blogun MSB lerini gonder
        % R pikseller
        source=dec2bin(RBlocks{2,2}(i,j),8);
        dest=dec2bin(RBlocks{1,1}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        RBlocks{1,1}(i,j)=bin2dec(dest);    
        % G pikseller
        source=dec2bin(GBlocks{2,2}(i,j),8);
        dest=dec2bin(GBlocks{1,2}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        GBlocks{1,2}(i,j)=bin2dec(dest);
        % B pikseller
        source=dec2bin(BBlocks{2,2}(i,j),8);
        dest=dec2bin(BBlocks{2,1}(i,j),8);
        dest(7)=source(1);
        dest(8)=source(2);
        BBlocks{2,1}(i,j)=bin2dec(dest);
    end
end
%% Islenmis bloklari birlestirme
BRow = 1;
BCol = 1;
PRow = 1;
PCol = 1;
for i=1:1:512
    BCol = 1;
    for j=1:1:512
        noi = mod(i,256);
        noj = mod(j,256);
        if (noi==0)
            noi=256;
        end
        if (noj==0)
            noj=256;
        end
        WR(i,j)=RBlocks{BRow,BCol}(noi,noj);
        WG(i,j)=GBlocks{BRow,BCol}(noi,noj);
        WB(i,j)=BBlocks{BRow,BCol}(noi,noj);
        if (mod(j,256)==0) 
            BCol = BCol + 1;
        end
    end
    if (mod(i,256)==0) 
       BRow = BRow + 1;
    end
end
WMI=cat(3,WR,WG,WB);
figure;imshow(WMI);
imwrite(WMI,'d:\hbmPhd\Watermarked.png');
psnr(I,WMI)
ssim(I,WMI)

Recover

clc; 
%% Tanimlamalar
imge='d:\hbmPhd\Tampered.jpg';
I = imread(imge); % Goruntu
R = I(:,:,1); % Kirmizi
G = I(:,:,2); % Yesil
B = I(:,:,3); % Mavi 

% Tamper tarama esik degeri. 0 ile 6 aras? olabiliyor. 
% Esik degeri azaldikca pikseldeki tamper olasiligi artar
treshold=3; 

%% Resmi bloklara ayirma islemi
RBlocks = cell(8,8);
GBlocks = cell(8,8);
BBlocks = cell(8,8);
counti = 0;
for i = 1:256:257
   counti = counti + 1;
   countj = 0;
   for j = 1:256:257
        countj = countj + 1;
        RBlocks{counti,countj} = R(i:i+255,j:j+255);    
        GBlocks{counti,countj} = G(i:i+255,j:j+255); 
        BBlocks{counti,countj} = B(i:i+255,j:j+255); 
   end
end

% Blokları kopyala
CRBlocks = cell(8,8);
CGBlocks = cell(8,8);
CBBlocks = cell(8,8);
counti = 0;
for i = 1:256:257
   counti = counti + 1;
   countj = 0;
   for j = 1:256:257
        countj = countj + 1;
        CRBlocks{counti,countj} = R(i:i+255,j:j+255);    
        CGBlocks{counti,countj} = G(i:i+255,j:j+255); 
        CBBlocks{counti,countj} = B(i:i+255,j:j+255); 
   end
end

% Blokları kopyala
DRBlocks = cell(8,8);
DGBlocks = cell(8,8);
DBBlocks = cell(8,8);
counti = 0;
for i = 1:256:257
   counti = counti + 1;
   countj = 0;
   for j = 1:256:257
        countj = countj + 1;
        DRBlocks{counti,countj} = R(i:i+255,j:j+255);    
        DGBlocks{counti,countj} = G(i:i+255,j:j+255); 
        DBBlocks{counti,countj} = B(i:i+255,j:j+255); 
   end
end

%% MSB leri LSB lerle kontrol etme işlemi

for i=1:1:256
    for j=1:1:256
        % 1. blogun MSB lerini kontrol et
        % R pikseller
        source1=dec2bin(RBlocks{1,1}(i,j),8);
        source2=dec2bin(GBlocks{1,1}(i,j),8);
        source3=dec2bin(BBlocks{1,1}(i,j),8);
        dest1=dec2bin(RBlocks{1,2}(i,j),8);
        dest2=dec2bin(GBlocks{2,1}(i,j),8);
        dest3=dec2bin(BBlocks{2,2}(i,j),8);           
        tamper=6;
        if (dest1(7)==source1(1)) 
            tamper=tamper-1;
        end
        if (dest1(8)==source1(2)) 
            tamper=tamper-1;
        end
        if (dest2(7)==source2(1))
            tamper=tamper-1;
        end
        if (dest2(8)==source2(2)) 
            tamper=tamper-1;
        end
        if (dest3(7)==source3(1)) 
            tamper=tamper-1;
        end
        if (dest3(8)==source3(2))
            tamper=tamper-1;
        end
        if tamper>treshold            
            % Piksel bozulmu?. Bozuk pikseli g?stermek i?in DBlocks'ta siyaha boya  
            %DRBlocks{1,1}(i,j)=0;
            %DGBlocks{1,1}(i,j)=0;
            %DBBlocks{1,1}(i,j)=0;
            % Bozuk pikseli tamir et ve CBlocks'a at
            % R tamir et
            source=dec2bin(RBlocks{1,1}(i,j),8);
            dest=dec2bin(RBlocks{1,2}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CRBlocks{1,1}(i,j)=bin2dec(source);
            % G tamir et
            source=dec2bin(GBlocks{1,1}(i,j),8);
            dest=dec2bin(GBlocks{2,1}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CGBlocks{1,1}(i,j)=bin2dec(source);
            % B tamir et
            source=dec2bin(BBlocks{1,1}(i,j),8);
            dest=dec2bin(BBlocks{2,2}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CBBlocks{1,1}(i,j)=bin2dec(source);
        else
            DRBlocks{1,1}(i,j)=0;
            DGBlocks{1,1}(i,j)=0;
            DBBlocks{1,1}(i,j)=0;
        end        
        % 2. blogun MSB lerini kontrol et
        % R pikseller
        source1=dec2bin(RBlocks{1,2}(i,j),8);
        source2=dec2bin(GBlocks{1,2}(i,j),8);
        source3=dec2bin(BBlocks{1,2}(i,j),8);
        dest1=dec2bin(RBlocks{2,1}(i,j),8);
        dest2=dec2bin(GBlocks{2,2}(i,j),8);
        dest3=dec2bin(BBlocks{1,1}(i,j),8);
        tamper=6;
        if (dest1(7)==source1(1)) 
            tamper=tamper-1;
        end
        if (dest1(8)==source1(2)) 
            tamper=tamper-1;
        end
        if (dest2(7)==source2(1))
            tamper=tamper-1;
        end
        if (dest2(8)==source2(2)) 
            tamper=tamper-1;
        end
        if (dest3(7)==source3(1)) 
            tamper=tamper-1;
        end
        if (dest3(8)==source3(2))
            tamper=tamper-1;
        end
        if tamper>treshold             
            % Piksel bozulmu?. Bozuk pikseli g?stermek i?in DBlocks'ta siyaha boya  
            %DRBlocks{1,2}(i,j)=0;
            %DGBlocks{1,2}(i,j)=0;
            %DBBlocks{1,2}(i,j)=0;
            % Bozuk pikseli tamir et ve CBlocks'a at
            % R tamir et
            source=dec2bin(RBlocks{1,2}(i,j),8);
            dest=dec2bin(RBlocks{2,1}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CRBlocks{1,2}(i,j)=bin2dec(source);
            % G tamir et
            source=dec2bin(GBlocks{1,2}(i,j),8);
            dest=dec2bin(GBlocks{2,2}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CGBlocks{1,2}(i,j)=bin2dec(source);
            % B tamir et
            source=dec2bin(BBlocks{1,2}(i,j),8);
            dest=dec2bin(BBlocks{1,1}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CBBlocks{1,2}(i,j)=bin2dec(source);
        else
            DRBlocks{1,2}(i,j)=0;
            DGBlocks{1,2}(i,j)=0;
            DBBlocks{1,2}(i,j)=0;
        end
        
        % 3. blogun MSB lerini kontrol et
        % R pikseller
        source1=dec2bin(RBlocks{2,1}(i,j),8);
        source2=dec2bin(GBlocks{2,1}(i,j),8);
        source3=dec2bin(BBlocks{2,1}(i,j),8);
        dest1=dec2bin(RBlocks{2,2}(i,j),8);
        dest2=dec2bin(GBlocks{1,1}(i,j),8);
        dest3=dec2bin(BBlocks{1,2}(i,j),8);
        tamper=6;
        if (dest1(7)==source1(1)) 
            tamper=tamper-1;
        end
        if (dest1(8)==source1(2)) 
            tamper=tamper-1;
        end
        if (dest2(7)==source2(1))
            tamper=tamper-1;
        end
        if (dest2(8)==source2(2)) 
            tamper=tamper-1;
        end
        if (dest3(7)==source3(1)) 
            tamper=tamper-1;
        end
        if (dest3(8)==source3(2))
            tamper=tamper-1;
        end
        if tamper>treshold            
            % Piksel bozulmu? Bozuk pikseli göstermek için DBlocks'ta siyaha boya  
            % R tamir et
            source=dec2bin(RBlocks{2,1}(i,j),8);
            dest=dec2bin(RBlocks{2,2}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CRBlocks{2,1}(i,j)=bin2dec(source);
            % G tamir et
            source=dec2bin(GBlocks{2,1}(i,j),8);
            dest=dec2bin(GBlocks{1,1}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CGBlocks{2,1}(i,j)=bin2dec(source);
            % B tamir et
            source=dec2bin(BBlocks{2,1}(i,j),8);
            dest=dec2bin(BBlocks{1,2}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CBBlocks{2,1}(i,j)=bin2dec(source);
        
        else
            DRBlocks{2,1}(i,j)=0;
            DGBlocks{2,1}(i,j)=0;
            DBBlocks{2,1}(i,j)=0;
        end
    
        
        % 4. blogun MSB lerini kontrol et
        % R pikseller
        source1=dec2bin(RBlocks{2,2}(i,j),8);
        source2=dec2bin(GBlocks{2,2}(i,j),8);
        source3=dec2bin(BBlocks{2,2}(i,j),8);
        dest1=dec2bin(RBlocks{1,1}(i,j),8);
        dest2=dec2bin(GBlocks{1,2}(i,j),8);
        dest3=dec2bin(BBlocks{2,1}(i,j),8);
        tamper=6;
        if (dest1(7)==source1(1)) 
            tamper=tamper-1;
        end
        if (dest1(8)==source1(2)) 
            tamper=tamper-1;
        end
        if (dest2(7)==source2(1))
            tamper=tamper-1;
        end
        if (dest2(8)==source2(2)) 
            tamper=tamper-1;
        end
        if (dest3(7)==source3(1)) 
            tamper=tamper-1;
        end
        if (dest3(8)==source3(2))
            tamper=tamper-1;
        end
        if tamper>treshold       
            % Piksel bozulmu? Bozuk pikseli göstermek için DBlocks'ta siyaha boya 
            % Bozuk pikseli tamir et ve CBlocks'a at 
            % R tamir et
            source=dec2bin(RBlocks{2,2}(i,j),8);
            dest=dec2bin(RBlocks{1,1}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CRBlocks{2,2}(i,j)=bin2dec(source);
            % G tamir et
            source=dec2bin(GBlocks{2,2}(i,j),8);
            dest=dec2bin(GBlocks{1,2}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CGBlocks{2,2}(i,j)=bin2dec(source);
            % B tamir et
            source=dec2bin(BBlocks{2,2}(i,j),8);
            dest=dec2bin(BBlocks{2,1}(i,j),8);
            source(1)=dest(7);
            source(2)=dest(8);
            CBBlocks{2,2}(i,j)=bin2dec(source);
        else
            DRBlocks{2,2}(i,j)=0;
            DGBlocks{2,2}(i,j)=0;
            DBBlocks{2,2}(i,j)=0;
        end
    end
end
%% Islenmis bloklari birlestirme
BRow = 1;
BCol = 1;
PRow = 1;
PCol = 1;
for i=1:1:512
    BCol = 1;
    for j=1:1:512
        noi = mod(i,256);
        noj = mod(j,256);
        if (noi==0)
            noi=256;
        end
        if (noj==0)
            noj=256;
        end
        WR(i,j)=CRBlocks{BRow,BCol}(noi,noj);
        WG(i,j)=CGBlocks{BRow,BCol}(noi,noj);
        WB(i,j)=CBBlocks{BRow,BCol}(noi,noj);
        
        DWR(i,j)=DRBlocks{BRow,BCol}(noi,noj);
        DWG(i,j)=DGBlocks{BRow,BCol}(noi,noj);
        DWB(i,j)=DBBlocks{BRow,BCol}(noi,noj);
        
        if (mod(j,256)==0) 
            BCol = BCol + 1;
        end
    end
    if (mod(i,256)==0) 
       BRow = BRow + 1;
    end
end

%% Sonuclar ve ekrana yazdirma

figure;
subplot(2,2,2);
imshow(I);title('Tampered image');

WMI=cat(3,WR,WG,WB);
subplot(2,2,4);
imshow(WMI);title('Recovered image');
imwrite(WMI,'d:\hbmPhd\Recovered.png');

psnr(I,WMI) % Tampered vs Recovered
ssim(I,WMI)

imge='d:\hbmPhd\IDCard.png';
I = imread(imge); % Orijinal Goruntu
subplot(2,2,1);
imshow(I);title('Original image');

DWMI=cat(3,DWR,DWG,DWB);
subplot(2,2,3);
imshow(DWMI);title('Tampered region');
imwrite(DWMI,'d:\hbmPhd\marked.png');

psnr(I,WMI) % Original vs Recovered
ssim(I,WMI)