-
Notifications
You must be signed in to change notification settings - Fork 34
/
two_bin.py
101 lines (92 loc) · 3.08 KB
/
two_bin.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
import cv2
import numpy as np
from matplotlib import pyplot as plt
def get_pixel(img, center, x, y):
new_value = 0
try:
if img[x][y] >= center:
new_value = 1
except:
pass
return new_value
def calculated_pixel(img, x, y):
center = img[x][y]
left = get_pixel(img, center, x, y-1)
right = get_pixel(img, center, x, y+1)
if left>right:
return 100
else:
return 200
def show_output(output_list):
output_list_len = len(output_list)
figure = plt.figure()
for i in range(output_list_len):
current_dict = output_list[i]
current_img = current_dict["img"]
current_xlabel = current_dict["xlabel"]
current_ylabel = current_dict["ylabel"]
current_xtick = current_dict["xtick"]
current_ytick = current_dict["ytick"]
current_title = current_dict["title"]
current_type = current_dict["type"]
current_plot = figure.add_subplot(1, output_list_len, i+1)
if current_type == "gray":
current_plot.imshow(current_img, cmap = plt.get_cmap('gray'))
current_plot.set_title(current_title)
current_plot.set_xticks(current_xtick)
current_plot.set_yticks(current_ytick)
current_plot.set_xlabel(current_xlabel)
current_plot.set_ylabel(current_ylabel)
elif current_type == "histogram":
current_plot.plot(current_img, color = "black")
current_plot.set_xlim([0,260])
current_plot.set_title(current_title)
current_plot.set_xlabel(current_xlabel)
current_plot.set_ylabel(current_ylabel)
ytick_list = [int(i) for i in current_plot.get_yticks()]
current_plot.set_yticklabels(ytick_list,rotation = 90)
plt.show()
def main():
image_file = 'lenna.jpg'
img_bgr = cv2.imread(image_file)
height, width, channel = img_bgr.shape
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
img_two_bin = np.zeros((height, width,3), np.uint8)
for i in range(0, height):
for j in range(0, width):
img_two_bin[i, j] = calculated_pixel(img_gray, i, j)
hist_two_bin = cv2.calcHist([img_two_bin], [0], None, [256], [0, 256])
output_list = []
output_list.append({
"img": img_gray,
"xlabel": "",
"ylabel": "",
"xtick": [],
"ytick": [],
"title": "Gray Image",
"type": "gray"
})
output_list.append({
"img": img_two_bin,
"xlabel": "",
"ylabel": "",
"xtick": [],
"ytick": [],
"title": "Two Bin Image",
"type": "gray"
})
output_list.append({
"img": hist_two_bin,
"xlabel": "Bins",
"ylabel": "Number of pixels",
"xtick": None,
"ytick": None,
"title": "Histogram(Two Bin)",
"type": "histogram"
})
show_output(output_list)
cv2.waitKey(0)
cv2.destroyAllWindows()
print("Two bin Program is finished")
if __name__ == '__main__':
main()