Skip to content

Commit

Permalink
hotfix on draw_contours (convexHull only when contour is not convex)
Browse files Browse the repository at this point in the history
  • Loading branch information
simoneponcioni committed Jan 30, 2024
1 parent f553ec6 commit 0fefbaa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
34 changes: 26 additions & 8 deletions src/pyhexspline/spline_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,41 @@ def draw_contours(self, img, loc=str("outer")):
out = np.zeros(np.shape(img), dtype=np.uint8)
for c in _contours:
if not cv2.isContourConvex(c):
c = cv2.convexHull(c)
cv2.drawContours(out, [c], -1, 1, 1)
contour = out
self.logger.warning("Outer contour is not convex (catch 1)")
hull_list = [cv2.convexHull(c) for c in _contours]
contour = cv2.drawContours(out, hull_list, -1, 1, 1)
else:
contour = cv2.drawContours(out, _contours, -1, 1, 1)
elif loc == "inner":
_contours, hierarchy = cv2.findContours(
img.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
inn = np.zeros(np.shape(img), dtype=np.uint8)
for c in _contours:
if not cv2.isContourConvex(c):
c = cv2.convexHull(c)
cv2.drawContours(inn, [c], -1, 1, 1)
contour = inn
# if not cv2.isContourConvex(c):
# self.logger.warning("Inner contour is not convex (catch 2)")
# hull_list = [cv2.convexHull(c) for c in _contours]
# contour = cv2.drawContours(inn, hull_list, -1, 1, 1)
# * New, added by POS on 24.01.2024/30.01.2024
if len(_contours) > 2:
if not cv2.isContourConvex(c):
self.logger.warning("Inner contour is not convex (catch 3)")
hull_list = [cv2.convexHull(c) for c in _contours]
contour = cv2.drawContours(inn, hull_list, 2, 1, 1)
else:
contour = cv2.drawContours(inn, _contours, 2, 1, 1)
else:
if not cv2.isContourConvex(c):
self.logger.warning("Inner contour is not convex (catch 4)")
hull_list = [cv2.convexHull(c) for c in _contours]
contour = cv2.drawContours(inn, hull_list, 1, 1, 1)
else:
contour = cv2.drawContours(inn, _contours, 1, 1, 1)
else:
raise ValueError(
"The location of the contour is not valid. Please choose between 'outer' and 'inner'."
"The location of the contour is not valid. Please choose between 'outer' or 'inner'."
)
contour = contour.astype(np.uint8)
return contour

def get_binary_contour(self, image):
Expand Down
2 changes: 1 addition & 1 deletion standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def main():
"n_elms_transverse_cort": 3, # number of elements in the transverse direction for the cortical compartment
"n_elms_radial": 15, # number of elements in the radial direction # ! Should be 10 if trab_refinement is True
"ellipsoid_fitting": True, # True: perform ellipsoid fitting
"show_plots": True, # show plots during construction
"show_plots": False, # show plots during construction
"show_gmsh": True, # show gmsh GUI
"write_mesh": True, # write mesh to file
"trab_refinement": False, # True: refine trabecular mesh at the center
Expand Down

0 comments on commit 0fefbaa

Please sign in to comment.