-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.py
executable file
·206 lines (167 loc) · 8.23 KB
/
detect.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
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import filter
# The Specifications of clock
# base on the distributed degree with the detected circle to determine min_angle and max_angle
min_angle = 17
max_angle = 340
min_value = 0
max_value = 150
def determine_avg_circles(circles):
"""
It takes a list of circles and returns the average circle
:param circles: The output of cv2.HoughCircles()
:return: The average of the circles.
"""
return np.mean(circles, axis=1, dtype=np.int32).reshape((3,))
def compute_distance(x1, y1, x2, y2):
"""
It takes four numbers as input and returns the distance between two points
:param x1: x coordinate of the first point
:param y1: the y-coordinate of the first point
:param x2: x-coordinate of the second point
:param y2: the y coordinate of the second point
:return: The distance between two points.
"""
return np.sqrt((x2 - x1)**2 + (y2 - y1)**2)
def detect_circle(image):
"""
It takes an image, crops it to a square, resizes it to 400x400, improves the contrast, detects the
edges, detects the circle, and draws the circle and the degree lines on the image
:param image: the image to be processed
:return: x, y, r, image
"""
height, width = image.shape[:2]
residual = int((width - height) / 2) if width > height else 0
image = image[:, residual:width-residual] # crop image to make width and height equal
height, width = image.shape[:2]
ratio = 400/height
height, width = int(height*ratio), int(width*ratio)
image = cv.resize(image, (width, height)) # desired image 400x400
img = filter.filter_clahe(image) # improve contrast of image
img = filter.filter_laplacian(img, 9) # detect edge of image to get information easily
circles = cv.HoughCircles(img,cv.HOUGH_GRADIENT, 1, 35, param2=35, minRadius=int(height*0.35), maxRadius=int(height*0.55))
if isinstance(circles, type(None)):
return
circles = np.uint16(np.around(circles))
x, y, r = determine_avg_circles(circles)
# draw center and circle
cv.circle(image, (x, y), r, (0, 0, 255), 3, cv.LINE_AA) # draw circle
cv.circle(image, (x, y), 2, (0, 255, 0), 3, cv.LINE_AA) # draw center of circle
# this part is only used to visualize the circle and the distributed degree
separation = 10.0 #in degrees
interval = int(360 / separation)
p1 = np.zeros((interval,2)) #set empty arrays
p2 = np.zeros((interval,2))
p_text = np.zeros((interval,2))
for i in range(0,interval):
for j in range(0,2):
if (j%2==0):
p1[i][j] = x + 0.9 * r * np.cos(separation * i * np.pi / 180) #point for lines
else:
p1[i][j] = y + 0.9 * r * np.sin(separation * i * np.pi / 180)
text_offset_x = 10
text_offset_y = 5
for i in range(0, interval):
for j in range(0, 2):
if (j % 2 == 0):
p2[i][j] = x + r * np.cos(separation * i * np.pi / 180)
p_text[i][j] = x - text_offset_x + 1.1 * r * np.cos((separation) * (i+9) * np.pi / 180) #point for text labels, i+9 rotates the labels by 90 degrees
else:
p2[i][j] = y + r * np.sin(separation * i * np.pi / 180)
p_text[i][j] = y + text_offset_y + 1.1 * r * np.sin((separation) * (i+9) * np.pi / 180) # point for text labels, i+9 rotates the labels by 90 degrees
# add the lines and labels to the image
for i in range(0,interval):
cv.line(image, (int(p1[i][0]), int(p1[i][1])), (int(p2[i][0]), int(p2[i][1])),(0, 255, 0), 2)
cv.putText(image, '%s' %(int(i*separation)), (int(p_text[i][0]), int(p_text[i][1])), cv.FONT_HERSHEY_SIMPLEX, 0.3,(0,0,0),1,cv.LINE_AA)
return x, y, r, image
def detect_line(image, circle, min_angle, max_angle, min_value, max_value, x, y, r):
"""
It takes in an image, the center of the gauge, the minimum and maximum angles of the gauge, the
minimum and maximum values of the gauge, and the radius of the gauge. It then returns the value of
the gauge
:param image: the image we're working with
:param circle: the image of the gauge
:param min_angle: the minimum angle of the gauge (e.g. 0)
:param max_angle: the maximum angle of the gauge (e.g. 240)
:param min_value: the minimum value of the gauge
:param max_value: the maximum value of the gauge
:param x: x coordinate of the center of the gauge
:param y: y coordinate of the center of the gauge
:param r: radius of the circle
:return: The median value of the list of values and the circle image
"""
height, width = image.shape[:2]
residual = int((width - height) / 2) if width > height else 0
image = image[:, residual:width-residual] # crop image to make width and height equal
height, width = image.shape[:2]
ratio = 400/height
height, width = int(height*ratio), int(width*ratio)
img = cv.resize(image, (width, height))
img = filter.filter_clahe(img)
dst = filter.filter_laplacian(img, 5)
lines = cv.HoughLinesP(image=dst, rho=2, theta=np.pi / 180, threshold=100, minLineLength=0, maxLineGap=0)
if isinstance(lines, type(None)):
return
# this part is used to select the good line
final_line_list = []
diff1LowerBound = 0.0 # diff1LowerBound and diff1UpperBound determine how close the line should be from the center
diff1UpperBound = 0.3
diff2LowerBound = 0.55 # diff2LowerBound and diff2UpperBound determine how close the other point of the line should be to the outside of the gauge
diff2UpperBound = 1.0
for i in range(lines.shape[0]):
for x1, y1, x2, y2 in lines[i]:
diff1 = compute_distance(x, y, x1, y1)
diff2 = compute_distance(x, y, x2, y2)
if diff1 > diff2:
diff1, diff2 = diff2, diff1
if (((diff1<diff1UpperBound*r) and (diff1>diff1LowerBound*r) and (diff2<diff2UpperBound*r)) and (diff2>diff2LowerBound*r)):
final_line_list.append([x1, y1, x2, y2])
# this part is used to determine the real degree
# using bilinear interpolation to approximate the value of the point between 2 point min and max value of the clock
res = []
for x1, y1, x2, y2 in final_line_list:
dist_pt_0 = compute_distance(x, y, x1, y1)
dist_pt_1 = compute_distance(x, y, x2, y2)
if (dist_pt_0 > dist_pt_1):
x_angle1 = x1 - x
x_angle2 = x1 - x2
x_angle = sum([x_angle1, x_angle2]) / 2
y_angle1 = y - y1
y_angle2 = y2 - y1
y_angle = sum([y_angle1, y_angle2]) / 2
else:
x_angle1 = x2 - x
x_angle2 = x2 - x1
x_angle = sum([x_angle1, x_angle2]) / 2
y_angle1 = y - y2
y_angle2 = y1 - y2
y_angle = sum([y_angle1, y_angle2]) / 2
degree1 = np.arctan(np.divide(float(y_angle1), float(x_angle1)))
degree2 = np.arctan(np.divide(float(y_angle2), float(x_angle2)))
degree = sum([degree1, 3*degree2]) / 4
degree = np.rad2deg(degree)
if x_angle > 0 and y_angle > 0: #in quadrant I
final_angle = float(270 - degree)
elif x_angle < 0 and y_angle > 0: #in quadrant II
final_angle = float(90 - degree)
elif x_angle < 0 and y_angle < 0: #in quadrant III
final_angle = float(90 - degree)
elif x_angle > 0 and y_angle < 0: #in quadrant IV
final_angle = float(270 - degree)
# bilinear interpolation
value = ((max_angle-final_angle)/(max_angle-min_angle)) * min_value + ((final_angle-min_angle)/(max_angle-min_angle)) * max_value
res.append(value)
# only using to visualize the lines
for x1, y1, x2, y2 in final_line_list:
cv.line(circle, (x1, y1), (x2, y2),(0, 255, 0), 2)
cv.imwrite('result_image/detected.png', circle)
if len(res) == 0:
return None, circle
return np.median(np.array(res)), circle
if __name__ == '__main__':
image = cv.imread('test_image/test2.png')
x, y, r, circle = detect_circle(image)
res, img = detect_line(image, circle, min_angle, max_angle, min_value, max_value, x, y, r)
print(res)