How to get rotation angle and scale factor by cross-correlating two matrices -MATLAB- (log-polar coordinates)












0















I am trying to implement this image registration algorithm in Matlab: FFT Based Image Registration using Corner Response, I have some issues on how to get the correct rotation angle.



Here is the algorithm that I am trying to implement:



1) The corner response of the source and target images are determined and then zero padded so that the image size will be 𝑁 × 𝑁 where 𝑁 = 2𝑛such that 𝑁 ≥ max( 𝑠𝑖𝑧𝑒(𝑠𝑜𝑢𝑟𝑐𝑒), 𝑠𝑖𝑧𝑒(𝑇𝑎𝑟𝑔𝑒𝑡)).



2) Compute the forward Fast Fourier Transform on the corner response of source and target image and take magnitude image of fast Fourier transform.



3) Highpass filter is multiplied with the magnitude image of fast Fourier transform.



4) Fourier magnitude spectrum in the cartesian co-ordinates is mapped to the polar co-ordinates.



HERE IS MY PROBLEM 5) Phase correlation technique is applied on the log-polar spectra of both the images in order to determine the scale factor and rotation angle.



6) Calculated scale factor and rotation angle are applied to the target image and once again the phase correlation is performed to detect the translation



I have implemented the 4th first steps of the algorithm and the problem that I have is in the 5th step.



I've used xcorr2() to cross-correlate the two matrices, then find the maximum value and this value will be my rotation angle, but I didn't get the correct rotation angle and another thing I think is causing this is that the type of the data is complex double instead of double (the fft() function return complex double)



%LOAD images
ref = imread('reference_image.jpg');
rec = imread('image_to_be_registred.jpg');

%Cast the images to double
i1 = double(ref);
i2 = double(rec);

%Zero Pad the source image
i2 = padarray(i2,[1 1],0,'both');

%Apply Fast Fourrier Transform to both of the two images
i1 = fft(i1);
i2 = fft(i2);

%Apply High pass filter to the two images
i1 = HighPassFilter(i1);
i2 = HighPassFilter(i2);

%Calculate polar-coordinates of the two images
polar_i1 = calculate_polar_coord(i1);
polar_i2 = calculate_polar_coord(i2);

%Cross-correlate the two matrices
theta_correlation = xcorr2(polar_i1(:,:,1),polar_i2(:,:,1));

%Get the max value of the cross-correlation result
max_theta = max(max(angle(theta_correlation)));

%Rotate the image to image to be registred using the max value
img_rotated = imrotate(rec, max_theta*180);

%Show the rotated image
imshow(img_rotated);


The expected angle is close to 3.8, but the value I've got is 0.3681.










share|improve this question























  • You are trying to implement registration using the Fourier-Mellin transform. Search for that name, you’ll find really good resources out there. Translating the location of the peak in the cross-correlation image to angle and scale is the most tricky part of this, you need to do the math properly and it will work great.

    – Cris Luengo
    Jan 2 at 0:01
















0















I am trying to implement this image registration algorithm in Matlab: FFT Based Image Registration using Corner Response, I have some issues on how to get the correct rotation angle.



Here is the algorithm that I am trying to implement:



1) The corner response of the source and target images are determined and then zero padded so that the image size will be 𝑁 × 𝑁 where 𝑁 = 2𝑛such that 𝑁 ≥ max( 𝑠𝑖𝑧𝑒(𝑠𝑜𝑢𝑟𝑐𝑒), 𝑠𝑖𝑧𝑒(𝑇𝑎𝑟𝑔𝑒𝑡)).



2) Compute the forward Fast Fourier Transform on the corner response of source and target image and take magnitude image of fast Fourier transform.



3) Highpass filter is multiplied with the magnitude image of fast Fourier transform.



4) Fourier magnitude spectrum in the cartesian co-ordinates is mapped to the polar co-ordinates.



HERE IS MY PROBLEM 5) Phase correlation technique is applied on the log-polar spectra of both the images in order to determine the scale factor and rotation angle.



6) Calculated scale factor and rotation angle are applied to the target image and once again the phase correlation is performed to detect the translation



