forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram_stretch.py
64 lines (54 loc) · 1.85 KB
/
histogram_stretch.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
"""
Created on Fri Sep 28 15:22:29 2018
@author: Binish125
"""
importcopy
importos
importcv2
importnumpyasnp
frommatplotlibimportpyplotasplt
classConstantStretch:
def__init__(self):
self.img=""
self.original_image=""
self.last_list= []
self.rem=0
self.L=256
self.sk=0
self.k=0
self.number_of_rows=0
self.number_of_cols=0
defstretch(self, input_image):
self.img=cv2.imread(input_image, 0)
self.original_image=copy.deepcopy(self.img)
x, _, _=plt.hist(self.img.ravel(), 256, [0, 256], label="x")
self.k=np.sum(x)
foriinrange(len(x)):
prk=x[i] /self.k
self.sk+=prk
last= (self.L-1) *self.sk
ifself.rem!=0:
self.rem=int(last%last)
last=int(last+1ifself.rem>=0.5elselast)
self.last_list.append(last)
self.number_of_rows=int(np.ma.count(self.img) /self.img[1].size)
self.number_of_cols=self.img[1].size
foriinrange(self.number_of_cols):
forjinrange(self.number_of_rows):
num=self.img[j][i]
ifnum!=self.last_list[num]:
self.img[j][i] =self.last_list[num]
cv2.imwrite("output_data/output.jpg", self.img)
defplot_histogram(self):
plt.hist(self.img.ravel(), 256, [0, 256])
defshow_image(self):
cv2.imshow("Output-Image", self.img)
cv2.imshow("Input-Image", self.original_image)
cv2.waitKey(5000)
cv2.destroyAllWindows()
if__name__=="__main__":
file_path=os.path.join(os.path.basename(__file__), "image_data/input.jpg")
stretcher=ConstantStretch()
stretcher.stretch(file_path)
stretcher.plot_histogram()
stretcher.show_image()