/
bomba.py
executable file
·134 lines (99 loc) · 4.37 KB
/
bomba.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
#!/usr/bin/python
import cv2
import numpy as np
class ImageDisplay:
def __init__(self, windowname):
self.windowname = windowname
cv2.namedWindow(windowname, cv2.WINDOW_NORMAL)
def show_blend(self, images, windowname):
if len(images) == 0:
print "WARNING: No images provided!"
return
height0, width0 = images[0].shape[:2]
for i, image in enumerate(images):
if (len(image.shape) != 3) or (image.shape[2] != 3):
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
height, width = image.shape[:2]
if (height != height0):
image = cv2.resize(image, (int(float(width*height0)/float(height)), height0), fx=0, fy=0, interpolation = cv2.INTER_CUBIC)
if (i == 0):
both = image
continue
both = np.hstack((both, image))
cv2.imshow(windowname, both)
def show_separate(self, images, windowname):
if len(images) == 0:
print "WARNING: No images provided!"
return
for i, image in enumerate(images):
name = windowname
if i != 0:
name = windowname + '_' + str(i)
cv2.namedWindow(name, cv2.WINDOW_NORMAL)
cv2.imshow(name, image)
def show(self, images, windowname = 'result', blend = True):
if blend:
self.show_blend(images, windowname)
else:
self.show_separate(images, windowname)
def show_wait(self, images, blend = True):
self.show(images, blend)
cv2.waitKey(0)
cv2.destroyAllWindows()
def spin(self, imager):
imager.init_interface(self.windowname)
while (1):
images = imager.get()
self.show_blend(images, self.windowname)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
class Process:
def __init__(self, path):
self.windowname = ''
self.orig_bgr = cv2.imread(path, cv2.IMREAD_COLOR)
self.orig_bgr = cv2.resize(self.orig_bgr, None, fx=1.0, fy=1.0, interpolation = cv2.INTER_CUBIC)
self.orig_hsv = cv2.cvtColor(self.orig_bgr, cv2.COLOR_BGR2HSV)
self.orig_gry = cv2.cvtColor(self.orig_bgr, cv2.COLOR_BGR2GRAY)
def nothing(self):
return
def init_interface(self, windowname):
self.windowname = windowname
cv2.createTrackbar('l1', self.windowname, 10, 255, self.nothing)
cv2.createTrackbar('l2', self.windowname, 5, 255, self.nothing)
cv2.createTrackbar('l3', self.windowname, 0, 255, self.nothing)
cv2.createTrackbar('r1', self.windowname, 255, 255, self.nothing)
cv2.createTrackbar('r2', self.windowname, 255, 255, self.nothing)
cv2.createTrackbar('r3', self.windowname, 255, 255, self.nothing)
def hist(self, image):
from matplotlib import pyplot as plt
plt.hist(image.ravel(),256,[0,256])
#plt.draw()
def get(self):
r1 = cv2.getTrackbarPos('r1', self.windowname)
r2 = cv2.getTrackbarPos('r2', self.windowname)
r3 = cv2.getTrackbarPos('r3', self.windowname)
l1 = cv2.getTrackbarPos('l1', self.windowname)
l2 = cv2.getTrackbarPos('l2', self.windowname)
l3 = cv2.getTrackbarPos('l3', self.windowname)
# Remove light
h, s, v = cv2.split(self.orig_hsv)
kernel = np.ones((9*2+1, 9*2+1), np.uint8)
v_dilated = cv2.dilate(v, kernel, iterations = 1)
v_out = cv2.subtract(v_dilated, v)
#ret, v_t = cv2.threshold(v, l3, r3, cv2.THRESH_TRUNC)
# Binarization
#ret, ots = cv2.threshold(v_out, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
#et, ots2 = cv2.threshold(v, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
#self.hist(v_out)
#for i in xrange(l1):
# ret, mask = cv2.threshold(v_out, l2, 255, cv2.THRESH_TOZERO)
# v_out = cv2.bitwise_and(v_out, mask)
# v_out = cv2.add(v_out, (v_out/l3))
v_out = cv2.bitwise_not(v_out)
th3 = cv2.adaptiveThreshold(v, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,l1*2+1,l2)
th4 = cv2.adaptiveThreshold(v_out, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,l1*2+1,l2)
return [v_out, th3, th4]
p = Process('/root/Desktop/b.jpg')
img_disp = ImageDisplay('result')
img_disp.spin(p)