8
8
# - Store as t, percentage in/out from radius (so we can re-use at different scales)
9
9
10
10
import numpy as np
11
- from glob import glob
12
- import csv
13
11
import cv2
14
12
import json
15
13
from os .path import exists
16
- from bezier_cyl_2d import BezierCyl2D
17
14
from line_seg_2d import LineSeg2D
18
- from fit_bezier_cyl_2d_edge import FitBezierCyl2DEdge
19
15
from HandleFileNames import HandleFileNames
16
+ from fit_bezier_cyl_2d_edge import FitBezierCyl2DEdge
20
17
21
18
22
19
class ExtractCurves :
@@ -53,7 +50,7 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, params=N
53
50
if b_recalc or not fname_calculated or not exists (self .fname_full_edge_stats ):
54
51
# Recalculate and write
55
52
self .edge_stats = ExtractCurves .full_edge_stats (self .bezier_edge .image_edge ,
56
- self .bezier_edge .fit_bezier_edge ,
53
+ self .bezier_edge .bezier_crv_fit_to_edge ,
57
54
self .params )
58
55
# Write out the bezier curve
59
56
if fname_calculated :
@@ -83,30 +80,34 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, params=N
83
80
84
81
if fname_debug :
85
82
# Draw the mask with the initial and fitted curve
86
- im_covert_back = cv2 .cvtColor (self .bezier_edge .edge_image , cv2 .COLOR_GRAY2RGB )
87
- im_rgb = cv2 . copyTo (self .bezier_edge .rgb_image )
83
+ im_covert_back = cv2 .cvtColor (self .bezier_edge .image_edge , cv2 .COLOR_GRAY2RGB )
84
+ im_rgb = np . copy (self .bezier_edge .image_rgb )
88
85
for pix in self .edge_stats ["pixs_edge" ]:
89
86
im_covert_back [pix [0 ], pix [1 ], :] = (255 , 0 , 0 )
90
87
im_rgb [pix [0 ], pix [1 ], :] = (255 , 255 , 255 )
91
88
92
89
for do_both_crv , do_both_name in [(self .left_curve , "Left" ), (self .right_curve , "Right" )]:
93
90
for pt_e1 , pt_e2 in zip (do_both_crv [0 :- 1 ], do_both_crv [1 :]):
94
- pt1 = self .bezier_edge .bezier_fit .edge_offset_pt (pt_e1 [0 ], pt_e1 [1 ], do_both_name )
95
- pt2 = self .bezier_edge .bezier_fit .edge_offset_pt (pt_e2 [0 ], pt_e2 [1 ], do_both_name )
91
+ pt1 = self .bezier_edge .bezier_crv_fit_to_edge .edge_offset_pt (pt_e1 [0 ], pt_e1 [1 ], do_both_name )
92
+ pt2 = self .bezier_edge .bezier_crv_fit_to_edge .edge_offset_pt (pt_e2 [0 ], pt_e2 [1 ], do_both_name )
96
93
LineSeg2D .draw_line (im_covert_back , pt1 , pt2 , color = (255 , 255 , 0 ))
97
94
LineSeg2D .draw_line (im_rgb , pt1 , pt2 , color = (255 , 255 , 0 ))
98
95
im_both = np .hstack ([im_covert_back , im_rgb ])
99
96
cv2 .imwrite (fname_debug , im_both )
100
97
101
- @staticmethod def full_edge_stats (image_edge , bezier_edge , params ):
98
+ @staticmethod
99
+ def full_edge_stats (image_edge , bezier_edge , params ):
102
100
""" Get the best pixel offset (if any) for each point along the edge
103
101
@param image_edge - the edge image
104
102
@param bezier_edge - the Bezier curve
105
103
@param params - parameters for extraction"""
106
104
bdry_rects1 , ts1 = bezier_edge .boundary_rects (step_size = params ["step_size" ], perc_width = params ["perc_width" ])
107
105
bdry_rects2 , ts2 = bezier_edge .boundary_rects (step_size = params ["step_size" ], perc_width = params ["perc_width" ], offset = True )
108
106
n_bdry1 = len (bdry_rects1 )
109
- t_step = (ts1 [- 1 ] - ts1 [0 ]) / (len (bdry_rects1 ) // 2 )
107
+ try :
108
+ t_step = ts1 [2 ] - ts1 [0 ]
109
+ except IndexError :
110
+ t_step = 1.0
110
111
111
112
bdry_rects1 .extend (bdry_rects2 )
112
113
ts1 .extend (ts2 )
@@ -120,22 +121,30 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, params=N
120
121
for i_rect , r in enumerate (bdry_rects1 ):
121
122
# b_rect_inside = BezierCyl2D._rect_in_image(image_edge, r, pad=2)
122
123
123
- im_warp , tform3_back = bezier_edge ._image_cutout (image_edge , r , step_size = width , height = height )
124
+ im_warp , tform3_back = bezier_edge .image_cutout (image_edge , r , step_size = width , height = height )
124
125
# Actual hough transform on the cut-out image
125
126
lines = cv2 .HoughLines (im_warp , 1 , np .pi / 180.0 , 10 )
126
127
128
+ # Check for any lines in the cutout image
129
+ if lines is None :
130
+ continue
131
+ # .. and check if any of those are horizontal
132
+ ret_pts = FitBezierCyl2DEdge .get_horizontal_lines_from_hough (lines , tform3_back , width , height )
133
+ if ret_pts is []:
134
+ continue
127
135
i_side = i_rect % 2
128
136
129
137
if i_rect == n_bdry1 :
130
- t_step = (ts2 [- 1 ] - ts2 [0 ]) / (len (bdry_rects2 ) // 2 )
138
+ try :
139
+ t_step = ts2 [2 ] - ts2 [0 ]
140
+ except IndexError :
141
+ t_step = 1.0
131
142
132
- if not lines :
133
- continue
134
143
max_y = im_warp .max (axis = 0 )
135
144
136
- ts_seg = np .linspace (ts1 [i_rect ] - t_step * 0.5 , ts1 [i_rect ] - t_step * 0.5 , len (max_y ))
145
+ ts_seg = np .linspace (ts1 [i_rect ] - t_step * 0.5 , ts1 [i_rect ] + t_step * 0.5 , len (max_y ))
137
146
for i_col , y in enumerate (max_y ):
138
- if max_y > params ["edge_max" ]:
147
+ if y > params ["edge_max" ]:
139
148
ids = np .where (im_warp [:, i_col ] == y )
140
149
if i_side == 0 :
141
150
tag = "left"
@@ -146,13 +155,17 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, params=N
146
155
h_max = (1 + params ["perc_width" ]) * bezier_edge .radius (ts1 [i_col ])
147
156
ret_stats [tag + "_perc" ].append (h_min + h_max + ids [0 ] / width )
148
157
149
- p1_in = np .transpose (np .array ([ids [0 ], i_col , 1.0 ]))
158
+ p1_in = np .transpose (np .array ([ids [0 ][ 0 ] , i_col , 1.0 ]))
150
159
p1_back = tform3_back @ p1_in
151
160
152
161
ret_stats ["pixs_edge" ].append ([p1_back [0 ], p1_back [1 ]])
153
162
154
- sort (zip (ret_stats ["ts_left" ], ret_stats ["left_perc" ]), key = 0 )
155
- sort (zip (ret_stats ["ts_right" ], ret_stats ["right" ]), key = 0 )
163
+ np .array ([ret_stats ["ts_left" ], ret_stats ["left_perc" ]])
164
+ np .sort (axis = 0 )
165
+ ret_stats ["ts_left" ] = list (np .array [0 , :])
166
+ ret_stats ["left_perc" ] = list (np .array [1 , :])
167
+ # sort(zip(ret_stats["ts_left"], ret_stats["left_perc"]), key=0)
168
+ # sort(zip(ret_stats["ts_right"], ret_stats["right"]), key=0)
156
169
return ret_stats
157
170
158
171
@staticmethod
@@ -168,35 +181,37 @@ def curves_from_stats(stats_edge, params):
168
181
for ts , ps in [(stats_edge ["ts_left" ], stats_edge ["Left_perc" ]), (stats_edge ["ts_right" ], stats_edge ["Right_perc" ])]:
169
182
ps_filter = np .array (ps )
170
183
for i_filter in range (0 , params ["n_filter" ]):
171
- np .filter (ps_filter )
184
+ # np.filter(ps_filter)
172
185
ts_crvs = np .linspace (0 , 1 , params ["n_samples" ])
173
186
ps_crvs = np .interp (ts_crvs , ts , ps_filter )
174
187
crvs .append ([(t , p ) for t , p in zip (ts_crvs , ps_crvs )])
175
188
return crvs [0 ], crvs [1 ]
176
189
177
190
178
191
if __name__ == '__main__' :
179
- #path_bpd = "./data/trunk_segmentation_names.json"
192
+ # path_bpd = "./data/trunk_segmentation_names.json"
180
193
path_bpd = "./data/forcindy_fnames.json"
181
194
all_files = HandleFileNames .read_filenames (path_bpd )
182
195
183
196
b_do_debug = True
184
- b_do_recalc = False
197
+ b_do_recalc = True
185
198
for ind in all_files .loop_masks ():
186
- mask_fname = all_files .get_mask_name (path = all_files .path , index = ind , b_add_tag = True )
187
199
rgb_fname = all_files .get_image_name (path = all_files .path , index = ind , b_add_tag = True )
188
- edge_fname = all_files .get_image_name (path = all_files .path_calculated , index = ind , b_add_tag = False ) + "_edge.png"
189
- mask_fname_debug = all_files .get_mask_name (path = all_files .path_debug , index = ind , b_add_tag = False )
200
+ edge_fname = all_files .get_edge_image_name (path = all_files .path_calculated , index = ind , b_add_tag = True )
201
+ mask_fname = all_files .get_mask_name (path = all_files .path , index = ind , b_add_tag = True )
202
+ ec_fname_debug = all_files .get_mask_name (path = all_files .path_debug , index = ind , b_add_tag = False )
190
203
if not b_do_debug :
191
- mask_fname_debug = ""
204
+ ec_fname_debug = ""
192
205
else :
193
- mask_fname_debug = mask_fname_debug + "_crv .png"
206
+ ec_fname_debug = ec_fname_debug + "_extract_profile .png"
194
207
195
- mask_fname_calculate = all_files .get_mask_name (path = all_files .path_calculated , index = ind , b_add_tag = False )
208
+ ec_fname_calculate = all_files .get_mask_name (path = all_files .path_calculated , index = ind , b_add_tag = False )
196
209
197
210
if not exists (mask_fname ):
198
211
raise ValueError (f"Error, file { mask_fname } does not exist" )
199
212
if not exists (rgb_fname ):
200
213
raise ValueError (f"Error, file { rgb_fname } does not exist" )
201
214
202
- b_stats = ExtractCurves (rgb_fname , edge_fname , mask_fname , mask_fname_calculate , mask_fname_debug , b_recalc = b_do_recalc )
215
+ profile_crvs = ExtractCurves (rgb_fname , edge_fname , mask_fname ,
216
+ fname_calculated = ec_fname_calculate ,
217
+ fname_debug = ec_fname_debug , b_recalc = b_do_recalc )
0 commit comments