Skip to content

Commit f4998a7

Browse files
committed
Fixing mask and bezier curve write
1 parent 5583c00 commit f4998a7

File tree

5 files changed

+134
-149
lines changed

5 files changed

+134
-149
lines changed

Image_based/BaseStatsImage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ def debug_image(self, in_image):
181181

182182

183183
if __name__ == '__main__':
184-
path_bpd = "./data/trunk_segmentation_names.json"
185-
#path_bpd = "./data/forcindy_fnames.json"
184+
#path_bpd = "./data/trunk_segmentation_names.json"
185+
path_bpd = "./data/forcindy_fnames.json"
186186
all_files = HandleFileNames.read_filenames(path_bpd)
187187

188188
b_do_debug = True

Image_based/bezier_cyl_2d.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, start_pt=None, end_pt=None, radius=1, mid_pt=None):
3434
@param radius - width in the image
3535
@param mid_pt - optional mid point (otherwise set to halfway between start_pt and end_pt)"""
3636

37-
if not start_pt or not end_pt:
37+
if start_pt is None or end_pt is None:
3838
self.p0 = np.array([0, 0])
3939
self.p2 = np.array([1, 0])
4040
self.orientation = "Horizontal"
@@ -441,8 +441,10 @@ def read_json(fname, bezier_crv=None):
441441
bezier_crv = BezierCyl2D([0, 0], [1, 1], 1)
442442
for k, v in my_data.items():
443443
try:
444-
if len(v) > 1:
444+
if len(v) == 2:
445445
setattr(bezier_crv, k, np.array(v))
446+
else:
447+
setattr(bezier_crv, k, v)
446448
except TypeError:
447449
setattr(bezier_crv, k, v)
448450

Image_based/fit_bezier_cyl_2d.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, bezier_crv_start=None):
2929
else:
3030
super(BezierCyl2D, self).__init__()
3131

32-
def _setup_least_squares(self, ts):
32+
def setup_least_squares(self, ts):
3333
"""Setup the least squares approximation - ts is the number of constraints to add, also
3434
puts in a copule constraints to keep end points where they are
3535
@param ts - t values to use
@@ -49,7 +49,7 @@ def _setup_least_squares(self, ts):
4949
b_rhs[i, :] = self.pt_axis(ts_constraints[i])
5050
return a_constraints, b_rhs
5151

