Automate thresholding w. unknown values - Max Entropy (Python)

Hey there. I’m currently building a script to segment aneurysms and surrounding blood vessels from CT scans. I have successfully created a modular python script that completes the entire process from importing PNGs, applying filters, create segmentation editor and volume nodes, global thresholding, island effects, smoothing, saving of STL etc. However, I’m currently hard coding threshold min/max values in - see the function I created below.

def globalThresholding(segmentEditorWidget, minThresh, maxThresh):

  # Apply thresholding
  print("Applying Global Thresholding...")
  segmentEditorWidget.setActiveEffectByName("Threshold")   
  effect = segmentEditorWidget.activeEffect()
  effect.setParameter("MinimumThreshold", f"{minThresh}")
  effect.setParameter("MaximumThreshold", f"{maxThresh}")
  effect.self().onApply()
  print("Thresholding Complete")
  
  return 

I’ve found, that using the automatic thresholding method Maximum Entropy is sufficient for my needs. I’ve found a couple forum posts about how to implement this and have followed. However regardless of the method I use (yen/max entropy etc), it just doesn’t work and returns a solid cube as the segemented volume. This is the function I created.


def globalThresholdingMaxEntropy(segmentEditorWidget):
  # Apply thresholding
  print(“Applying Global Thresholding…”)
  segmentEditorWidget.setActiveEffectByName(“Threshold”)
  effect = segmentEditorWidget.activeEffect()
  
  # if effect is not None:
  # Set the thresholding method to Yen's method
  effect.setParameter("AutomaticThresholdMethod", "MaximumEntropy")
  effect.setParameter("ThresholdType", "Above")
  effect.self().onApply()
  print("Thresholding Complete using MaximumEntropy's method")
  
  return 

Could someone please help me figure out why this is the case and how I can fix this? Is it a bug?

Or, could someone suggest another method where the threshold values are unknown. I have considered using histograms of the pixel intensities but there doesn’t seem to be a sufficient pattern. Ideally, when I segment manually, I use local thresholding.

One issue that I see is that the name of the parameter is incorrect. It should be AutoThresholdMode.

I suggest looking at the code of the Threshold effect to find out more about the parameters it uses