-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.py
96 lines (71 loc) · 2.67 KB
/
color.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Example Usage from terminal: python color.py ./pics/r1-min.jpg ./pics/r2-min.jpg 0.1
import matplotlib.pyplot as plt
import cv2
import numpy as np
import sys
from PIL import Image
import argparse
def is_too_bright(pixel):
# Filters out too bright pixels which can skew mean RGB values
if pixel[0] < 250 and pixel[1] < 250 and pixel[2] < 250:
return False
else:
return True
def is_too_dark(pixel):
# Filters out too dark pixels which can skew mean RGB values
if pixel[0] > 5 and pixel[1] > 5 and pixel[2] > 5:
return False
else:
return True
def color_info(image, normalize=False):
# Returns mean values of reds, greens and blues in an image
r = 0.
g = 0.
b = 0.
total_color_pixels = 0.
for row in range(image.shape[0]):
for col in range(image.shape[1]):
pixel = image[row][col]
if not is_too_bright(pixel) and not is_too_dark(pixel):
total_color_pixels += 1
r += pixel[0]
g += pixel[1]
b += pixel[2]
if normalize:
total = r + g + b
r = r / total
g = g / total
b = b / total
return [r,g,b]
else:
return [r/total_color_pixels,g/total_color_pixels,b/total_color_pixels]
def chromatic_match(im1, im2, tolerance):
# Executes the image color comparison pipeline
if type(im1) == 'str':
im1 = cv2.imread(im1)
im1 = cv2.cvtColor(im1, cv2.COLOR_BGR2RGB)
im2 = cv2.imread(im2)
im2 = cv2.cvtColor(im2, cv2.COLOR_BGR2RGB)
im1 = np.array(Image.fromarray(im1).resize((100,120)))[20:]
im2 = np.array(Image.fromarray(im2).resize((100,120)))[20:]
else:
im1 = np.array(im1.resize((100,120)))[20:]
im2 = np.array(im2.resize((100,120)))[20:]
r1,g1,b1 = color_info(np.array(im1))
r2,g2,b2 = color_info(np.array(im2))
tr = tg = tb = False
rn1, gn1 , bn1 = color_info(np.array(im1), normalize=True)
rn2, gn2 , bn2 = color_info(np.array(im2), normalize=True)
rn = abs(rn1-rn2)/rn1
gn = abs(gn1-gn2)/gn1
bn = abs(bn1-bn2)/bn1
total_deviation = rn+gn+bn
match = total_deviation < tolerance
return match
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process inputs')
parser.add_argument('source_path', default='', help='Path to source(first) Image')
parser.add_argument('sample_path', default='', help='Path to sample(second) Image')
parser.add_argument('tolerance', type = float, default=float(0.1), help='Error tolerance')
args=parser.parse_args()
chrotmatic_match(args.source_path, args.sample_path, args.tolerance)