I have implemented the 4th first steps of the algorithm and the problem that I have is in the 5th step.



I've used xcorr2() to cross-correlate the two matrices, then find the maximum value and this value will be my rotation angle, but I didn't get the correct rotation angle and another thing I think is causing this is that the type of the data is complex double instead of double (the fft() function return complex double)



%LOAD images
ref = imread('reference_image.jpg');
rec = imread('image_to_be_registred.jpg');

%Cast the images to double
i1 = double(ref);
i2 = double(rec);

%Zero Pad the source image
i2 = padarray(i2,[1 1],0,'both');

%Apply Fast Fourrier Transform to both of the two images
i1 = fft(i1);
i2 = fft(i2);

%Apply High pass filter to the two images
i1 = HighPassFilter(i1);
i2 = HighPassFilter(i2);

%Calculate polar-coordinates of the two images
polar_i1 = calculate_polar_coord(i1);
polar_i2 = calculate_polar_coord(i2);

%Cross-correlate the two matrices
theta_correlation = xcorr2(polar_i1(:,:,1),polar_i2(:,:,1));

%Get the max value of the cross-correlation result
max_theta = max(max(angle(theta_correlation)));

%Rotate the image to image to be registred using the max value
img_rotated = imrotate(rec, max_theta*180);

%Show the rotated image
imshow(img_rotated);


The expected angle is close to 3.8, but the value I've got is 0.3681.










share|improve this question























  • You are trying to implement registration using the Fourier-Mellin transform. Search for that name, you’ll find really good resources out there. Translating the location of the peak in the cross-correlation image to angle and scale is the most tricky part of this, you need to do the math properly and it will work great.

    – Cris Luengo
    Jan 2 at 0:01














0












0








0








I am trying to implement this image registration algorithm in Matlab: FFT Based Image Registration using Corner Response, I have some issues on how to get the correct rotation angle.



Here is the algorithm that I am trying to implement:



1) The corner response of the source and target images are determined and then zero padded so that the image size will be 𝑁 × 𝑁 where 𝑁 = 2𝑛such that 𝑁 ≥ max( 𝑠𝑖𝑧𝑒(𝑠𝑜𝑢𝑟𝑐𝑒), 𝑠𝑖𝑧𝑒(𝑇𝑎𝑟𝑔𝑒𝑡)).



2) Compute the forward Fast Fourier Transform on the corner response of source and target image and take magnitude image of fast Fourier transform.



3) Highpass filter is multiplied with the magnitude image of fast Fourier transform.



4) Fourier magnitude spectrum in the cartesian co-ordinates is mapped to the polar co-ordinates.



HERE IS MY PROBLEM 5) Phase correlation technique is applied on the log-polar spectra of both the images in order to determine the scale factor and rotation angle.



6) Calculated scale factor and rotation angle are applied to the target image and once again the phase correlation is performed to detect the translation



I have implemented the 4th first steps of the algorithm and the problem that I have is in the 5th step.



I've used xcorr2() to cross-correlate the two matrices, then find the maximum value and this value will be my rotation angle, but I didn't get the correct rotation angle and another thing I think is causing this is that the type of the data is complex double instead of double (the fft() function return complex double)



%LOAD images
ref = imread('reference_image.jpg');
rec = imread('image_to_be_registred.jpg');

%Cast the images to double
i1 = double(ref);
i2 = double(rec);

%Zero Pad the source image
i2 = padarray(i2,[1 1],0,'both');

%Apply Fast Fourrier Transform to both of the two images
i1 = fft(i1);
i2 = fft(i2);

%Apply High pass filter to the two images
i1 = HighPassFilter(i1);
i2 = HighPassFilter(i2);

%Calculate polar-coordinates of the two images
polar_i1 = calculate_polar_coord(i1);
polar_i2 = calculate_polar_coord(i2);

%Cross-correlate the two matrices
theta_correlation = xcorr2(polar_i1(:,:,1),polar_i2(:,:,1));

%Get the max value of the cross-correlation result
max_theta = max(max(angle(theta_correlation)));

%Rotate the image to image to be registred using the max value
img_rotated = imrotate(rec, max_theta*180);

%Show the rotated image
imshow(img_rotated);


