Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshall #2

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions Image_based/branchpointdetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import json
from os.path import exists
from bezier_cyl_2d import BezierCyl2D
from line_seg_2d import draw_line, draw_box, draw_cross, LineSeg2D
from line_seg_2d import LineSeg2D
from scipy.cluster.vq import kmeans, whiten, vq

class BranchPointDetection:
Expand Down Expand Up @@ -67,18 +67,19 @@ def __init__(self, path, image_name, b_output_debug=True, b_recalc=False):

# For each of the trunk/branches, see if we have reasonable upper left/lower right points
# Save points in debug image

if b_output_debug:
self.images_single["marked points"] = np.copy(self.images_single["masked"])
for im in self.images:
try:
p1 = im["stats"]["lower_left"]
p2 = im["stats"]["upper_right"]
draw_line(im["image"], p1, p2, (128, 128, 128), 2)
draw_line(self.images_single["marked points"], p1, p2, (128, 128, 128), 1)
LineSeg2D.draw_line(im["image"], p1, p2, (128, 128, 128), 2)
LineSeg2D.draw_line(self.images_single["marked points"], p1, p2, (128, 128, 128), 1)

pc = im["stats"]["center"]
draw_cross(im["image"], pc, (128, 128, 128), 1, 2)
draw_cross(self.images_single["marked points"], pc, (180, 180, 128), 1, 3)
LineSeg2D.draw_cross(im["image"], pc, (128, 128, 128), 1, 2)
LineSeg2D.draw_cross(self.images_single["marked points"], pc, (180, 180, 128), 1, 3)

cv2.imwrite(self.path_debug + image_name + "_" + im["name"] + "_points.png", im["image"])
except:
Expand Down Expand Up @@ -111,8 +112,8 @@ def __init__(self, path, image_name, b_output_debug=True, b_recalc=False):
# Draw the original, the edges, and the depth mask with the fitted quad
im["quad"].draw_bezier(im_orig_debug)
if im["quad"].is_wire():
draw_cross(im_orig_debug, im["quad"].p0, (255, 0, 0), thickness=2, length=10)
draw_cross(im_orig_debug, im["quad"].p2, (255, 0, 0), thickness=2, length=10)
LineSeg2D.draw_cross(im_orig_debug, im["quad"].p0, (255, 0, 0), thickness=2, length=10)
LineSeg2D.draw_cross(im_orig_debug, im["quad"].p2, (255, 0, 0), thickness=2, length=10)
else:
im["quad"].draw_boundary(im_orig_debug, 10)
im["quad"].draw_edge_rects(im_covert_back, step_size=params["step_size"], perc_width=params["width"])
Expand Down Expand Up @@ -158,8 +159,8 @@ def __init__(self, path, image_name, b_output_debug=True, b_recalc=False):
# Draw the original, the edges, and the depth mask with the fitted quad
im["quad_flow"].draw_bezier(im_orig_debug)
if im["quad_flow"].is_wire():
draw_cross(im_orig_debug, im["quad_flow"].p0, (255, 0, 0), thickness=2, length=10)
draw_cross(im_orig_debug, im["quad_flow"].p2, (255, 0, 0), thickness=2, length=10)
LineSeg2D.draw_cross(im_orig_debug, im["quad_flow"].p0, (255, 0, 0), thickness=2, length=10)
LineSeg2D.draw_cross(im_orig_debug, im["quad_flow"].p2, (255, 0, 0), thickness=2, length=10)
else:
im["quad_flow"].draw_boundary(im_orig_debug, 10)
im["quad_flow"].draw_edge_rects(im_covert_back, step_size=params["step_size"], perc_width=params["width"])
Expand Down Expand Up @@ -202,8 +203,8 @@ def __init__(self, path, image_name, b_output_debug=True, b_recalc=False):

