I used SimpleITK to do affine registration and find that after transform the moving image was scaled smaller than its original size while the edge was padded with gray color. How to pad the edge with black color (0 value) instead?
The output I got: Moved Image The output I want: Expected Moved Image
# Read moving and fixed images
moving_path = os.path.join(DATA_DIR, 'image/original', df['MovingFile'][i])
fixed_path = os.path.join(DATA_DIR, 'image/original', df['FixedFile'][i])
moving = sitk.ReadImage(moving_path, sitk.sitkFloat32, imageIO='PNGImageIO')
fixed = sitk.ReadImage(fixed_path, sitk.sitkFloat32, imageIO='PNGImageIO')
# The affine registration
initialTx = sitk.CenteredTransformInitializer(fixed, moving, sitk.AffineTransform(fixed.GetDimension()))
R = sitk.ImageRegistrationMethod()
shrinkFactors = [3, 2, 1]
smoothingSigmas = [2.0, 1.0, 1.0]
R.SetShrinkFactorsPerLevel(shrinkFactors)
R.SetSmoothingSigmasPerLevel(smoothingSigmas)
R.SetMetricAsJointHistogramMutualInformation(20)
R.MetricUseFixedImageGradientFilterOff()
lr = 1.0
iterations = 100
min_converge = 1e-6
window_size = 10
estimate_lr = R.EachIteration
R.SetOptimizerAsGradientDescent(lr, iterations, min_converge, window_size, estimate_lr)
R.SetOptimizerScalesFromIndexShift()
R.SetInitialTransform(initialTx)
R.SetInterpolator(sitk.sitkLinear)
outTx = R.Execute(fixed, moving)
# Save transform matrix
pair_name = '{}_{}_{}-{}'.format(
df['PatientID'][i], df['Time'][i], re.split('[-_.]', df['MovingFile'][i])[6], re.split('[-_.]', df['FixedFile'][i])[6])
sitk.WriteTransform(outTx, '{}/matric/{}/{}.mat'.format(DATA_DIR, METHOD, pair_name))
# Resampling
resampler = sitk.ResampleImageFilter()
resampler.SetReferenceImage(fixed)
resampler.SetInterpolator(sitk.sitkLinear)
resampler.SetDefaultPixelValue(100)
resampler.SetTransform(outTx)
# Transform
out = resampler.Execute(moving)
simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
cimg = sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
# Save moved and diff image
moved_path = '{}/image/moved/{}/moved_{}.png'.format(DATA_DIR, METHOD, pair_name)
sitk.WriteImage(cimg, '{}/image/diff/{}/diff_{}.png'.format(DATA_DIR, METHOD, pair_name), imageIO='PNGImageIO')
sitk.WriteImage(simg2, moved_path, imageIO='PNGImageIO')
the output I got: Moved Image The output I want: Expected Moved Image
You have the default pixel value set to 100 in the resampled. That's your grey. If you set it to 0, you'll get the black background you want.