I have two images from motor scan, Image1 is shifted by MxN pixels from Image2. How can I find the shift between Image1 and Image2 ?
Image -1 Image -2
If we compare the shift between the images in terms of {X,Y} I will be able to tell how many steps of the motor scan corresponds to the pixel shift on the image array sensor.
Answer
There are many different image registration algorithms, all with different advantages and disadvantages. But I usually try the simplest choice first: I simply pass the two images to ImageCorrelate
.
img1 = Import["http://i.stack.imgur.com/laZeT.png"];
img2 = Import["http://i.stack.imgur.com/LAHyF.png"];
corr = ImageAdjust[ImageCorrelate[img1, img2, EuclideanDistance]]
Intuitively, ImageCorrelate
shifts img2
by every possible offset, calculates the difference with img1
with that offset, and stores the difference in the output image. So the pixel location with the lowest value in this image corresponds to the offset that minimizes the difference:
minOffset = PixelValuePositions[corr, Min[ImageData[corr]]][[1]] -
ImageDimensions[img1]/2;
FlipView[{ImageTransformation[img1, TranslationTransform[minOffset ],
PlotRange -> Full], img2}]
If you look at the result of ImageCorrelate
, you can see that there's one global minimum. That's the offset where the "U" shapes in the two images are aligned. There's a second local minimum left of it; That's probably where the left "leg" of one U is aligned with the right leg of the other. The shape of the correlation function around the global minimum and the relative correlation values at the local minima give a qualitative impression of the accuracy and reliability of the alignment.
EuclideanDistance
compares brightness values. So the different overall brightness in the two images can "move" the minimum. If you don't want that, you can apply a filter to the images that basically removes the low-frequency background lighting changes:
filter = LaplacianGaussianFilter[#, 25] &;
corr = ImageAdjust[
ImageCorrelate[filter[img1], filter[img2], CosineDistance]]
(where 25
is roughly the width of the U-shape. Details smaller than that and low-frequency changes larger than that are removed by the LoG filter.)
Comments
Post a Comment