Skip to content

Commit d98b484

Browse files
committed
Temp check in
1 parent 3a10e6d commit d98b484

12 files changed

+3497
-3368
lines changed

Image_based/HandleFileNames.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,22 @@ def get_image_name(self, path, index, b_add_tag=True):
193193

194194
return im_name
195195

196+
def get_edge_image_name(self, path, index, b_add_tag=True):
197+
""" Get the image name corresponding to the index given by (subdirectory index, image index, -)
198+
@param path should be one of self.path, self.path_calculated, or path_debug
199+
@param index (tuple, either 2 dim or 3 dim, index into sorted lists)
200+
@param b_add_tag - add the image tag, y/n
201+
@return full image name with path"""
202+
203+
im_name = path
204+
if len(self.sub_dirs[index[0]]) > 0:
205+
im_name = im_name + self.sub_dirs[index[0]] + "/"
206+
im_name = im_name + self.image_names[index[0]][index[1]] + "_edge"
207+
if b_add_tag:
208+
im_name = im_name + self.image_tag
209+
210+
return im_name
211+
196212
def get_mask_name(self, path, index, b_add_tag=True):
197213
""" Get the mask name corresponding to the index given by (subdirectory index, image index, mask name, mask id)
198214
@param path should be one of self.path, self.path_calculated, or path_debug

Image_based/bezier_cyl_2d.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ def __init__(self, start_pt=None, end_pt=None, radius=1, mid_pt=None):
4747
else:
4848
self.p1 = np.array(mid_pt)
4949
self.start_radius = radius
50-
self.end_radius = 100
51-
#want to pass every single point from start to end and return radius along that path
50+
self.end_radius = radius
51+
5252
def radius(self, t):
53-
return (1-t)*(self.start_radius) + (t*self.end_radius)
54-
55-
53+
""" Radius is a linear interpolation of two end radii
54+
@param t - t between 0 and 1"""
55+
return (1 - t) * self.start_radius + t * self.end_radius
56+
5657
@staticmethod
5758
def _orientation(start_pt, end_pt):
5859
"""Set the orientation and ensure left-right or down-up
@@ -103,22 +104,22 @@ def edge_pts(self, t):
103104
right_pt = [pt[0] + vec_step[1], pt[1] - vec_step[0]]
104105
return left_pt, right_pt
105106

106-
def edge_offset_pt(self, t, perc_in_out, dir):
107+
def edge_offset_pt(self, t, perc_in_out, direction):
107108
""" Go in/out of the edge point a given percentage
108109
@param t - t value along the curve (in range 0, 1)
109110
@param perc_in_out - if 1, get point on edge. If 0.5, get halfway to centerline. If 2.0 get 2 width
110-
@param dir - 'Left' is the left direction, 'Right' is the right direction
111+
@param direction - 'Left' is the left direction, 'Right' is the right direction
111112
@return numpy array x,y """
112113
pt_edge = self.pt_axis(t)
113114
vec_tang = self.tangent_axis(t)
114115
vec_step = perc_in_out * self.radius(t) * vec_tang / np.sqrt(vec_tang[0] * vec_tang[0] + vec_tang[1] * vec_tang[1])
115116

116-
if dir == "Left":
117+
if direction == "Left":
117118
return np.array([pt_edge[0] + vec_step[1], pt_edge[1] - vec_step[0]])
118119
return np.array([pt_edge[0] - vec_step[1], pt_edge[1] + vec_step[0]])
119120

120121
@staticmethod
121-
def _rect_in_image(im, r, pad=2):
122+
def rect_in_image(im, r, pad=2):
122123
""" See if the rectangle is within the image boundaries
123124
@im - image (for width and height)
124125
@r - the rectangle
@@ -233,7 +234,7 @@ def interior_rects_mask(self, image_shape, step_size=40, perc_width=0.3):
233234
return ret_im_mask
234235

235236
@staticmethod
236-
def _image_cutout(im, rect, step_size, height):
237+
def image_cutout(im, rect, step_size, height):
237238
"""Cutout a warped bit of the image and return it
238239
@param im - the image rect is in
239240
@param rect - four corners of the rectangle to cut out
@@ -430,9 +431,9 @@ def make_mask_image(self, im_mask, step_size=20, perc_fuzzy=0.2):
430431
@param perc_fuzzy How much of the boundary to make fuzzy
431432
"""
432433
self.draw_interior_rects_filled(im_mask, b_solid=True,
433-
col_solid=(255, 255, 255),
434-
step_size=step_size,
435-
perc_width=1.0)
434+
col_solid=(255, 255, 255),
435+
step_size=step_size,
436+
perc_width=1.0)
436437
self.draw_boundary_rects_filled(im_mask, b_solid=True,
437438
col_solid=(128, 128, 128),
438439
step_size=step_size,

Image_based/extract_curves.py

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
# - Store as t, percentage in/out from radius (so we can re-use at different scales)
99

1010
import numpy as np
11-
from glob import glob
12-
import csv
1311
import cv2
1412
import json
1513
from os.path import exists
16-
from bezier_cyl_2d import BezierCyl2D
1714
from line_seg_2d import LineSeg2D
18-
from fit_bezier_cyl_2d_edge import FitBezierCyl2DEdge
1915
from HandleFileNames import HandleFileNames
16+
from fit_bezier_cyl_2d_edge import FitBezierCyl2DEdge
2017

2118

2219
class ExtractCurves:
@@ -53,7 +50,7 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, params=N
5350
if b_recalc or not fname_calculated or not exists(self.fname_full_edge_stats):
5451
# Recalculate and write
5552
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,
5754
self.params)
5855
# Write out the bezier curve
5956
if fname_calculated:
@@ -83,30 +80,34 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, params=N
8380

8481
if fname_debug:
8582
# 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)
8885
for pix in self.edge_stats["pixs_edge"]:
8986
im_covert_back[pix[0], pix[1], :] = (255, 0, 0)
9087
im_rgb[pix[0], pix[1], :] = (255, 255, 255)
9188

9289
for do_both_crv, do_both_name in [(self.left_curve, "Left"), (self.right_curve, "Right")]:
9390
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)
9693
LineSeg2D.draw_line(im_covert_back, pt1, pt2, color=(255, 255, 0))
9794
LineSeg2D.draw_line(im_rgb, pt1, pt2, color=(255, 255, 0))
9895
im_both = np.hstack([im_covert_back, im_rgb])
9996
cv2.imwrite(fname_debug, im_both)
10097

101-
@staticmethod def full_edge_stats(image_edge, bezier_edge, params):
98+
@staticmethod
99+
def full_edge_stats(image_edge, bezier_edge, params):
102100
""" Get the best pixel offset (if any) for each point along the edge
103101
@param image_edge - the edge image
104102
@param bezier_edge - the Bezier curve
105103
@param params - parameters for extraction"""
106104
bdry_rects1, ts1 = bezier_edge.boundary_rects(step_size=params["step_size"], perc_width=params["perc_width"])
107105
bdry_rects2, ts2 = bezier_edge.boundary_rects(step_size=params["step_size"], perc_width=params["perc_width"], offset=True)
108106
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
110111

111112
bdry_rects1.extend(bdry_rects2)
112113
ts1.extend(ts2)
@@ -120,22 +121,30 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, params=N
120121
for i_rect, r in enumerate(bdry_rects1):
121122
# b_rect_inside = BezierCyl2D._rect_in_image(image_edge, r, pad=2)
122123

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)
124125
# Actual hough transform on the cut-out image
125126
lines = cv2.HoughLines(im_warp, 1, np.pi / 180.0, 10)
126127

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
127135
i_side = i_rect % 2
128136

129137
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
131142

132-
if not lines:
133-
continue
134143
max_y = im_warp.max(axis=0)
135144

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))
137146
for i_col, y in enumerate(max_y):
138-
if max_y > params["edge_max"]:
147+
if y > params["edge_max"]:
139148
ids = np.where(im_warp[:, i_col] == y)
140149
if i_side == 0:
141150
tag = "left"
@@ -146,13 +155,17 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, params=N
146155
h_max = (1 + params["perc_width"]) * bezier_edge.radius(ts1[i_col])
147156
ret_stats[tag + "_perc"].append(h_min + h_max + ids[0] / width)
148157

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]))
150159
p1_back = tform3_back @ p1_in
151160

152161
ret_stats["pixs_edge"].append([p1_back[0], p1_back[1]])
153162

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)
156169
return ret_stats
157170

158171
@staticmethod
@@ -168,35 +181,37 @@ def curves_from_stats(stats_edge, params):
168181
for ts, ps in [(stats_edge["ts_left"], stats_edge["Left_perc"]), (stats_edge["ts_right"], stats_edge["Right_perc"])]:
169182
ps_filter = np.array(ps)
170183
for i_filter in range(0, params["n_filter"]):
171-
np.filter(ps_filter)
184+
# np.filter(ps_filter)
172185
ts_crvs = np.linspace(0, 1, params["n_samples"])
173186
ps_crvs = np.interp(ts_crvs, ts, ps_filter)
174187
crvs.append([(t, p) for t, p in zip(ts_crvs, ps_crvs)])
175188
return crvs[0], crvs[1]
176189

177190

178191
if __name__ == '__main__':
179-
#path_bpd = "./data/trunk_segmentation_names.json"
192+
# path_bpd = "./data/trunk_segmentation_names.json"
180193
path_bpd = "./data/forcindy_fnames.json"
181194
all_files = HandleFileNames.read_filenames(path_bpd)
182195

183196
b_do_debug = True
184-
b_do_recalc = False
197+
b_do_recalc = True
185198
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)
187199
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)
190203
if not b_do_debug:
191-
mask_fname_debug = ""
204+
ec_fname_debug = ""
192205
else:
193-
mask_fname_debug = mask_fname_debug + "_crv.png"
206+
ec_fname_debug = ec_fname_debug + "_extract_profile.png"
194207

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)
196209

197210
if not exists(mask_fname):
198211
raise ValueError(f"Error, file {mask_fname} does not exist")
199212
if not exists(rgb_fname):
200213
raise ValueError(f"Error, file {rgb_fname} does not exist")
201214

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

Comments
 (0)