Skip to content

Commit 9826914

Browse files
committed
Trying to do good enough measurement
1 parent 84352d8 commit 9826914

File tree

2 files changed

+60
-35
lines changed

2 files changed

+60
-35
lines changed

Image_based/LeaderDetector.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ def __init__(self, path, image_name, b_output_debug=True, b_recalc=False):
147147

148148
quad.draw_quad(self.images["RGB_Stats"])
149149

150+
for quad in self.vertical_leader_quads:
151+
score = self.score_quad(quad)
152+
print(f"Score {score}")
153+
154+
150155
def read_images(self, path, image_name):
151156
""" Read in all of the mask, rgb, flow images
152157
If Edge image does not exist, create it
@@ -441,6 +446,26 @@ def flow_mask(self, quad):
441446
im_mask[pixel_labels[0] == label_count[-1][1]] = 255
442447
return im_mask.reshape((im_flow.shape[0], im_flow.shape[1])), im_mask_labels.reshape((im_flow.shape[0], im_flow.shape[1]))
443448

449+
def score_quad(self, quad):
450+
""" See if the quad makes sense over the optical flow image
451+
@quad - the quad
452+
"""
453+
454+
# Two checks: one, are the depth/optical fow values largely consistent under the quad center
455+
# Are there boundaries in the optical flow image where the edge of the quad is?
456+
im_flow_mask = cv2.cvtColor(self.images["Flow"], cv2.COLOR_BGR2GRAY)
457+
perc_consistant, stats_slice = quad.check_interior_depth(im_flow_mask)
458+
459+
diff = 0
460+
for i in range(1, len(stats_slice)):
461+
diff_slices = np.abs(stats_slice[i]["Median"] - stats_slice[i-1]["Median"])
462+
if diff_slices > 20:
463+
print(f"Warning: Depth values not consistant from slice {self.name} {i} {stats_slice}")
464+
diff += diff_slices
465+
if perc_consistant < 0.9:
466+
print(f"Warning: not consistant {self.name} {stats_slice}")
467+
return perc_consistant, diff / (len(stats_slice) - 1)
468+
444469

445470
if __name__ == '__main__':
446471
path = "./data/predictions/"

Image_based/cyl_fit_2d.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ def edge_pts(self, t):
7070
right_pt = [pt[0] + vec_step[1], pt[1] - vec_step[0]]
7171
return left_pt, right_pt
7272

73+
@staticmethod
74+
def _rect_in_image(im, r, pad=2):
75+
""" See if the rectangle is within the image boundaries
76+
@im - image (for width and height)
77+
@r - the rectangle
78+
@pad - how much to allow for overlap
79+
@return True or False"""
80+
if np.min(r) < pad:
81+
return False
82+
if np.max(r[:, 0]) > im.shape[1] + pad:
83+
return False
84+
if np.max(r[:, 1]) > im.shape[0] + pad:
85+
return False
86+
return True
87+
7388
def _rect_corners(self, t1, t2, perc_width=0.3):
7489
""" Get two rectangles covering the expected edges of the cylinder
7590
@param t1 starting t value
@@ -172,33 +187,30 @@ def _image_cutout(self, im, rect, step_size, height):
172187
tform3_back = np.linalg.pinv(tform3)
173188
return cv2.warpPerspective(im, tform3, (step_size, height)), tform3_back
174189

175-
def depth_mask(self, im_depth, step_size=40, perc_width=0.3):
190+
def check_interior_depth(self, im_depth, step_size=40, perc_width=0.3):
176191
""" Find which pixels are valid depth and fit average depth
177192
@param im_depth - depth image
178193
@param step_size how many pixels to move along the boundary
179194
@param perc_width How much of the radius to move in/out of the edge
180195
"""
181196
height = int(self.radius_2d)
182-
rect_destination = np.array([[0, 0], [step_size, 0], [step_size, height], [0, height]], dtype="float32")
183-
ts, rects = self.interior_rects(self, step_size=step_size, perc_width=perc_width)
184-
l_col = height // 4
185-
r_col = 3 * height // 4
186-
for r in rects:
187-
tform3 = cv2.getPerspectiveTransform(r, rect_destination)
188-
tform3_back = np.linalg.pinv(tform3)
189-
im_warp = cv2.warpPerspective(im_depth, tform3, (step_size, height))
190-
for r in range(0, step_size):
191-
row = []
192-
row_mean = []
193-
for c in range(0, height):
194-
if im_warp[r, c, 0] > 100 and im_warp[r, c, 1] < 50:
195-
row.append(c, im_warp[r, c, 0] / 255.0)
196-
if l_col < c < r_col:
197-
row_mean.append(im_warp[r, c, 0] / 255.0)
198-
199-
200-
im_debug = cv2.cvtColor(im_warp, cv2.COLOR_GRAY2RGB)
201-
return rects, ts
197+
rects, ts = self.interior_rects(step_size=step_size, perc_width=perc_width)
198+
199+
stats = []
200+
perc_consistant = 0.0
201+
for i, r in enumerate(rects):
202+
b_rect_inside = Quad._rect_in_image(im_depth, r, pad=2)
203+
204+
im_warp, tform_inv = self._image_cutout(im_depth, r, step_size=step_size, height=height)
205+
206+
stats_slice = {"Min": np.min(im_warp),
207+
"Max": np.max(im_warp),
208+
"Median": np.median(im_warp)}
209+
stats_slice["Perc_in_range"] = np.count_nonzero(np.abs(im_warp - stats_slice["Median"]) < 10) / (im_warp.size)
210+
perc_consistant += stats_slice["Perc_in_range"]
211+
stats.append(stats_slice)
212+
perc_consistant /= len(rects)
213+
return perc_consistant, stats
202214

203215
def find_edges_hough_transform(self, im_edge, step_size=40, perc_width=0.3, axs=None):
204216
"""Find the hough transform of the images in the boxes; save the line orientations
@@ -222,13 +234,7 @@ def find_edges_hough_transform(self, im_edge, step_size=40, perc_width=0.3, axs=
222234
line_b = np.zeros((3, 1))
223235
line_b[2, 0] = 1.0
224236
for i_rect, r in enumerate(rects):
225-
b_rect_inside = True
226-
if np.min(r) < -2:
227-
b_rect_inside = False
228-
if np.max(r[:, 0]) > im_edge.shape[1]:
229-
b_rect_inside = False
230-
if np.max(r[:, 1]) > im_edge.shape[0]:
231-
b_rect_inside = False
237+
b_rect_inside = Quad._rect_in_image(im_edge, r, pad=2)
232238

233239
im_warp, tform3_back = self._image_cutout(im_edge, r, step_size=step_size, height=height)
234240
i_seg = i_rect // 2
@@ -391,13 +397,7 @@ def adjust_quad_by_mask(self, im_mask, step_size=40, perc_width=1.2, axs=None):
391397
if axs is not None:
392398
axs.imshow(im_mask, origin='lower')
393399
for i, r in enumerate(rects):
394-
b_rect_inside = True
395-
if np.min(r) < -2:
396-
b_rect_inside = False
397-
if np.max(r[:, 0]) > im_mask.shape[1]:
398-
b_rect_inside = False
399-
if np.max(r[:, 1]) > im_mask.shape[0]:
400-
b_rect_inside = False
400+
b_rect_inside = Quad._rect_in_image(im_mask, r, pad=2)
401401

402402
im_warp, tform_inv = self._image_cutout(im_mask, r, step_size=step_size, height=height)
403403
if b_rect_inside and np.sum(im_warp > 0) > 0:

0 commit comments

Comments
 (0)