Skip to content

Commit

Permalink
ENH: SimpleITK 2.4.0 migration changes for ObjectMorphology
Browse files Browse the repository at this point in the history
  • Loading branch information
hjmjohnson committed Nov 10, 2024
1 parent 3464508 commit 7c37d9e
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion AutoWorkup/BAW/fsscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def baw_fixed_brain_mask(
## some white matter is being classified as background when it it being avoid due
## to too strict of multi-modal thresholding.
hole_filled = sitk.ErodeObjectMorphology(
sitk.DilateObjectMorphology(clipping, fill_size), fill_size
sitk.DilateObjectMorphology(clipping, [fill_size] * 3), [fill_size] * 3
)
final_mask = sitk.Cast(hole_filled * not_blood, sitk.sitkUInt8)
## Now make an mgz version of the binary custom brain mask
Expand Down
11 changes: 4 additions & 7 deletions AutoWorkup/BAW/utilities/image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ def fill_hole_preserved_edge(inputMask, HOLE_FILL_SIZE):
:return:
"""

radius = [HOLE_FILL_SIZE] * inputMask.GetDimension()
filled_brain_mask = sitk.VotingBinaryHoleFilling(BM, radius)
eroded_brain_mask = sitk.ErodeObjectMorphology(filled_brain_mask, radius)
return sitk.BinaryThreshold(
inputMask
+ sitk.ErodeObjectMorphology(
sitk.VotingBinaryHoleFilling(
BM, [HOLE_FILL_SIZE, HOLE_FILL_SIZE, HOLE_FILL_SIZE]
),
HOLE_FILL_SIZE,
),
inputMask + eroded_brain_mask,
1,
10000,
)
Expand Down
4 changes: 2 additions & 2 deletions AutoWorkup/BAW/workflows/FixLabelMapsTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ def get_largest_label(inputMask, UseErosionCleaning):
"""
LargestComponentCode = 1
if UseErosionCleaning:
erosionMask = sitk.ErodeObjectMorphology(inputMask, 1)
erosionMask = sitk.ErodeObjectMorphology(inputMask, [1] * 3)
else:
erosionMask = inputMask
CC = sitk.ConnectedComponent(erosionMask)
Rlabel = sitk.RelabelComponent(CC)
largestMask = Rlabel == LargestComponentCode
if UseErosionCleaning:
dilateMask = sitk.DilateObjectMorphology(largestMask, 1)
dilateMask = sitk.DilateObjectMorphology(largestMask, [1] * 3)
else:
dilateMask = largestMask

Expand Down
4 changes: 2 additions & 2 deletions AutoWorkup/BAW/workflows/WorkupAddsonBrainStem.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ def crop_and_resample_in_place(
+ brainStemBinary * 30
)

errod_brain_mask = sitk.ErodeObjectMorphology((outputTissueLabel > 0), 2)
errod_brain_mask = sitk.ErodeObjectMorphology((outputTissueLabel > 0), [2] * 3)
LargestComponentCode = 1
one_region_mask = (
sitk.RelabelComponent(sitk.ConnectedComponent(errod_brain_mask))
== LargestComponentCode
)
dilate_one_region = sitk.DilateObjectMorphology(one_region_mask, 3) > 0
dilate_one_region = sitk.DilateObjectMorphology(one_region_mask, [3] * 3) > 0
cleanedOutputTissueLabel = outputTissueLabel * dilate_one_region

full_output_path = os.path.abspath(ouputTissuelLabelFilename)
Expand Down
4 changes: 2 additions & 2 deletions AutoWorkup/BAW/workflows/atlasNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ def create_atlas_xml_and_cleaned_deformed_averages(
print(f"MISSING FILE FROM patternDict: {base_name}")
## Make binary dilated mask
binmask = sitk.BinaryThreshold(load_images_list["AVG_BRAINMASK.nii.gz"], 1, 1000000)
brainmask_dilatedBy5 = sitk.DilateObjectMorphology(binmask, 5)
brainmask_dilatedBy5 = sitk.DilateObjectMorphology(binmask, [5] * 3)
brainmask_dilatedBy5 = sitk.Cast(
brainmask_dilatedBy5, sitk.sitkFloat32
) # Convert to Float32 for multiply

inv_brainmask_erodedBy5 = 1 - sitk.ErodeObjectMorphology(binmask, 5)
inv_brainmask_erodedBy5 = 1 - sitk.ErodeObjectMorphology(binmask, [5] * 3)
inv_brainmask_erodedBy5 = sitk.Cast(
inv_brainmask_erodedBy5, sitk.sitkFloat32
) # Convert to Float32 for multiply
Expand Down
8 changes: 4 additions & 4 deletions AutoWorkup/BAW/workflows/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ def get_largest_label(inputMask, UseErosionCleaning):
"""
LargestComponentCode = 1
if UseErosionCleaning:
erosionMask = sitk.ErodeObjectMorphology(inputMask, 1)
erosionMask = sitk.ErodeObjectMorphology(inputMask, [1] * 3)
else:
erosionMask = inputMask
CC = sitk.ConnectedComponent(erosionMask)
Rlabel = sitk.RelabelComponent(CC)
largestMask = Rlabel == LargestComponentCode
if UseErosionCleaning:
dilateMask = sitk.DilateObjectMorphology(largestMask, 1)
dilateMask = sitk.DilateObjectMorphology(largestMask, [1] * 3)
else:
dilateMask = largestMask

Expand All @@ -155,10 +155,10 @@ def get_largest_label(inputMask, UseErosionCleaning):
HDCMARegisteredVentricleLabels == HDMCARightVentricleCode
)
ExpandVentValue = 5
HDCMAMask_d5 = sitk.DilateObjectMorphology(HDCMAMask, ExpandVentValue)
HDCMAMask_d5 = sitk.DilateObjectMorphology(HDCMAMask, [ExpandVentValue] * 3)
CSFMaskImage = ABCLabelsImage == ABCCSFLabelCode
VentricleMask = (HDCMAMask_d5 * CSFMaskImage + HDCMAMask) > 0
VentricleMask_d2 = sitk.DilateObjectMorphology(VentricleMask, 2)
VentricleMask_d2 = sitk.DilateObjectMorphology(VentricleMask, [2] * 3)
ABCWMLabelCode = 1
WMMaskImage = ABCLabelsImage == ABCWMLabelCode

Expand Down
13 changes: 7 additions & 6 deletions AutoWorkup/logismosb/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ def fill_lateral_ventricle(
# Fill the ventricle region
for i in range(ventricleDilation):
ventTemp = (
sitk.DilateObjectMorphology(ventTemp, dilationFactor) * boundary
sitk.DilateObjectMorphology(ventTemp, [dilationFactor] * 3)
* boundary
)
ventTemp = largest_connected_component(ventTemp)
return ventTemp
Expand Down Expand Up @@ -420,7 +421,7 @@ def create_hemisphere_splits(right_template, left_template):
# dilate preserved regions
preservedDilation = 1
preservedRegions = sitk.DilateObjectMorphology(
preservedRegions, preservedDilation
preservedRegions, [preservedDilation] * 3
)
# add the whtie matter masks to the preserved regions
preservedRegions = preservedRegions + RightWhiteMatter + LeftWhiteMatter > 0
Expand All @@ -434,7 +435,7 @@ def create_hemisphere_splits(right_template, left_template):
# Remove mask around cerebellum and brain stem
cerebellumDilation = 1
cerebellumMaskDilated = sitk.DilateObjectMorphology(
CerebellumMask, cerebellumDilation
CerebellumMask, [cerebellumDilation] * 3
)
leftBoundaryMask = left_hemisphere * (1 - cerebellumMaskDilated)
leftBoundaryMask = largest_connected_component(leftBoundaryMask)
Expand All @@ -449,17 +450,17 @@ def create_hemisphere_splits(right_template, left_template):
# by eroding the brainlabels mask
brainlabelserosion = 1
brainlabelsmask = sitk.ErodeObjectMorphology(
brainlabelsImage > 0, brainlabelserosion
brainlabelsImage > 0, [brainlabelserosion] * 3
)
brainlabelsImage = (
sitk.Cast(brainlabelsmask, sitk.sitkUInt32) * brainlabelsImage
)
elif boundaryDilation > 0:
leftBoundaryMask = sitk.DilateObjectMorphology(
leftBoundaryMask, boundaryDilation
leftBoundaryMask, [boundaryDilation] * 3
)
rightBoundaryMask = sitk.DilateObjectMorphology(
rightBoundaryMask, boundaryDilation
rightBoundaryMask, [boundaryDilation] * 3
)

# Remove CSF from hemisphere masks
Expand Down
4 changes: 2 additions & 2 deletions AutoWorkup/logismosb/maclearn/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ def create_water_sheds(aseg_file, filled_file, dilation=6):
# dilate putamen/pallidum and assign label 12
rh_pitpul = (
(
sitk.DilateObjectMorphology((fs == 51) + (fs == 52), dilation)
sitk.DilateObjectMorphology((fs == 51) + (fs == 52), [dilation] * 3)
* (rh_hipthal == 0)
)
> 0
) * wm_labels[2]
lh_pitpul = (
(
sitk.DilateObjectMorphology((fs == 12) + (fs == 13), dilation)
sitk.DilateObjectMorphology((fs == 12) + (fs == 13), [dilation] * 3)
* (lh_hipthal == 0)
)
> 0
Expand Down

0 comments on commit 7c37d9e

Please sign in to comment.