-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_comp.py
42 lines (25 loc) · 1.04 KB
/
image_comp.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
from skimage.measure import compare_ssim as ssim
import cv2
def most_similar(original,files):
results = ssim_compare(original,files)
sorted_images = sorted(results.items(),reverse = True, key=lambda val: val[1])
return sorted_images[1]
def ssim_compare(original,files):
retVal = {}
original_image = cv2.imread(original)
for file in files:
temp_cv2 = cv2.imread(file)
temp_original = original_image
temp_original,temp_cv2 = size_match_images(temp_original,temp_cv2)
height1, width1, channel1 = temp_original.shape
height2, width2, channel2 = temp_cv2.shape
retVal[file] = str(ssim(temp_original,temp_cv2,multichannel=True))
return retVal
def size_match_images(image1,image2):
height1, width1, channel1 = image1.shape
height2, width2, channel2 = image2.shape
new_height = min(height1,height2)
new_width = min(width1,width2)
image1 = cv2.resize(image1,(new_width, new_height))
image2 = cv2.resize(image2,(new_width, new_height))
return image1, image2