How to capture the lateral and angular position between a reference image and a shifted (and/or) turned copy of the same image using the correlation of images using matlab? What program should be used for this?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
How to capture the lateral and angular position between a reference image and a shifted (and/or) turned copy of the same image using the correlation of images using matlab? What program should be used for this? How to process it?
0 comentarios
Respuestas (2)
Jonas
el 30 de Mayo de 2021
use xcorr2 for finding shifts between two images
if you have to use correlation to find the rotation of a image i would rotate the image several times and calculate the xcorr2 for each rotation.
if you don't have to use correlation, have a look into https://de.mathworks.com/help/vision/ug/find-image-rotation-and-scale-using-automated-feature-matching.html
3 comentarios
Image Analyst
el 30 de Mayo de 2021
Vishnu, You accepted this answer so you must have gotten lucky and tried the same angle it got rotated, because this will not work in general. Evidently you don't know what what the radon transfer is because you didn't try my suggestion, or even the first suggestion. Even if you tried my first suggestion, it would have been better than randomly trying some rotations and doing a cross correlation. At the very least you should use normalized corss correlation, using normxcorr2() (like in my attached demo) rather than xcorr2.
I know the help says "Use cross-correlation to find where a section of an image fits in the whole. Cross-correlation enables you to find the regions in which two signals most resemble each other. " but that is not really true in general. The max of the correlation does not indicate the template is located in the original (despite what the help says), unless the template really is in the original and you get lucky. For example if you had a block of all 255 in the big image and were trying to find a small template in the big image, it would find it in the block of all 255s instead of where you actually copied the template from. Even if the template is located in the image, the correlation max is not necessarily where the template is. Don't believe me? Most people don't because they'd rather believe the documentation than me (who has more than 40 years of image processing experience). So I'll prove it:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'coins.png';
grayImage = imread(baseFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = min(grayImage, [], 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% Define template we want to look for
template = grayImage(59:111, 83:137);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 2);
imshow(template, []);
[rows2, columns2] = size(template)
axis('on', 'image');
title('Template Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
% Do the correlation
corrImage = xcorr2(grayImage, template); % Wrong
%corrImage = normxcorr2(template, grayImage); % Right
[rows3, columns3] = size(corrImage)
% Crop the image
corrImage = imcrop(corrImage, [(columns3-columns)/2, (rows3-rows)/2, columns, rows]);
% Display the image.
subplot(2, 2, 3);
imshow(corrImage, []);
axis('on', 'image');
title('Correlation Image. Max at red crosshairs', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
% Find the max
maxValue = max(corrImage(:))
[y, x] = find(corrImage == maxValue)
% Plot the max over the correlation image
hold on;
plot(x, y, 'r+', 'LineWidth', 2, 'MarkerSize', 50);
% Plot it over the original image
subplot(2, 2, 4);
imshow(grayImage, []);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Plot where the correlation max is.
hold on;
plot(x, y, 'r+', 'LineWidth', 4, 'MarkerSize', 50);
% Plot where the template ACTUALLY came from.
plot(mean([83,137]), mean([59,111]), 'g+', 'LineWidth', 4, 'MarkerSize', 50);
title('Green = Template location, Red = correlation max', 'FontSize', fontSize, 'Interpreter', 'None');
See? It totally missed the location where the template was actually plucked from and the template and image are most similar. So I hope this proves that what the help documentation says is not true. xcorr2 does NOT "find the regions in which two signals most resemble each other. " as I have just proven.
Now run the code with the xcorr2 line commented out and the line below it (call to normxcorr2) in there and you'll see it finds the right location.
Nonetheless, if you're interested, try finding angles of a known object in the image with regionprops() and see how it changes in the second image (my first suggestion). Or else learn how the radon transform works. I mean, the guy who used it won a Nobel prize for it so I think it's worth knowing about.
Jonas
el 31 de Mayo de 2021
@ImageAnalyst: actually i really like your answer more compared to mine, maybe the issue here is that he wrote he is allowed to use correlation only, so any other function which works better is not 'allowed' (except of your point regarding normalization)
Image Analyst
el 30 de Mayo de 2021
Is there something in the image that we can use to determine the angle of the image? I don't know -- you forgot to post it.
Otherwise, you might compute the radon transform of the images and correlate the radon transforms to determine the change in angle from one image to the next.
0 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!