@@ -70,6 +70,21 @@ def edge_pts(self, t):
70
70
right_pt = [pt [0 ] + vec_step [1 ], pt [1 ] - vec_step [0 ]]
71
71
return left_pt , right_pt
72
72
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
+
73
88
def _rect_corners (self , t1 , t2 , perc_width = 0.3 ):
74
89
""" Get two rectangles covering the expected edges of the cylinder
75
90
@param t1 starting t value
@@ -172,33 +187,30 @@ def _image_cutout(self, im, rect, step_size, height):
172
187
tform3_back = np .linalg .pinv (tform3 )
173
188
return cv2 .warpPerspective (im , tform3 , (step_size , height )), tform3_back
174
189
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 ):
176
191
""" Find which pixels are valid depth and fit average depth
177
192
@param im_depth - depth image
178
193
@param step_size how many pixels to move along the boundary
179
194
@param perc_width How much of the radius to move in/out of the edge
180
195
"""
181
196
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
202
214
203
215
def find_edges_hough_transform (self , im_edge , step_size = 40 , perc_width = 0.3 , axs = None ):
204
216
"""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=
222
234
line_b = np .zeros ((3 , 1 ))
223
235
line_b [2 , 0 ] = 1.0
224
236
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 )
232
238
233
239
im_warp , tform3_back = self ._image_cutout (im_edge , r , step_size = step_size , height = height )
234
240
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):
391
397
if axs is not None :
392
398
axs .imshow (im_mask , origin = 'lower' )
393
399
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 )
401
401
402
402
im_warp , tform_inv = self ._image_cutout (im_mask , r , step_size = step_size , height = height )
403
403
if b_rect_inside and np .sum (im_warp > 0 ) > 0 :
0 commit comments