-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistogramProcessing.py
128 lines (97 loc) · 3.71 KB
/
HistogramProcessing.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
import cv2
def float2int(img):
img = np.round(img, 0)
img = np.minimum(img, 255)
img = np.maximum(img, 0)
img = img.astype('uint8')
return img
def histogramEqualization(img):
img_height = img.shape[0]
img_width = img.shape[1]
histogram = np.zeros([256], np.int32)
for i in range (0, img_height):
for j in range (0, img_width):
histogram[img[i,j]] += 1
pdf = histogram/histogram.sum()
cdf = np.zeros([256],float)
cdf[0] = pdf[0]
for i in range (1,256):
cdf[i] = cdf[i-1]+ pdf[i]
cdf_eq = np.round(cdf * 255,0)
imgEqualized = np.zeros((img_height, img_width))
# map input image to s.
for i in range(0, img_height):
for j in range(0, img_width):
r = img[i, j]
s = cdf_eq[r]
imgEqualized[i, j] = s
return imgEqualized
def compute_histogram(image):
hist = np.zeros(256)
for pixel in image.ravel():
hist[pixel] += 1
return hist / np.sum(hist)
def compute_cdf(hist):
return np.cumsum(hist)
def histogram_matching(source, template):
source_hist = compute_histogram(source)
template_hist = compute_histogram(template)
source_cdf = compute_cdf(source_hist)
template_cdf = compute_cdf(template_hist)
# Create the mapping from source to template
mapping = np.zeros(256, dtype=np.uint8)
template_value = 0
for i in range(256):
while template_value < 255 and template_cdf[template_value] < source_cdf[i]:
template_value += 1
mapping[i] = template_value
matched_image = np.zeros_like(source)
for i in range(source.shape[0]):
for j in range(source.shape[1]):
matched_image[i, j] = mapping[source[i, j]]
return matched_image
def histogramMatching(source_img, reference_img):
source_height, source_width = source_img.shape
reference_hist = np.zeros([256], np.int32)
for i in range(0, reference_img.shape[0]):
for j in range(0, reference_img.shape[1]):
reference_hist[reference_img[i, j]] += 1
reference_pdf = reference_hist / reference_hist.sum()
reference_cdf = np.zeros([256], float)
reference_cdf[0] = reference_pdf[0]
for i in range(1, 256):
reference_cdf[i] = reference_cdf[i - 1] + reference_pdf[i]
source_cdf_eq = np.zeros([256], float)
source_hist = np.zeros([256], np.int32)
for i in range(0, source_height):
for j in range(0, source_width):
source_hist[source_img[i, j]] += 1
source_pdf = source_hist / source_hist.sum()
source_cdf_eq[0] = source_pdf[0]
for i in range(1, 256):
source_cdf_eq[i] = source_cdf_eq[i - 1] + source_pdf[i]
mapping = np.zeros(256, dtype='uint8')
for i in range(256):
min_diff = np.abs(reference_cdf - source_cdf_eq[i]).min()
mapping[i] = np.where(np.abs(reference_cdf - source_cdf_eq[i]) == min_diff)[0][0]
matched_img = np.zeros_like(source_img)
for i in range(0, source_height):
for j in range(0, source_width):
matched_img[i, j] = mapping[source_img[i, j]]
return matched_img
# img_low = cv2.imread('image.png', cv2.IMREAD_GRAYSCALE)
# img_eq_low = histogramEqualization(img_low)
# img_eq_low = float2int(img_eq_low)
# cv2.imshow('img_eq_low', img_eq_low)
# cv2.waitKey()
# source = cv2.imread('source.png', cv2.IMREAD_GRAYSCALE)
# template = cv2.imread('template.png', cv2.IMREAD_GRAYSCALE)
# matched_image = histogramMatching(source, template)
# matched_image = float2int(matched_image)
# # Display the images
# cv2.imshow('Source Image', source)
# cv2.imshow('Template Image', template)
# cv2.imshow('Matched Image', matched_image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()