Image registration: Difference between revisions
(Created page with "'''Image registration''' is the process of spatially aligning images in a dataset. Registration is often used to correct for drift or motion within an imaging dataset. Registration can also be used to align images taken from different viewpoints or by different equipment (e.g., aligning EM images with optical images). === Strategies to correct image drift === Drift in an image can be caused by different issues, including thermal changes (which causes expansion/contracti...") |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 19: | Line 19: | ||
=== Example code === | === Example code === | ||
Example of an implementation in MATLAB:<syntaxhighlight lang="matlab"> | Example of an implementation in MATLAB: | ||
<syntaxhighlight lang="matlab"> | |||
function pxShift = xcorrreg(refImg, movedImg) | function pxShift = xcorrreg(refImg, movedImg) | ||
%REGISTERIMG Register two images using cross-correlation | %REGISTERIMG Register two images using cross-correlation | ||
Line 27: | Line 29: | ||
% and M is the moved image. The output P will be a 1-by-2 vector with the | % and M is the moved image. The output P will be a 1-by-2 vector with the | ||
% [x, y] offsets. | % [x, y] offsets. | ||
% | |||
% The resulting image can then be corrected, e.g., by using the circshift | |||
% function Mcorr = circshift(M, P). | |||
% | % | ||
% Note: This algorithm only works for translational shifts, and will not | % Note: This algorithm only works for translational shifts, and will not |
Latest revision as of 19:24, 14 September 2023
Image registration is the process of spatially aligning images in a dataset. Registration is often used to correct for drift or motion within an imaging dataset. Registration can also be used to align images taken from different viewpoints or by different equipment (e.g., aligning EM images with optical images).
Strategies to correct image drift
Drift in an image can be caused by different issues, including thermal changes (which causes expansion/contraction of the sample) or due to mounting (e.g., if the imaged objects are free-floating). This typically results in a global translation of the image.
Cross-correlation
The simplest method to correct for image drift is to use a cross-correlation measurement. This method works best for fluorescence images or other other images where there are easy features that are recognizable in the image. This method is also often referred to as template matching.
Assume that we have a reference image and a moving image. To register the two images, one can calculate the cross-correlation. To calculate a cross-correlation, the two images are displaced from each other. The product of the overlapping sections between the two images is then calculated and summed. This calculation is repeated for all possible displacements over the x and y axes. The displacement is then given by the position of the maximum cross-correlation to the center of the image.
Cross-correlation calculations are typically computationally expensive. To speed up the process, one can take the 2D Fourier Transform instead. Thus, the equations to calculate the cross-correlation can be given by
where denotes the Fourier transform, is the complex conjugate, and is the inverse transform.
Example code
Example of an implementation in MATLAB:
function pxShift = xcorrreg(refImg, movedImg)
%REGISTERIMG Register two images using cross-correlation
%
% P = xcorrreg(R, M) registers two images by calculating the
% cross-correlation between them. R is the reference or stationary image,
% and M is the moved image. The output P will be a 1-by-2 vector with the
% [x, y] offsets.
%
% The resulting image can then be corrected, e.g., by using the circshift
% function Mcorr = circshift(M, P).
%
% Note: This algorithm only works for translational shifts, and will not
% work for rotational shifts or image resizing.
%Compute the cross-correlation of the two images
crossCorr = ifft2((fft2(refImg) .* conj(fft2(movedImg))));
%Find the location in pixels of the maximum correlation
[xMax, yMax] = find(crossCorr == max(crossCorr(:)));
%Compute the relative shift in pixels
Xoffset = fftshift(-size(refImg,1)/2:(size(refImg,1)/2 - 1));
Yoffset = fftshift(-size(refImg,2)/2:(size(refImg,2)/2 - 1));
pxShift = round([Xoffset(xMax), Yoffset(yMax)]);
end