52-
def _extract_least_squares(self, a_constraints, b_rhs):
52+
def extract_least_squares(self, a_constraints, b_rhs):
5353
""" Do the actual Ax = b and keep horizontal/vertical end points
5454
@param a_constraints the A of Ax = b
5555
@param b_rhs the b of Ax = b
@@ -91,15 +91,15 @@ def set_end_pts(self, pt0, pt2):
9191

9292
ts_mid = np.array([0.25, 0.75])
9393
# Set up the matrix - include the 3 current points plus the centers of the mask
94-
a_constraints, b_rhs = self._setup_least_squares(ts_mid)
94+
a_constraints, b_rhs = self.setup_least_squares(ts_mid)
9595
b_rhs[-3, :] = pt0.transpose()
9696
b_rhs[-2, :] = self.pt_axis(0.5 * (t0 + t2))
9797
b_rhs[-1, :] = pt2.transpose()
9898
for i, t in enumerate(ts_mid):
9999
t_map = (1-t) * t0 + t * t2
100100
b_rhs[i, :] = self.pt_axis(t_map)
101101

102-
return self._extract_least_squares(a_constraints, b_rhs)
102+
return self.extract_least_squares(a_constraints, b_rhs)
103103

104104

105105
if __name__ == '__main__':

Image_based/fit_bezier_cyl_2d_edge.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,77 @@ def debug_image_quad_fit(self, image_debug):
591591
cv2.imwrite('data/forcindy/0_quad.png', im_orig)
592592

593593
print("foo")
594+
595+
from branchpointdetection import BranchPointDetection
596+
597+
# Compute all the branch points/approximate lines for branches
598+
bp = BranchPointDetection("data/forcindy/", "0")
599+
600+
# Read in/compute the additional images we need for debugging
601+
# Original image, convert to canny edge
602+
# Mask image
603+
# Depth image
604+
im_orig = cv2.imread('data/forcindy/0.png')
605+
im_depth = cv2.imread('data/forcindy/0_depth.png')
606+
im_mask_color = cv2.imread('data/forcindy/0_trunk_0.png')
607+
im_mask = cv2.cvtColor(im_mask_color, cv2.COLOR_BGR2GRAY)
608+
im_gray = cv2.cvtColor(im_orig, cv2.COLOR_BGR2GRAY)
609+
im_edge = cv2.Canny(im_gray, 50, 150, apertureSize=3)
610+
im_depth_color = cv2.cvtColor(im_depth, cv2.COLOR_BGR2RGB)
611+
im_covert_back = cv2.cvtColor(im_edge, cv2.COLOR_GRAY2RGB)
612+
613+
# Write out edge image
614+
cv2.imwrite('data/forcindy/0_edges.png', im_edge)
615+
616+
# For the vertical leader...
617+
trunk_pts = bp.trunks[0]["stats"]
618+
# Fit a quad to the trunk
619+
quad = BezierCyl2D(trunk_pts['lower_left'], trunk_pts['upper_right'], 0.5 * trunk_pts['width'])
620+
621+
# Current parameters for the vertical leader
622+
step_size_to_use = int(quad.radius_2d * 1.5) # Go along the edge at 1.5 * the estimated radius
623+
perc_width_to_use = 0.3 # How "fat" to make the edge rectangles
624+
perc_width_to_use_mask = 1.4 # How "fat" a rectangle to cover the mask
625+
626+
# Debugging image - draw the interior rects
627+
quad.draw_interior_rects(im_mask_color, step_size=step_size_to_use, perc_width=perc_width_to_use_mask)
628+
cv2.imwrite('data/forcindy/0_mask.png', im_mask_color)
629+
630+
# For debugging images
631+
fig, axs = plt.subplots(2, 2)
632+
axs[0, 0].imshow(im_orig)
633+
axs[0, 1].imshow(im_mask_color)
634+
plt.tight_layout()
635+
636+
# Iteratively move the quad to the center of the mask
637+
for i in range(0, 5):
638+
res = quad.adjust_quad_by_mask(im_mask,
639+
step_size=step_size_to_use, perc_width=perc_width_to_use_mask,
640+
axs=axs[1, 0])
641+
print(f"Res {res}")
642+
643+
# Draw the original, the edges, and the depth mask with the fitted quad
644+
quad.draw_bezier(im_orig)
645+
quad.draw_boundary(im_orig, 10)
646+
quad.draw_bezier(im_covert_back)
647+
648+
quad.draw_edge_rects(im_orig, step_size=step_size_to_use, perc_width=perc_width_to_use)
649+
quad.draw_edge_rects(im_covert_back, step_size=step_size_to_use, perc_width=perc_width_to_use)
650+
#quad.draw_edge_rects_markers(im_edge, step_size=step_size_to_use, perc_width=perc_width_to_use)
651+
quad.draw_interior_rects(im_depth_color, step_size=step_size_to_use, perc_width=perc_width_to_use)
652+
653+
im_both = np.hstack([im_orig, im_covert_back, im_depth_color])
654+
cv2.imshow("Original and edge and depth", im_both)
655+
cv2.imwrite('data/forcindy/0_rects.png', im_both)
656+
657+
# Now do the hough transform - first draw the hough transform edges
658+
for i in range(0, 5):
659+
ret = quad.adjust_quad_by_hough_edges(im_edge, step_size=step_size_to_use, perc_width=perc_width_to_use, axs=axs[1, 1])
660+
print(f"Res Hough {ret}")
661+
662+
im_orig = cv2.imread('data/forcindy/0.png')
663+
quad.draw_bezier(im_orig)
664+
quad.draw_boundary(im_orig, 10)
665+
cv2.imwrite('data/forcindy/0_quad.png', im_orig)
666+
667+
print("foo")

0 commit comments

Comments
 (0)