mosta02 commited on
Commit
3a20d9c
·
verified ·
1 Parent(s): 2ed8a02

Upload centerline_extraction.py

Browse files
Files changed (1) hide show
  1. centerline_extraction.py +10 -8
centerline_extraction.py CHANGED
@@ -144,7 +144,7 @@ def compute_centerlines_network(
144
  # Extract main aorta→iliacs centerline via Voronoi method
145
  try:
146
  print("Extracting main centerline...")
147
- centerline_polydata = extract_centerline_from_surface(surface_polydata_ras, aorta_iliac_endpoints_ras, curve_sampling_distance, voronoi_simplify=False, smooth_iterations=20)
148
  print(f"Main centerline: {centerline_polydata.GetNumberOfCells()} cells, {centerline_polydata.GetNumberOfPoints()} points")
149
  except Exception as e:
150
  print(f"Main centerline extraction failed: {type(e).__name__}: {e}")
@@ -649,7 +649,7 @@ def decimate_surface_for_voronoi(surface_polydata, target_points, max_reduction=
649
  return result
650
 
651
  # Voronoi-based centerline extraction from aorta to iliacs to compute accurate centerline paths, transforms closed surface to centerline network
652
- def extract_centerline_from_surface(surface_polydata, endpoints, curve_sampling_distance, voronoi_simplify=False, smooth_iterations=40, smooth_relaxation=0.1):
653
  if not endpoints or len(endpoints) < 2:
654
  raise ValueError("At least two endpoints are needed for centerline extraction")
655
 
@@ -689,7 +689,7 @@ def extract_centerline_from_surface(surface_polydata, endpoints, curve_sampling_
689
  cl_filter.SetCostFunction("1/R")
690
  cl_filter.SetFlipNormals(False) # Controls whether the Voronoi diagram is built from the inward normals (flipped) or outward normals
691
  cl_filter.SetAppendEndPointsToCenterlines(0) # Path starts/ends at the nearest Voronoi node, which may be a fraction of a millimeter away from exact seed point
692
- cl_filter.SetSimplifyVoronoi(voronoi_simplify) # Voronoi smoothing disabled (slows up path finding but maintains narrow vessel sections)
693
  cl_filter.SetCenterlineResampling(1) # Enable resampling every 1mm, without this the raw Voronoi path is returned which contains thousands of points per segment
694
  cl_filter.SetResamplingStepLength(curve_sampling_distance) # Turns on resampler itself
695
  cl_filter.Update()
@@ -711,14 +711,16 @@ def extract_centerline_from_surface(surface_polydata, endpoints, curve_sampling_
711
 
712
  return centerline_polydata
713
 
714
- # Applies Laplacian smoothing to a centerline polydata while keeping endpoints fixed
715
  def smooth_centerline_polydata(centerline_polydata, iterations, relaxation_factor):
716
- smoother = vtk.vtkSmoothPolyDataFilter()
717
  smoother.SetInputData(centerline_polydata)
718
  smoother.SetNumberOfIterations(iterations)
719
- smoother.SetRelaxationFactor(relaxation_factor)
720
- smoother.BoundarySmoothingOff() # Keep boundary endpoints fixed (ree edges at the end of the centerline polyline)
721
  smoother.FeatureEdgeSmoothingOff()
 
 
722
  smoother.Update()
723
 
724
  smoothed = vtk.vtkPolyData()
@@ -770,7 +772,7 @@ def extract_branch_centerline(branch_info, main_centerline_polydata, surface_pol
770
 
771
  # Extract the branch centerline
772
  try:
773
- branch_centerline = extract_centerline_from_surface(clipped_surface, branch_endpoints, curve_sampling_distance, voronoi_simplify=True, smooth_iterations=10)
774
  return branch_centerline, contact_point
775
  except Exception as e:
776
  print(f"\nWarning: branch centerline extraction failed: {e}")
 
144
  # Extract main aorta→iliacs centerline via Voronoi method
145
  try:
146
  print("Extracting main centerline...")
147
+ centerline_polydata = extract_centerline_from_surface(surface_polydata_ras, aorta_iliac_endpoints_ras, curve_sampling_distance, smooth_iterations=20)
148
  print(f"Main centerline: {centerline_polydata.GetNumberOfCells()} cells, {centerline_polydata.GetNumberOfPoints()} points")
149
  except Exception as e:
150
  print(f"Main centerline extraction failed: {type(e).__name__}: {e}")
 
649
  return result
650
 
651
  # Voronoi-based centerline extraction from aorta to iliacs to compute accurate centerline paths, transforms closed surface to centerline network
652
+ def extract_centerline_from_surface(surface_polydata, endpoints, curve_sampling_distance, smooth_iterations=40, smooth_relaxation=0.1):
653
  if not endpoints or len(endpoints) < 2:
654
  raise ValueError("At least two endpoints are needed for centerline extraction")
655
 
 
689
  cl_filter.SetCostFunction("1/R")
690
  cl_filter.SetFlipNormals(False) # Controls whether the Voronoi diagram is built from the inward normals (flipped) or outward normals
691
  cl_filter.SetAppendEndPointsToCenterlines(0) # Path starts/ends at the nearest Voronoi node, which may be a fraction of a millimeter away from exact seed point
692
+ cl_filter.SetSimplifyVoronoi(False) # Voronoi smoothing disabled (slows up path finding but maintains narrow vessel sections)
693
  cl_filter.SetCenterlineResampling(1) # Enable resampling every 1mm, without this the raw Voronoi path is returned which contains thousands of points per segment
694
  cl_filter.SetResamplingStepLength(curve_sampling_distance) # Turns on resampler itself
695
  cl_filter.Update()
 
711
 
712
  return centerline_polydata
713
 
714
+ # Applies low pass smoothing filter to a centerline polydata while keeping endpoints fixed, it removes high-frequency zigzag noise while preserving vessel curvature
715
  def smooth_centerline_polydata(centerline_polydata, iterations, relaxation_factor):
716
+ smoother = vtk.vtkWindowedSincPolyDataFilter()
717
  smoother.SetInputData(centerline_polydata)
718
  smoother.SetNumberOfIterations(iterations)
719
+ smoother.SetPassBand(relaxation_factor)
720
+ smoother.BoundarySmoothingOff() # Keep both endpoints exactly fixed
721
  smoother.FeatureEdgeSmoothingOff()
722
+ smoother.NonManifoldSmoothingOn() # Required for polyline topology (not just surfaces)
723
+ smoother.NormalizeCoordinatesOn() # Numerical stability on long centerlines
724
  smoother.Update()
725
 
726
  smoothed = vtk.vtkPolyData()
 
772
 
773
  # Extract the branch centerline
774
  try:
775
+ branch_centerline = extract_centerline_from_surface(clipped_surface, branch_endpoints, curve_sampling_distance, smooth_iterations=20)
776
  return branch_centerline, contact_point
777
  except Exception as e:
778
  print(f"\nWarning: branch centerline extraction failed: {e}")