Estimate displacement field that aligns two 2-D or 3-D images
estimates the displacement field
D that aligns the image to be
moving, with the reference image,
fixed. The displacement vectors at each pixel location map locations
fixed image grid to a corresponding location in the
moving_reg is a warped version of
moving image that is warped according to the displacement field
D and resampled using linear interpolation.
Register Two Images with Local Distortions
This example shows how to solve a registration problem in which the same hand has been photographed in two different poses. The misalignment of the images varies locally throughout each image. This is therefore a non-rigid registration problem.
Read the two images into the workspace.
fixed = imread('hands1.jpg'); moving = imread('hands2.jpg');
Convert the images to grayscale for processing.
fixed = im2gray(fixed); moving = im2gray(moving);
Observe the initial misalignment. Fingers are in different poses.
Overlay the two images to make it easy to see where the images differ. The differences are highlighted in green and magenta.
Correct illumination differences between the
fixed images using histogram matching. This is a common pre-processing step.
moving = imhistmatch(moving,fixed);
Estimate the transformation needed to bring the two images into alignment.
[~,movingReg] = imregdemons(moving,fixed,[500 400 200],... 'AccumulatedFieldSmoothing',1.3);
Display the results of the registration. In the first figure, the images are overlaid to show the alignment.
moving — Image to be registered
2-D grayscale image | 3-D grayscale image
Image to be registered, specified as a 2-D or 3-D grayscale image.
fixed — Reference image in the target orientation
3-D grayscale image | 2-D grayscale image
Reference image in the target orientation, specified as a 2-D or 3-D grayscale image.
N — Number of iterations
100 (default) | positive integer scalar or vector
Number of iterations, specified as a positive integer scalar or vector.
When you specify a vector,
N is the number of iterations per pyramid
level (resolution level). For example, if there are 3 pyramid levels, then you can
specify the vector
imregdemons performs 100 iterations at the lowest resolution
level, 50 iterations at the next pyramid level, and 25 iterations at the last iteration
level — the level with full resolution. Because it takes less time to process the
lower resolution levels, running more iterations at low resolution and fewer iterations
at the higher resolutions of the pyramid can help performance.
Specify optional pairs of arguments as
the argument name and
Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
'AccumulatedFieldSmoothing',1.5 applies Gaussian smoothing with a
standard deviation of 1.5 at each iteration
AccumulatedFieldSmoothing — Smoothing applied at each iteration
1.0 (default) | positive scalar
Smoothing applied at each iteration, specified as the comma-separated pair consisting of
'AccumulatedFieldSmoothing' and a numeric value. This parameter
controls the amount of diffusion-like regularization.
applies the standard deviation of the Gaussian smoothing to regularize the accumulated
field at each iteration. Larger values result in smoother output displacement fields.
Smaller values result in more localized deformation in the output displacement field.
Values typically are in the range [0.5, 3.0]. When you specify multiple
PyramidLevels, the standard deviation used in the Gaussian
smoothing remains the same at each pyramid level.
PyramidLevels — Number of multi-resolution image pyramid levels to use
3 (default) | positive integer
Number of multi-resolution image pyramid levels to use, specified as the comma-separated pair
'PyramidLevels' and a positive integer.
DisplayWaitbar — Display wait bar to indicate progress
true (default) |
Display wait bar to indicate progress, specified as the comma-separated pair
'DisplayWaitbar' and the value
false. When set to
imregdemons displays a wait bar to
indicate progress for long-running operations. To prevent
imregdemons from displaying a wait bar, set
D — Displacement field
Displacement field, specified as a numeric array. Displacement values are in units of pixels.
fixedis a 2-D grayscale image of size m-by-n, then the displacement field array is m-by-n-by-2.
D(:,:,1)contains displacements along the x-axis and
D(:,:,2)contains displacements along the y-axis.
fixedis a 3-D grayscale image of size m-by-n-by-p, then the displacement field array is m-by-n-by-p-by-3.
D(:,:,:,1)contains displacements along the x-axis,
D(:,:,:,2)contains displacements along the y-axis, and
D(:,:,:,3)contains displacements along the z-axis.
moving_reg — Aligned image
2-D or 3-D grayscale image
Registered image, returned as a 2-D or 3-D grayscale image. The image is warped according to
the displacement field
D and resampled using linear
 Thirion, J.-P. "Image matching as a diffusion process: an analogy with Maxwell’s demons". Medical Image Analysis. Vol. 2, Number 3, 1998, pp. 243–260.
 Vercauteren, T., X. Pennec, A. Perchant, N. Ayache, "Diffeomorphic Demons: Efficient Non-parametric Image Registration", NeuroImage. Vol. 45, Number 1, Supplement 1, March 2009, pp. 61–72.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
DisplayWaitbarname-value pair argument is not supported on the GPU.
For more information, see Image Processing on a GPU.