if b_output_debug:
for p, v in self.branch_points:
draw_box(self.images_single["marked points"], p, (254, 128, 254), 6)
draw_line(self.images_single["marked points"], p, p + v, (128, 254, 254), 1)
LineSeg2D.draw_box(self.images_single["marked points"], p, (254, 128, 254), 6)
LineSeg2D.draw_line(self.images_single["marked points"], p, p + v, (128, 254, 254), 1)

cv2.imwrite(self.path_debug + image_name + "_" + "_marked_joins_points.png", self.images_single["marked points"])

Expand Down Expand Up @@ -333,7 +334,7 @@ def fit_quad(self, im, b_output_debug=True):
quad = BezierCyl2D(pts['lower_left'], pts['upper_right'], 0.5 * pts['width'])

# Current parameters for the vertical leader
params = {"step_size": int(quad.radius_2d * 1.5), "width_mask": 1.4, "width": 0.3}
params = {"step_size": int(quad.radius * 1.5), "width_mask": 1.4, "width": 0.3}

# Iteratively move the quad to the center of the mask
for i in range(0, 5):
Expand Down Expand Up @@ -482,4 +483,4 @@ def find_branch_point(self, im_trunk, im_sidebranch):
for im_i in range(0, 18):
name = str(im_i)
print(name)
bp = BranchPointDetection(path, name, b_output_debug=True)
bp = BranchPointDetection(path, name, b_output_debug=False)
Binary file added __pycache__/bezier_cyl_3d.cpython-310.pyc
Binary file not shown.
18 changes: 11 additions & 7 deletions bezier_cyl_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import numpy as np
from json import load, dump


#switch to this
#use sdk--> look at point cloud
#take every 3 points and do bezier
class BezierCyl3D:

def __init__(self, p1=(0.0, 0.0, 0.0), p2=(0.5, 0.75, 0.5), p3=(1.0, 1.0, 1.0), start_radius=10.0, end_radius=20.0):
Expand Down Expand Up @@ -145,14 +147,15 @@ def frenet_frame(self, t):
def _calc_radii(self):
""" Calculate the radii along the branch
@return a numpy array of radii"""

radii = np.linspace(self.start_radii, self.end_radii, self.n_along)
return radii

def _calc_cyl_vertices(self):
"""Calculate the cylinder vertices"""
pt = np.ones(shape=(4,))
radii = self._calc_radii()

for it, t in enumerate(np.linspace(0, 1.0, self.n_along)):
mat = self.frenet_frame(t)
pt[0] = 0
Expand All @@ -165,6 +168,7 @@ def _calc_cyl_vertices(self):
pt_on_crv = mat @ pt

self.vertex_locs[it, itheta, :] = pt_on_crv[0:3].transpose()


def make_mesh(self):
""" Make a 3D generalized cylinder """
Expand Down Expand Up @@ -237,17 +241,17 @@ def read_json(fname, bezier_crv=None, compute_mesh=True):
from os.path import exists
from os import mkdir

if not exists("data/DebugImages"):
mkdir("data/DebugImages")
#if not exists("data/DebugImages"):
#mkdir("data/DebugImages")
branch = BezierCyl3D([506.5, 156.0, 0.0], [457.49999996771703, 478.9999900052037, 0.0], [521.5, 318.0, 0.0],
start_radius=10.5, end_radius=8.25)
branch.make_mesh()
branch.write_mesh("data/DebugImages/check_3d_bezier1.obj")
branch.write_mesh("check_3d_bezier1.obj")

branch = BezierCyl3D([-0.5, 0.0, 0.0], [0.0, 0.1, 0.05], [0.5, 0.0, 0.0], start_radius=0.5, end_radius=0.25)
branch.make_mesh()
branch.write_mesh("data/DebugImages/check_3d_bezier2.obj")
branch.write_mesh("check_3d_bezier2.obj")

branch.set_dims(n_along=30, n_radial=32)
branch.make_mesh()
branch.write_mesh("data/DebugImages/check_3d_bezier_more_vs.obj")
branch.write_mesh("check_3d_bezier_more_vs.obj")
Loading