├── README └── Yanjun-Tiegang ├── README ├── algo.py ├── example ├── data.json └── test.jpg └── utils.py /README: -------------------------------------------------------------------------------- 1 | This repo includes codes for various Image forgery detection Algorithms published. 2 | 3 | python and Opencv Library are used. 4 | 5 | -------------------------------------------------------------------------------- /Yanjun-Tiegang/README: -------------------------------------------------------------------------------- 1 | Link: http://www.ccse.kfupm.edu.sa/~ahmadsm/coe589-121/cao2012-image-forgery.pdf 2 | 3 | 4 | Details of Algorithm: 5 | 6 | 1) Divide the block into small blocks of 16x16 or 8x8 7 | 2) Apply DCT on blocks 8 | 3) Extract DCT coefficients of lower frequency by selecting coefficients from 1st square of [W/2,W/2] size 9 | 4) Overlap a circle of radius W/2 with origin at center of square, and form vectors of dimension 4 as explained in the paper 10 | 5) similarity between feature vectors is calculated 11 | 12 | 13 | == Work in Progress == 14 | -------------------------------------------------------------------------------- /Yanjun-Tiegang/algo.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import cv2 3 | import numpy as np 4 | from utils import pointsInsideCircle, compare, zigzag 5 | from math import pi as PI 6 | 7 | 8 | W = 8 #block size for comparision 9 | Dsim = 0.1 #threshold for symmetry 10 | Nd = 25 #nearest block 11 | 12 | quadrants_points = pointsInsideCircle(W/4) #(i,j) position of blocks which are partially/completely inside circle of radius W/2 13 | zigzag_points = zigzag(W/2) 14 | test_image = cv2.imread(sys.argv[1],0) 15 | height,width = test_image.shape[:2] 16 | #print (height,width) 17 | vectors_list = [] 18 | for j in range(0,height-W+1): 19 | for i in range(0,width-W+1): 20 | block = test_image[j:j+W,i:i+W] 21 | dct_block = cv2.dct(np.float32(block)) 22 | feature_block = [[],[],[],[]] 23 | for index,coeff_list in enumerate(zigzag_points): 24 | for coeff in coeff_list: 25 | feature_block[index].append(dct_block[coeff[0],coeff[1]]) 26 | 27 | feature_block_np = np.array(feature_block) 28 | 29 | feature_vector = [] 30 | for quadrant,points in quadrants_points.iteritems(): 31 | summ = 0 32 | for point in points: 33 | summ = summ + feature_block_np[point[0],point[1]] 34 | feature_vector.append(summ/PI) 35 | vectors_list.append(np.array(feature_vector)) 36 | 37 | vectors_list2 = cv2.sort(np.array(vectors_list),cv2.SORT_EVERY_ROW) 38 | print "vectors calculated" 39 | import json 40 | with open('data.json', 'w') as outfile: 41 | json.dump(vectors_list2.tolist(), outfile) 42 | 43 | 44 | i=0 45 | blocks = [] 46 | for i in range(0,len(vectors_list)): 47 | if i%width == 0: 48 | print i/width 49 | posA = [i/width,i%width] 50 | j = i+1 51 | for j in range(i+1,len(vectors_list)): 52 | posB = [j/width,j%width] 53 | if compare(vectors_list[i],vectors_list[j],posA,posB,Dsim,Nd): 54 | print (posA,posB) 55 | blocks.append([posA,posB]) 56 | 57 | output_image = cv2.imread(sys.argv[1],1) 58 | for block in blocks: 59 | x1 = block[0][0] 60 | x1_8 = block[0][0]+W 61 | y1 = block[0][1] 62 | y1_8 = block[0][1]+W 63 | 64 | output_image[x1:x1_8,y1:y1_8] = [0,0,255] 65 | 66 | x2 = block[1][0] 67 | x2_8 = block[1][0]+W 68 | y2 = block[1][1] 69 | y2_8 = block[1][1]+W 70 | 71 | output_image[x2:x2_8,y2:y2_8]=[0,255,0] 72 | 73 | cv2.imwrite("output.jpg",output_image) 74 | 75 | 76 | print "feature vectors extracted" 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /Yanjun-Tiegang/example/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dewgeek/image-forgery-detection/da93be3f0d81df9d2e6958a82bfaba066d723be4/Yanjun-Tiegang/example/test.jpg -------------------------------------------------------------------------------- /Yanjun-Tiegang/utils.py: -------------------------------------------------------------------------------- 1 | from random import randrange 2 | import numpy as np 3 | from math import sqrt 4 | 5 | """ 6 | 7 | 8 | 9 | """ 10 | def insideCircle(i,j,radius): 11 | x,y=radius,radius 12 | d =sqrt((i-x)*(i-x)+(j-y)*(j-y)) 13 | if d<=radius: 14 | return True 15 | else: 16 | return False 17 | 18 | 19 | def pointsInsideCircle(radius): 20 | W = radius 21 | i=0;j=0; 22 | #forming points and grouping them into Quadrants 23 | points = {} 24 | points['I']=[] 25 | points['II']=[] 26 | points['III']=[] 27 | points['IV']=[] 28 | for i in range(0,2*W): 29 | for j in range(0,2*W): 30 | if i < W: 31 | if j Dsim and sim < 0.2: 58 | d = sqrt(pow(posA[0]-posB[0],2)+pow(posB[0]-posB[1],2)) 59 | if d > Nd: 60 | print [d,sim] 61 | return True 62 | else: 63 | return False 64 | else: 65 | return False 66 | 67 | 68 | def zigzag(max_value): 69 | coeffs = [[],[],[],[]] 70 | counter = 0 71 | index = 0 72 | for i in range(0,max_value+1): 73 | j = i 74 | for k in range(0,i+1): 75 | if i%2 == 0: 76 | coeffs[index].append([j,k]) 77 | else: 78 | coeffs[index].append([k,j]) 79 | j=j-1 80 | counter = counter+1 81 | if counter%max_value == 0: 82 | index = index+1 83 | coeffs[index].append([0,max_value+1]) 84 | return coeffs 85 | --------------------------------------------------------------------------------