-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_comparison.py
77 lines (51 loc) · 1.67 KB
/
image_comparison.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
This program is my first attempt to find duplicate images and similar images using python.
It will heavily use python hashing.
"""
import os
import sys
from scipy.misc import imread
from scipy.linalg import norm
from scipy import sum, average
SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
ORIGINAL_IMG = os.path.join(SCRIPT_PATH, "swiss_blade.jpg")
RESIZED_IMG = os.path.join(SCRIPT_PATH, "swiss_blade1.jpg")
def normalise(arr):
"""
"""
rng = arr.max()-arr.min()
amin = arr.min()
return (arr-amin)*255/rng
def compareImages(img1, img2):
"""
Parameters:
img1: the first image.
img2: the second image.
Return:
numerical representaiton of the difference between the images.
"""
img1 = normalise(img1)
img2 = normalise(img2)
# calculate the difference and its norms
diff = img1 - img2 # elementwise for scipy arrays
m_norm = sum(abs(diff)) # Manhattan norm
z_norm = norm(diff.ravel(), 0) # Zero norm
return (m_norm, z_norm)
def toGrayscale(arr):
"""
Parameters:
arr: array representation of an image.
This function convers the arr (3D image) to a greyscale image if it is a colour image(2D)
"""
if len(arr.shape) == 3:
return average(arr, -1) # average over the last axis (color channels)
else:
return arr
def main():
original_img = toGrayscale(imread(ORIGINAL_IMG).astype(float))
resized_img = toGrayscale(imread(RESIZED_IMG).astype(float))
n_m, n_0 = compareImages(original_img, resized_img)
print("Manhattan norm:", n_m, "/ per pixel:", n_m/img1.size)
print("Zero norm:", n_0, "/ per pixel:", n_0*1.0/img1.size)
if __name__ == "__main__":
main()