Stacking Z dimension images using SimpleITK

You probably need to apply flat-fielding. C++ example is here. The key class for that, N4BiasFieldCorrectionImageFilter, is wrapped in Python. However, that is best done via hardware-specific way, e.g. acquire “dark” and “white” images and use them for flat-fielding.

I used a similar method like you suggested and it resulted in no improvement.

Code:

import SimpleITK as sitk #1

raw_img = "resampled_image.tif"

raw_img_sitk = sitk.ReadImage(raw_img, sitk.sitkFloat32) #2
raw_img_sitk_arr = sitk.GetArrayFromImage(raw_img_sitk) #3

transformed = sitk.RescaleIntensity(raw_img_sitk, 0, 255) #1
transformed = sitk.LiThreshold(transformed,0,1) #2
head_mask = transformed

shrinkFactor = 4 #1
inputImage = raw_img_sitk

inputImage = sitk.Shrink( raw_img_sitk, [ shrinkFactor ] * inputImage.GetDimension() ) #2
maskImage = sitk.Shrink( head_mask, [ shrinkFactor ] * inputImage.GetDimension() ) #3

bias_corrector = sitk.N4BiasFieldCorrectionImageFilter() #4

corrected = bias_corrector.Execute(inputImage, maskImage) #5

log_bias_field = bias_corrector.GetLogBiasFieldAsImage(raw_img_sitk) #1
corrected_image_full_resolution = raw_img_sitk / sitk.Exp( log_bias_field ) #2

out_path =  "field_corrected_image.tif"

# Cast the pixel type to float
corrected_float = sitk.Cast(corrected_image_full_resolution, sitk.sitkFloat32)

out_path = "corrected_resampled_image.tif"
sitk.WriteImage(corrected_float, out_path)

image

I was reading through Optical flow to fix “artifacts” that comes-up after tile merging and it looked nice. You have any thoughts or other algorithms to point to? Registration using optical flow — skimage 0.23.2 documentation @dzenanz

In my experience, N4BiasFieldCorrectionImageFilter needs a decent estimate of what is tissue and what is background in order to work well.

do you have any advise how to provide that decent estimate? @dzenanz

I ran into that with MRIs of the head. I had an iterative approach: threshold to get some segmentation of the head, apply bias field correction using that segmentation, then threshold the corrected image and iterate that a few times.

Your case looks harder, I don’t think this approach would work for you.

Hello @sleepingcat4,

This looks like vignetting. Possibly look at the PyBaSiC tool to deal with these artifacts in Python, also available as a Fiji plugin.

1 Like

I agree it looks like vignetting. This should be corrected on a per tile basis before composing the mosaic.

The contribution of vignetting to the image can be estimated from images of a constant or empty field. It may even be constant between images.

1 Like