CHAPTER-5 IMPLEMENTATION DETAILS
5.5. PROGRAM CODE
5.5..1 INTRODUCTION
The following Matlab code is executed on Microsoft windows XP professional version
2002 with intel ( R) Pentium ( R) D CPU 3.00GHZ 3.01GHZ and 1GB RAM system and
the Matlab software version used is 7.0.
5.5.2 PROGRAM CODE FOR EXACT MATCH METHOD
clear all;
close all;
% Converting an RGB image into a Grey scale image
A1= imread('image.bmp');
A= rgb2gray(A1);
figure,imshow(A);
[m n]=size(A);
% Preparing the index regarding the row and column position of the matrix
index=[];
for j=1:n-(B-1)
   for i=1:m-(B-1)
      index=[index;i,j];
   end
end
[y u]=size(index);
% Sliding a B x B block along the image
A1=A(:,1:187);
A2=im2col(A1,[B B],'sliding');
A3=A2';
A4=A(:,183:370);
A5=im2col(A4,[B B ],'sliding');
A6=A5';
A7=[A3;A6]; % Resultant matrix of size (m-B+1)(n-B+1) by BxB
A8=length(A7);
[B,index1]=sortrows(A7); % preparing the index for the resultant matrix
% Identifying identical rows
B1=diff(B);
B2=B(1,:);
B3=[B2;B1];
B4=abs(B3);
B5=B(1:A8-1,:)-B(2:A8,:);
B6=B(A8,:);
B7=[B5;B6];
B8=abs(B7);
C=B4';
C1=sum(C);
C2=C1';
                                                                                33
            DETECTION OF COPY MOVE FORGERY IN DIGITAL IMAGES
                                          CHAPTER-5 IMPLEMENTATION DETAILS
C3=B8';
C4=sum(C3);
C5=C4';
C6=C2.*C5;
D=find(C6==0);
E=index1([D]);
r=index([E],:);
G=length(r);
for H=1:G
   p=r(H,1);
   q=r(H,2);
   A(p,q)=255;
end
figure,imshow(A);
5.5.3 PROGRAM CODE FOR ROBUST MATCH METHOD
clear all;
close all;
% Converting an RGB image into a Grey scale image
A1=imread('buck.bmp');
A=rgb2gray(A1);
figure,imshow(A);
[m n]=size(A);
% Preparing the index regarding the row and column position of the matrix
index=[];
for j=1:n-7
   for i=1:m-7
      index=[index;i,j];
   end
end
[y u]=size(index);
% Sliding a B x B block along the image
A1=A(:,1:188);
A2=im2col(A1,[8 8],'sliding');
A3=A2';
A4=A(:,182:368);
A5=im2col(A4,[8 8],'sliding');
A6=A5';
A7=[A3;A6]; % Resultant matrix of size (m-B+1)(n-B+1) by BxB
A8=length(A7);
[o y]=size(A7);
l=[16 11 10 16 24 40 51 61;12 12 14 19 26 58 60 55;14 13 16 24 40 57 69 56;14 17 22
29 51 87 80 62;18 22 37 56 68 109 103 77;24 35 55 64 81 104 113 92;49 64 78 87 103
121 120 101;72 92 95 98 112 100 103 99]; % 8 X 8 quantization matrix
% calculating DCT for each block and dividing by quantization matrix
for v=1:o
   j=col2im(A7(v,:),[1 1],[8 8],'sliding');
   t=dct2(j);
                                                                                 34
           DETECTION OF COPY MOVE FORGERY IN DIGITAL IMAGES
                                    CHAPTER-5 IMPLEMENTATION DETAILS
   Z=t./l;
   k=reshape(Z,1,y);
   A7(v,:)=k;
end
[B,index1]=sortrows(A7);
% Identifying identical rows
B1=diff(B);
B2=B(1,:);
B3=[B2;B1];
B4=abs(B3);
B5=B(1:A8-1,:)-B(2:A8,:);
B6=B(A8,:);
B7=[B5;B6];
B8=abs(B7);
C=B4';
C1=sum(C);
C2=C1';
C3=B8';
C4=sum(C3);
C5=C4';
C6=C2.*C5;
D=find(C6==0);
E=index1([D]);
r=index([E],:);
G=length(r);
for H=1:G
   p=r(H,1);
   q=r(H,2);
A(p,q)=255;
 endfigure,
imshow(A);
                                                                  35
            DETECTION OF COPY MOVE FORGERY IN DIGITAL IMAGES
                                             CHAPTER-5 IMPLEMENTATION DETAILS
5.5.4 MATLAB FUNCTIONS USED AND THEIR DESCRIPTION
IMREAD : The imread function reads an image from any supported graphics image file
format, in any of the supported bit depths. Most image file formats use 8 bits to store
pixel values. When these are read into memory, MATLAB stores them as class uint8.
For file formats that support 16-bit data, PNG and TIFF, MATLAB stores the images as
class uint16.
RGB2GRAY : rgb2gray converts RGB images to grayscale by eliminating the hue and
saturation information while retaining the luminance.
IM2COL : Converts each sliding m-by-n block of A into a column of B, with no zero
padding. B has m*n rows and contains as many columns as there are m-by-n
neighborhoods of A. If the size of A is [mm nn], then the size of B is (m*n)-by-((mm-
m+1)*(nn-n+1)).
SORTROWS : Sort rows in ascending order
    [B,index] = sortrows(A) also returns an index vector index.
DIFF : Y = diff(X) calculates differences between adjacent elements of X. If X is a
vector, then diff(X) returns a vector, one element shorter than X, of differences between
adjacent elements: [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]
If X is a matrix, then diff(X) returns a matrix of row differences: [X(2:m,:)-X(1:m-1,:)]
ABS : abs(X) returns an array Y such that each element of Y is the absolute value of
the corresponding element of X.
SUM : B = sum(A) returns sums along different dimensions of an array. If A is a vector,
sum(A) returns the sum of the elements. If A is a matrix, sum(A) treats the columns of A
as vectors, returning a row vector of the sums of each column. If A is a
multidimensional array, sum(A) treats the values along the first non-singleton dimension
as vectors, returning an array of row vectors.
                                                                                      36
            DETECTION OF COPY MOVE FORGERY IN DIGITAL IMAGES