Skip to content

Commit

Permalink
Merge pull request #75 from JoHof/fix/postprocessing
Browse files Browse the repository at this point in the history
Fix/postprocessing
  • Loading branch information
JoHof committed Feb 9, 2023
2 parents c4bee46 + 75cd990 commit 0fe26b9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lungmask/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def apply(image, model=None, force_cpu=False, batch_size=20, volume_postprocessi
# postprocessing includes removal of small connected components, hole filling and mapping of small components to
# neighbors
if volume_postprocessing:
outmask = utils.postrocessing(timage_res)
outmask = utils.postprocessing(timage_res)
else:
outmask = timage_res

Expand Down Expand Up @@ -110,7 +110,7 @@ def get_model(modeltype, modelname, modelpath=None, n_classes=3):


def apply_fused(image, basemodel = 'LTRCLobes', fillmodel = 'R231', force_cpu=False, batch_size=20, volume_postprocessing=True, noHU=False):
'''Will apply basemodel and use fillmodel to mitiage false negatives'''
'''Will apply basemodel and use fillmodel to mitigate false negatives'''
mdl_r = get_model('unet',fillmodel)
mdl_l = get_model('unet',basemodel)
logging.info("Apply: %s" % basemodel)
Expand All @@ -121,4 +121,4 @@ def apply_fused(image, basemodel = 'LTRCLobes', fillmodel = 'R231', force_cpu=Fa
res_l[np.logical_and(res_l==0, res_r>0)] = spare_value
res_l[res_r==0] = 0
logging.info("Fusing results... this may take up to several minutes!")
return utils.postrocessing(res_l, spare=[spare_value])
return utils.postprocessing(res_l, spare=[spare_value])
11 changes: 7 additions & 4 deletions lungmask/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ def read_dicoms(path, primary=True, original=True):
# dcm_parameters = np.asarray(dcm_parameters)[sidx]
vol_unique = np.unique(conc, return_index=1, return_inverse=1) # unique volumes
n_vol = len(vol_unique[1])
logging.info('There are ' + str(n_vol) + ' volumes in the study')
if n_vol == 1:
logging.info('There is ' + str(n_vol) + ' volume in the study')
else:
logging.info('There are ' + str(n_vol) + ' volumes in the study')

relevant_series = []
relevant_volumes = []
Expand Down Expand Up @@ -191,7 +194,7 @@ def get_input_image(path):
return input_image


def postrocessing(label_image, spare=[]):
def postprocessing(label_image, spare=[]):
'''some post-processing mapping small label patches to the neighbout whith which they share the
largest border. All connected components smaller than min_area will be removed
'''
Expand Down Expand Up @@ -223,7 +226,7 @@ def postrocessing(label_image, spare=[]):
maxmap = 0
myarea = 0
for ix, n in enumerate(neighbours):
if n != 0 and n != r.label and counts[ix] > maxmap and n != spare:
if n != 0 and n != r.label and counts[ix] > maxmap and n not in spare:
maxmap = counts[ix]
mapto = n
myarea = r.area
Expand All @@ -235,7 +238,7 @@ def postrocessing(label_image, spare=[]):
regions[regionlabels.index(mapto)].__dict__['_cache']['area'] += myarea

outmask_mapped = region_to_lobemap[regionmask]
outmask_mapped[outmask_mapped==spare] = 0
outmask_mapped[np.isin(outmask_mapped, spare)] = 0

if outmask_mapped.shape[0] == 1:
# holefiller = lambda x: ndimage.morphology.binary_fill_holes(x[0])[None, :, :] # This is bad for slices that show the liver
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="lungmask",
version="0.2.12",
version="0.2.13",
author="Johannes Hofmanninger",
author_email="[email protected]",
description="Package for automated lung segmentation in CT",
Expand Down

0 comments on commit 0fe26b9

Please sign in to comment.