-
Notifications
You must be signed in to change notification settings - Fork 68
/
code.txt
431 lines (391 loc) · 14.7 KB
/
code.txt
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
################################################################################
####################### source code basic_cv_tool.py ##########################
################################################################################
import cv2
import matplotlib.pyplot as plt
import base64
import struct
import numpy as np
from scipy.interpolate import interp1d
from pylab import *
from PIL import Image
class basic_cv_tool:
def __init__(self, ImageName):
self.ImageName = ImageName
def ImageRead(self, ImageName):
img = cv2.imread(ImageName, 0)
return img
def BMP_information_analysis(self, ImageName):
with open(ImageName, 'rb') as f:
raw_info = f.read(30)
info = struct.unpack('<ccIIIIIIHH', raw_info)
if(info[0]!=b'B' or info[1] !=b'M'):
return None
else:
return {
'size' : info[2],
'bias' : info[4],
'header' : info[5],
'width' : info[6],
'height' : info[7],
'color_bit' : info[9]
}
def greyscale_reduce(self, img, reduce_index):
shape = img.shape
width = shape[0]
height = shape[1]
for i in range(width):
for j in range(height):
for k in range(3):
img[i,j,k] =(img[i,j,k]/ reduce_index) *(255 /(255 / reduce_index))
return img
def image_average(self, img):
mean = np.mean(img)
return mean
def image_variance(self, img):
var = np.var(img)
return var
def image_Nearest_neighbor_interpolation(self, img, Zoom_index):
img = cv2.resize(img, Zoom_index, interpolation = cv2.INTER_NEAREST)
return img
def image_bilinear_interpolation(self, img, Zoom_index):
img = cv2.resize(img, Zoom_index, interpolation = cv2.INTER_LINEAR)
return img
def image_bicubic_interpolation(self, img, Zoom_index):
img = cv2.resize(img, Zoom_index, interpolation = cv2.INTER_CUBIC)
return img
def image_shear(self, img, shear_index):
shear_matrix =np.array([
[1,shear_index,0],
[0,1,0]
],dtype=np.float32)
img = cv2.warpAffine(img, shear_matrix, (int(img.shape[0]*(1+shear_index)),img.shape[1]))
return img
def image_rotation(self, img, rotation_theta):
theta=rotation_theta*np.pi/180
rotate_matrix=np.array([
[np.cos(theta),-np.sin(theta),np.sin(theta)*img.shape[0]],
[np.sin(theta),np.cos(theta),0]
],dtype=np.float32)
img = cv2.warpAffine(img,rotate_matrix, (int(img.shape[0]*(np.cos(theta)+np.sin(theta))),int(img.shape[1]*(np.cos(theta)+np.sin(theta)))))
return img
def interest_point_choosing(self, ImageName):
img = array(Image.open(ImageName))
imshow(img)
fea_point = ginput(7)
fea_point = np.float32(fea_point)
fea_point = np.column_stack((fea_point,array([1,1,1,1,1,1,1])))
return fea_point
def Getting_H_Matrix(self, img_points_1, img_points_2):
H_matrix = ((img_points_2.transpose()).dot(img_points_1)).dot(np.linalg.inv((img_points_1.transpose()).dot(img_points_1)))
print(H_matrix)
return H_matrix[:2]
def calcdf(self, img):
hist, bins = np.histogram(img.flatten(), 256, [0,256])
cdf = hist.cumsum()
cdf_normalized = cdf*255/cdf.max()
cdf = (cdf-cdf[0]) *255/ (cdf[-1]-1)
cdf = cdf_normalized.astype(np.uint8)
temp = np.zeros(256,dtype = np.uint8)
j = 0
for i in range(256):
j = cdf[i]
temp[j]=i
for i in range(255):
if temp[i+1]<temp[i]:
temp[i+1] = temp[i]
return temp
def cdf(self, img):
hist, bins = np.histogram(img.flatten(), 256, [0,256])
cdf = hist.cumsum()
cdf_normalized = cdf*255/cdf.max()
cdf = (cdf-cdf[0]) *255/ (cdf[-1]-1)
cdf = cdf_normalized.astype(np.uint8)
return cdf
def createcdf(self, hist):
cdf = hist.cumsum()
cdf_normalized = cdf*255/cdf.max()
cdf = (cdf-cdf[0]) *255/ (cdf[-1]-1)
cdf = cdf_normalized.astype(np.uint8)
temp = np.zeros(256,dtype = np.uint8)
j = 0
for i in range(256):
j = cdf[i]
temp[j]=i
for i in range(255):
if temp[i+1]<temp[i]:
temp[i+1] = temp[i]
return temp
def createhisto(self, array):
array = np.array(array)
x = np.linspace(0,255,shape(array)[0])
f = interp1d(x,array,kind='linear')
x_pred=np.linspace(0,255,256)
arr = f(x_pred)
return arr
def histo_matching(self, img, cdf):
res = np.zeros((512, 512, 3), dtype =np.uint8)
res = cdf[img]
return res
def local_histo(self, img, index):
img_copy = cv2.copyMakeBorder(img,(index-1)//2,(index-1)//2,(index-1)//2,(index-1)//2, cv2.BORDER_CONSTANT,value=[0,0,0])
for i in range(np.shape(img)[0]):
for j in range(np.shape(img)[1]):
temp = cv2.equalizeHist(img_copy[i:i+index,j:j+index])
img[i,j] = temp[(index-1)//2,(index-1)//2]
return img
def segmentation(self, img):
T = 30
color = np.linspace(0,255,256)
hist , bins = np.histogram(img.flatten(), 256,[0,256])
print("image mean value is", T)
while(1):
T1 = (hist[:T]*color[:T]).sum()/hist[:T].sum()
T2 = (hist[T:]*color[T:]).sum()/hist[T:].sum()
temp = T
T = int((T1+T2)/2)
print("T1",T1,"T2",T2,"T",T)
if abs(temp-T)<0.01:
break
img1 = zeros((shape(img)[0],shape(img)[1],3),dtype = uint8)
img2 = zeros((shape(img)[0],shape(img)[1],3),dtype = uint8)
for i in range(shape(img)[0]):
for j in range(shape(img)[1]):
if img[i,j]<T:
img1[i,j] = img[i,j]
else:
img2[i,j] = img[i,j]
return img1,img2
def equalize_histogram(self, img, result_name):
equ = cv2.equalizeHist(img)
res = np.hstack((img, equ))
cv2.imwrite(result_name, res)
return equ
################################################################################
############################ script code histogram.py ##########################
################################################################################
import sys
import os
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
lib_path = os.path.abspath(os.path.join(sys.path[0], '..'))
sys.path.append(lib_path)
from src.basic_cv_tool import *
'''This is the test file for project No.3 which consists of all the required
assignments.
'''
def draw_histogram(imagename):
image_name1 = "../../homework3/project3/"+imagename+".bmp"
image_hist1 = "../../homework3/project3/"+imagename+"_hist.png"
tool = basic_cv_tool(image_name1)
img = tool.ImageRead(image_name1)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax3 = ax1.twinx()
ax1.set_title('original histogram',fontsize = 11)
ax1.hist(img.ravel(),256,[0,255])
cdf = tool.cdf(img)
ax3.plot(cdf,color = 'r')
fig.legend(('cdf','histogram'), loc = 'upper left')
plt.savefig(image_hist1)
plt.close()
def equalized_histogram(imagename):
image_name1 = "../../homework3/project3/"+imagename+".bmp"
image_cdf1 = "../../homework3/project3/"+imagename+"_cdf.png"
result_name1 = "../../homework3/result_"+imagename+".bmp"
result_cdf1 = "../../homework3/result_"+imagename+"_cdf.png"
result_hist = "../../homework3/result_"+imagename+"_hist.png"
tool = basic_cv_tool(image_name1)
img = tool.ImageRead(image_name1)
equ = tool.equalize_histogram(img, result_name1)
fig = plt.figure(figsize=(7,7),dpi = 98)
ax1 = fig.add_subplot(211)
ax3 = ax1.twinx()
ax2 = fig.add_subplot(212)
ax4 = ax2.twinx()
ax1.set_title('original histogram',fontsize = 11)
ax1.hist(img.ravel(),256,[0,255])
cdf = tool.cdf(img)
ax3.plot(cdf,color = 'r')
ax2.set_title('equalized histogram', fontsize = 11)
ax2.hist(equ.ravel(),256,[0,255])
cdf2 = tool.cdf(equ)
ax4.plot(cdf2,color = 'r')
fig.legend(('cdf','histogram'), loc = 'upper left')
plt.savefig(result_hist)
plt.close()
def histogram_specialization(imagename, cdf):
image_name = "../../homework3/project3/"+imagename+".bmp"
result_name = "../../homework3/mat_result_"+imagename+".bmp"
result_hist = "../../homework3/mat_result_"+imagename+"_hist.png"
tool = basic_cv_tool(image_name)
img = tool.ImageRead(image_name)
equ = cv2.equalizeHist(img)
mat = tool.histo_matching(equ, cdf)
res = np.hstack((img,equ, mat))
cv2.imwrite(result_name, res)
fig = plt.figure(figsize=(7,9),dpi=98)
ax1 = fig.add_subplot(311)
ax4 = ax1.twinx()
ax2 = fig.add_subplot(312)
ax5 = ax2.twinx()
ax3 = fig.add_subplot(313)
ax6 = ax3.twinx()
ax1.set_title('original histogram',fontsize = 11)
ax1.hist(img.ravel(),256,[0,255])
cdf1 = tool.cdf(img)
ax4.plot(cdf1,color = 'r')
ax2.set_title('equalized histogram', fontsize = 11)
ax2.hist(equ.ravel(),256,[0,255])
cdf2 = tool.cdf(equ)
ax5.plot(cdf2,color = 'r')
ax3.set_title('specialized histogram', fontsize = 11)
ax3.hist(equ.ravel(),256,[0,255])
cdf3 = tool.cdf(mat)
ax6.plot(cdf3,color = 'r')
fig.legend(('cdf','histogram'), loc = 'upper left')
plt.savefig(result_hist)
plt.close()
'''
p1 = plt.subplot(311)
p2 = plt.subplot(312)
p3 = plt.subplot(313)
p1.hist(img.ravel(),256,[0,255])
p1.set_title('original histogram',fontsize = 11)
p2.hist(equ.ravel(),256,[0,255])
p2.set_title('equalized histogram',fontsize= 11)
p3.hist(mat.ravel(),256,[0,255])
p3.set_title('specialized histogram',fontsize = 11)
plt.savefig(result_hist)
plt.close()
mat = tool.histo_matching(img, cdf)
res = np.hstack((img, mat))
cv2.imwrite(result_name, res)
plt.figure()
p1 = plt.subplot(211)
p2 = plt.subplot(212)
p1.hist(img.ravel(),256,[0,255])
p1.set_title('original histogram',fontsize = 11)
p2.hist(mat.ravel(),256,[0,255])
p2.set_title('specialized histogram',fontsize = 11)
plt.savefig(result_hist)
plt.close()
'''
def local_histogram(imagename,index):
image_name = "../../homework3/project3/"+imagename+".bmp"
result_name = "../../homework3/local_result_"+imagename+".bmp"
result_hist = "../../homework3/local_result_"+imagename+"_hist.png"
tool = basic_cv_tool(image_name)
img = tool.ImageRead(image_name)
fig = plt.figure(figsize=(7,7),dpi = 98)
ax1 = fig.add_subplot(211)
ax3 = ax1.twinx()
ax2 = fig.add_subplot(212)
ax4 = ax2.twinx()
ax1.set_title('original histogram',fontsize = 11)
ax1.hist(img.ravel(),256,[0,255])
cdf1 = tool.cdf(img)
ax3.plot(cdf1,color = 'r')
fig.legend(('cdf','histogram'), loc = 'upper left')
loc = tool.local_histo(img, index)
cv2.imwrite(result_name, loc)
ax2.set_title('local equalized histogram', fontsize = 11)
ax2.hist(loc.ravel(),256,[0,255])
cdf2 = tool.cdf(loc)
ax4.plot(cdf2,color = 'r')
plt.savefig(result_hist)
'''
plt.figure()
p1 = plt.subplot(211)
p2 = plt.subplot(212)
p1.hist(img.ravel(),256,[0,255])
p1.set_title('original histogram',fontsize = 11)
loc = tool.local_histo(img, index)
cv2.imwrite(result_name, loc)
p2.hist(loc.ravel(),256,[0,255])
p2.set_title('local equalized histogram',fontsize = 11)
plt.savefig(result_hist)'''
plt.close()
def calcdf(imagename):
img_name = "../../homework3/project3/"+imagename+".bmp"
tool = basic_cv_tool(img_name)
img = tool.ImageRead(img_name)
cdf = tool.calcdf(img)
return cdf
def hist_segmentation(imagename):
image_name = "../../homework3/project3/"+imagename+".bmp"
result_name1 = "../../homework3/seg_result1_"+imagename+".bmp"
result_name2 = "../../homework3/seg_result2_"+imagename+".bmp"
result_hist = "../../homework3/seg_result_"+imagename+"_hist.png"
tool = basic_cv_tool(image_name)
img = tool.ImageRead(image_name)
img1, img2 = tool.segmentation(img)
cv2.imwrite(result_name1, img1)
cv2.imwrite(result_name2, img2)
fig = plt.figure(figsize=(7,7),dpi = 98)
ax1 = fig.add_subplot(211)
ax3 = ax1.twinx()
ax2 = fig.add_subplot(212)
ax4 = ax2.twinx()
ax1.set_title('image 1 histogram',fontsize = 11)
ax1.hist(img1.ravel(),256,[0,255])
cdf = tool.cdf(img1)
ax3.plot(cdf,color = 'r')
fig.legend(('cdf','histogram'), loc = 'upper left')
ax2.set_title('image 2 histogram', fontsize = 11)
ax2.hist(img2.ravel(),256,[0,255])
cdf2 = tool.cdf(img2)
ax4.plot(cdf2,color = 'r')
plt.savefig(result_hist)
'''
plt.figure()
p1 = plt.subplot(211)
p2 = plt.subplot(212)
p1.hist(img1.ravel(),256,[0,255])
p1.set_title('image1 histogram',fontsize = 11)
#loc = tool.local_histo(img1, index)
cv2.imwrite(result_name1, img1)
cv2.imwrite(result_name2, img2)
p2.hist(img2.ravel(),256,[0,255])
p2.set_title('image2 histogram',fontsize = 11)
plt.savefig(result_hist)'''
plt.close()
if __name__ == '__main__':
draw_histogram('lena')
draw_histogram('elain')
draw_histogram('woman')
draw_histogram('citywall')
#Assignment 1, equalized histogram transformation
equalized_histogram('lena')
equalized_histogram('elain')
equalized_histogram('lena1')
equalized_histogram('lena2')
equalized_histogram('lena4')
equalized_histogram('elain1')
equalized_histogram('elain2')
equalized_histogram('elain3')
equalized_histogram('woman')
equalized_histogram('woman1')
equalized_histogram('woman2')
equalized_histogram('citywall')
equalized_histogram('citywall1')
equalized_histogram('citywall2')
#Assignment 2, specialized histogram transformation
cdf1 = calcdf('lena')
cdf2 = calcdf('elain')
cdf3 = calcdf('citywall')
cdf4 = calcdf('woman')
histogram_specialization('lena1',cdf1)
histogram_specialization('lena2',cdf1)
histogram_specialization('lena4',cdf1)
histogram_specialization('elain1',cdf2)
histogram_specialization('elain2',cdf2)
histogram_specialization('elain3',cdf2)
histogram_specialization('citywall1',cdf3)
histogram_specialization('citywall2',cdf3)
histogram_specialization('woman1',cdf4)
histogram_specialization('woman2',cdf4)
#Assignment 3, local histogram transformation using equalization transformation.
local_histogram('lena',7)
local_histogram('elain',7)
hist_segmentation('elain')
hist_segmentation('woman')