-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel2_2.2_GrayscaleHistogram.py
84 lines (68 loc) · 2.64 KB
/
level2_2.2_GrayscaleHistogram.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
# Global Similar (Level 2): Grayscale histogram - Normalize an exposed image.
# Modified based on [Code Reference].(https://gist.github.com/duhaime/211365edaddf7ff89c0a36d9f3f7956c).
import warnings
from skimage.transform import resize
from imageio import imread
import numpy as np
warnings.filterwarnings('ignore') # ignore warnings
# specify size of image to adjust
height = 2 ** 10
width = 2 ** 10
def get_img(path):
"""
Prepare an image for image processing tasks
"""
# imread function converts an image to a 2d grayscale array
img = imread(path, as_gray=True).astype(int)
# resize function resize image to a soecific size; the type of returned numbers is float
img = resize(img, (height, width), anti_aliasing=True, preserve_range=True)
# float → int
img = img.astype(int)
# Normalize the data from an exposed image.
img = normalize_exposure(img)
return img
def get_histogram(img):
# used in 'normalize_exposure' function
"""
Get a histogram of the target image. For a grayscale image in 8-bit,
so the histogram is a 256 units vector that represents the pixels in the image
and all values sum to 1.
"""
hei, wid = img.shape # get height and width of the target image
hist = [0.0] * 256 # create a list with 256 vacancy
for i in range(hei):
for j in range(wid):
hist[img[i, j]] += 1
return np.array(hist) / (hei * wid)
def normalize_exposure(img):
# used in 'get_img' function
"""
Normalize the data from an exposed image.
"""
histOfImage = get_histogram(img) # Get a darkness data from histogram of the target image.
# get sums of whole values in each position of the histogram
sumsArray = np.array([sum(histOfImage[:i + 1]) for i in range(len(histOfImage))])
# define the normalization values of each unit in the sumsArray
norm = np.uint8(255 * sumsArray)
# normalize the data of each position in the output image
hei, wid = img.shape
normalized = np.zeros_like(img)
for i in range(0, hei):
for j in range(0, wid):
normalized[i, j] = norm[img[i, j]]
return normalized.astype(int)
def pixel_sim(path_a, path_b):
"""
Measure the pixel-level similarity between two images
@returns:
percentage% that measures structural similarity between the input images
"""
img_1 = get_img('path{image1.jpg}')
img_2 = get_img('path{image2.jpg}')
return 1-np.sum(np.absolute(img_1 - img_2)) / (height * width) / 255
if __name__ == '__main__':
img_a = 'a.jpg'
img_b = 'b.jpg'
# get the similarity values
pixel_sim = pixel_sim(img_a, img_b)
print(str(pixel_sim*100)+"%")