-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram_assignment1_part1.py
234 lines (168 loc) · 7.02 KB
/
histogram_assignment1_part1.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -*- coding: utf-8 -*-
import numpy as np
import cv2
from matplotlib import pyplot as plt
from scipy import ndimage as ndi
from mpl_toolkits.mplot3d import Axes3D
def twoDHistogram(x, y, img):
#Source - https://matplotlib.org/3.1.1/gallery/mplot3d/hist3d.html
#Takes the LAB image, its a* and b* component as the input and returns a 2D histogram of the image
fig = plt.figure()
plt.title('3D bar plot of 2D histogram of the LAB image')
ax = fig.add_subplot(111, projection='3d')
#x, y = np.random.rand(2, 100) * 4
#Compute the 2D histogram
hist, xedges, yedges = np.histogram2d(x.ravel(), y.ravel(), bins=100)
# Construct arrays for the anchor positions of the 16 bars.
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25, indexing="ij")
xpos = xpos.ravel()
ypos = ypos.ravel()
zpos = 0
# Construct arrays with dimensions for the 16 bars.
dx = dy = 0.5 * np.ones_like(zpos)
dz = hist.ravel()
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort= 'average')
return hist
def computeSpatialDerivative(x):
#Source - https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.gaussian_filter.html
#Source - https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.diff.html
#Apply Gaussian filter to the luminance component
filteredImage = ndi.gaussian_filter(x,sigma = 5)
plt.figure()
plt.title('After gaussian filter')
plt.imshow(filteredImage,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
#Computing the spatial derivative of the luminance component in the horixontal (x) direction
lx_derivative = np.diff(filteredImage, axis = 0) #axis = 0 indicates X axis
plt.figure()
plt.title('Spatial derivative of the luminance component in the horixontal (x) direction')
plt.imshow(lx_derivative,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
#Computing the spatial derivative of the luminance component in the vertical (y) direction
ly_derivative = np.diff(filteredImage, axis = 1) #axis = 1 indicates Y axis
plt.figure()
plt.title('Spatial derivative of the luminance component in the vertical (y) direction')
plt.imshow(ly_derivative,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
return x
def twoDBackProjection(l_original,a_original,b_original,lab_original_img):
#Source - https://pysource.com/2018/03/30/histogram-and-back-projection-opencv-3-4-with-python-3-tutorial-28/
#Source - http://www.ee.columbia.edu/ln/dvmm/publications/PhD_theses/jrsmith-thesis.pdf
#Read the cropped image
cropped_img = plt.imread('cropped_img1.jpg')
#Display the cropped image
plt.figure()
plt.title('Cropped image')
plt.imshow(cropped_img,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
#Convert the cropped image to LAB format
lab_cropped_img = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2LAB)
#Display LAB cropped image
#Display the cropped image
plt.figure()
plt.title('LAB Cropped image')
plt.imshow(lab_cropped_img,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
#Splitting L, a* and b* components of the cropped image
l_cropped, a_cropped, b_cropped = cv2.split(lab_cropped_img)
# Histogram of the cropped image
lab_cropped_hist = cv2.calcHist([lab_cropped_img],[1,2],None,[256,256],[0,256,0,256])
#lab_cropped_hist = twoDHistogram(a_cropped, b_cropped, lab_cropped_img)
mask = cv2.calcBackProject([lab_original_img], [1, 2], lab_cropped_hist, [0, 256, 0, 256], 1)
#Generating an elliptical kernel of array size 5x5
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
#Convolution of the mask with the kernel
mask = cv2.filter2D(mask, -1, kernel)
#Perform threasholding
_, mask = cv2.threshold(mask, 100, 255, cv2.THRESH_BINARY)
#Merging the 3 components of the mask
mask = cv2.merge((mask, mask, mask))
#Bitwise AND operation of the original LAB image and the mask
result = cv2.bitwise_and(lab_original_img, mask)
'''
#Display the original image
plt.figure()
plt.title('Original LAB image')
plt.imshow(lab_original_img,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
'''
#Display the mask
plt.figure()
plt.title('Mask')
plt.imshow(mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Display the result of back projection
plt.figure()
plt.title('Result')
plt.imshow(result,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
return
def histEqualization(l_comp):
#Source: https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
#Source: Pg. 24, Programming Computer Vision with Python, Jan Erik Solem
#http://programmingcomputervision.com/downloads/ProgrammingComputerVision_CCdraft.pdf
#Compute 1D histogram of the luminance component
hist_l_comp,bins = np.histogram(l_comp.flatten(),256)
#The transform function is in this case a cumulative distribution
#function (cdf) of the pixel values in the image (normalized to map
#the range of pixel values to the desired range).
cdf = hist_l_comp.cumsum() #cumulative distribution function
cdf = 255 * cdf / cdf[-1] #normalize to map the range of pixel values to the desired range
# use linear interpolation of cdf to find new pixel values
im_hist_eq = np.interp(l_comp.flatten(),bins[:-1],cdf)
hist_eq = im_hist_eq.reshape(l_comp.shape)
plt.figure()
plt.title('After Histogram equalization of the Luminance component')
plt.imshow(hist_eq,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
return
def main():
#Load a color image and show it
img = plt.imread('img1.jpg')
#print(img)
#Display the image
plt.figure()
plt.title('Image')
plt.imshow(img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Converting the image from RGB to LAB
LABimg = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l_comp, a_comp, b_comp = cv2.split(LABimg)
#Display the L component
plt.figure()
plt.title('L')
plt.imshow(l_comp,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
#Display the a* component
plt.figure()
plt.title('a*')
plt.imshow(a_comp,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
#Display the b* component
plt.figure()
plt.title('b*')
plt.imshow(b_comp,cmap='gray')
cv2.waitKey(0)
cv2.destroyAllWindows()
#Computing spatial derivative of the luminance component
computeSpatialDerivative(l_comp)
#Computing a 2D histogram with the chrominance (a,b) component
twoDHistogram(a_comp, b_comp, LABimg)
#Performing histogram equilization for a 1D histogram using Luminance component
histEqualization(l_comp)
#Computing back projection map using 2D Histogram with the chrominance (a,b) component
twoDBackProjection(l_comp,a_comp,b_comp,LABimg)
if __name__ == '__main__':
main()