I have an image montage problem and I have been using flat fielding to fix the individual image tile before applying simple itk’s montage feature to receive the final resampled image. Btw I’m using simpleitk for the flat fielding process as well. Can someone help me out?
reference code
import os
import SimpleITK as sitk
def process_image(file_path, output_dir, shrink_factor=4):
raw_img_sitk = sitk.ReadImage(file_path, sitk.sitkFloat32)
raw_img_sitk_arr = sitk.GetArrayFromImage(raw_img_sitk)
transformed = sitk.RescaleIntensity(raw_img_sitk, 0, 255)
transformed = sitk.LiThreshold(transformed, 0, 1)
head_mask = transformed
input_image = sitk.Shrink(raw_img_sitk, [shrink_factor] * raw_img_sitk.GetDimension())
mask_image = sitk.Shrink(head_mask, [shrink_factor] * raw_img_sitk.GetDimension())
bias_corrector = sitk.N4BiasFieldCorrectionImageFilter()
corrected = bias_corrector.Execute(input_image, mask_image)
log_bias_field = bias_corrector.GetLogBiasFieldAsImage(raw_img_sitk)
corrected_image_full_resolution = raw_img_sitk / sitk.Exp(log_bias_field)
corrected_float = sitk.Cast(corrected_image_full_resolution, sitk.sitkFloat32)
output_path = os.path.join(output_dir, os.path.basename(file_path))
sitk.WriteImage(corrected_float, output_path)
def process_folder(input_folder, output_folder):
os.makedirs(output_folder, exist_ok=True)
for file_name in os.listdir(input_folder):
if file_name.lower().endswith(('.tif', '.tiff')):
file_path = os.path.join(input_folder, file_name)
process_image(file_path, output_folder)
Images before flat fielding being applied
Image after flat fielding applied