The expected angle is close to 3.8, but the value I've got is 0.3681.










share|improve this question














I am trying to implement this image registration algorithm in Matlab: FFT Based Image Registration using Corner Response, I have some issues on how to get the correct rotation angle.



Here is the algorithm that I am trying to implement:



1) The corner response of the source and target images are determined and then zero padded so that the image size will be 𝑁 × 𝑁 where 𝑁 = 2𝑛such that 𝑁 ≥ max( 𝑠𝑖𝑧𝑒(𝑠𝑜𝑢𝑟𝑐𝑒), 𝑠𝑖𝑧𝑒(𝑇𝑎𝑟𝑔𝑒𝑡)).



2) Compute the forward Fast Fourier Transform on the corner response of source and target image and take magnitude image of fast Fourier transform.



3) Highpass filter is multiplied with the magnitude image of fast Fourier transform.



4) Fourier magnitude spectrum in the cartesian co-ordinates is mapped to the polar co-ordinates.



HERE IS MY PROBLEM 5) Phase correlation technique is applied on the log-polar spectra of both the images in order to determine the scale factor and rotation angle.



6) Calculated scale factor and rotation angle are applied to the target image and once again the phase correlation is performed to detect the translation



I have implemented the 4th first steps of the algorithm and the problem that I have is in the 5th step.



I've used xcorr2() to cross-correlate the two matrices, then find the maximum value and this value will be my rotation angle, but I didn't get the correct rotation angle and another thing I think is causing this is that the type of the data is complex double instead of double (the fft() function return complex double)



%LOAD images
ref = imread('reference_image.jpg');
rec = imread('image_to_be_registred.jpg');

%Cast the images to double
i1 = double(ref);
i2 = double(rec);

%Zero Pad the source image
i2 = padarray(i2,[1 1],0,'both');

%Apply Fast Fourrier Transform to both of the two images
i1 = fft(i1);
i2 = fft(i2);

%Apply High pass filter to the two images
i1 = HighPassFilter(i1);
i2 = HighPassFilter(i2);

%Calculate polar-coordinates of the two images
polar_i1 = calculate_polar_coord(i1);
polar_i2 = calculate_polar_coord(i2);

%Cross-correlate the two matrices
theta_correlation = xcorr2(polar_i1(:,:,1),polar_i2(:,:,1));

%Get the max value of the cross-correlation result
max_theta = max(max(angle(theta_correlation)));

%Rotate the image to image to be registred using the max value
img_rotated = imrotate(rec, max_theta*180);

%Show the rotated image
imshow(img_rotated);


The expected angle is close to 3.8, but the value I've got is 0.3681.







matlab medical cross-correlation image-registration






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 23:47









Amine SehabaAmine Sehaba

133




133













  • You are trying to implement registration using the Fourier-Mellin transform. Search for that name, you’ll find really good resources out there. Translating the location of the peak in the cross-correlation image to angle and scale is the most tricky part of this, you need to do the math properly and it will work great.

    – Cris Luengo
    Jan 2 at 0:01



















  • You are trying to implement registration using the Fourier-Mellin transform. Search for that name, you’ll find really good resources out there. Translating the location of the peak in the cross-correlation image to angle and scale is the most tricky part of this, you need to do the math properly and it will work great.

    – Cris Luengo
    Jan 2 at 0:01

















You are trying to implement registration using the Fourier-Mellin transform. Search for that name, you’ll find really good resources out there. Translating the location of the peak in the cross-correlation image to angle and scale is the most tricky part of this, you need to do the math properly and it will work great.

– Cris Luengo
Jan 2 at 0:01





You are trying to implement registration using the Fourier-Mellin transform. Search for that name, you’ll find really good resources out there. Translating the location of the peak in the cross-correlation image to angle and scale is the most tricky part of this, you need to do the math properly and it will work great.

– Cris Luengo
Jan 2 at 0:01












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999826%2fhow-to-get-rotation-angle-and-scale-factor-by-cross-correlating-two-matrices-ma%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999826%2fhow-to-get-rotation-angle-and-scale-factor-by-cross-correlating-two-matrices-ma%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith