Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to calculate the angle between two images?

Subject: How to calculate the angle between two images?

From: khoo

Date: 3 Apr, 2009 11:55:03

Message: 1 of 43

Hi all, currently i face the problem to find the rotation angle...i got one image is rotate in unknown angle compare with the original image, but i not have idea how to calculate the angle cause the angle is a variable angle..can anyone senior teach me here,...need it urgently...and i quite noob in image processing..

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 3 Apr, 2009 12:07:01

Message: 2 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gr4ten$oks$1@fred.mathworks.com>...
> Hi all, currently i face the problem to find the rotation angle...i got one image is rotate in unknown angle compare with the original image, but i not have idea how to calculate the angle cause the angle is a variable angle..can anyone senior teach me here,...need it urgently...and i quite noob in image processing..

One method that I found for easily determining the relative
angle of 'twist' between a known reference image and a rotated image was accomplished very reliably even for arbitrary images.

1) Compute the vertical and horizontal gradient of every
pixel
2) Use the atan2 function to convert the image to an angle
image. Note that pixels on a featureless region returns
angles that are purely dictated by noise.
3) Calculate an edge image - using edge, Canny is a good
choice, but Sobel will probably work - depending on the
complexity of the image.
4) Fatten up the edge lines using morphology operators
dilate/erode. Binarize into a mask image
5) Mask the angle image with above mask, thus removing
meaningless angle pixels. We are only looking at gradients
that are real.
6) Apply this to the reference image, and the target image
7) Histogram the results. Remember as the pixels represent
angles, the histogram essentially wraps round 0 mapping to
2Pi radians.
8) Compare the histograms, I found correlation a good way
to do this, the peak on the correlogram provides you with
the relative rotation between the two images.

One danger point is that if your image suffers badly from
digital stepping (aliasing) you can get peaks at 0 and 90
degrees on both images. So you need to do something
intelligent at removing this from your histograms prior to
doing your comparison.

One of the fun things that I tried, was to use fast
correlation to do the matching. I FFT'd both histograms,
then extended the spectra by zero packing, done the
multiplication in the frequency domain, then followed up by
the IFFT. This generated a very nicely interpolated measure
of the relative rotation between the two images.

Hope that this is helpful, and makes sense

Regards

Dave Robinson

Subject: How to calculate the angle between two images?

From: khoo

Date: 3 Apr, 2009 12:22:01

Message: 3 of 43

> One method that I found for easily determining the relative
> angle of 'twist' between a known reference image and a rotated image was accomplished very reliably even for arbitrary images.
>
> 1) Compute the vertical and horizontal gradient of every
> pixel
> 2) Use the atan2 function to convert the image to an angle
> image. Note that pixels on a featureless region returns
> angles that are purely dictated by noise.
> 3) Calculate an edge image - using edge, Canny is a good
> choice, but Sobel will probably work - depending on the
> complexity of the image.
> 4) Fatten up the edge lines using morphology operators
> dilate/erode. Binarize into a mask image
> 5) Mask the angle image with above mask, thus removing
> meaningless angle pixels. We are only looking at gradients
> that are real.
> 6) Apply this to the reference image, and the target image
> 7) Histogram the results. Remember as the pixels represent
> angles, the histogram essentially wraps round 0 mapping to
> 2Pi radians.
> 8) Compare the histograms, I found correlation a good way
> to do this, the peak on the correlogram provides you with
> the relative rotation between the two images.
>
> One danger point is that if your image suffers badly from
> digital stepping (aliasing) you can get peaks at 0 and 90
> degrees on both images. So you need to do something
> intelligent at removing this from your histograms prior to
> doing your comparison.
>
> One of the fun things that I tried, was to use fast
> correlation to do the matching. I FFT'd both histograms,
> then extended the spectra by zero packing, done the
> multiplication in the frequency domain, then followed up by
> the IFFT. This generated a very nicely interpolated measure
> of the relative rotation between the two images.
>
> Hope that this is helpful, and makes sense
>
> Regards
>
> Dave Robinson

  4) Binarize into a mask image
> 5) Mask the angle image with above mask, thus removing
> meaningless angle pixels. We are only looking at gradients
> that are real.


i no understand this two sentence meaning..can u explain it or provide the code need to be used??

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 3 Apr, 2009 12:51:01

Message: 4 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gr4v19$60a$1@fred.mathworks.com>...
> > 5) Mask the angle image with above mask, thus removing
> > meaningless angle pixels. We are only looking at gradients
> > that are real.
>
>
> i no understand this two sentence meaning..can u explain it or provide the code need to be used??

No I don't have any Matlab code I can give you, I make it a policy not to do this. You need to write your own software in order for you to be able to master the craft - just like all of the Guru's here had to.

What I am saying in those two sentences is every pixel in the image has an angle calculated using the atan2 operation. However these angles in regions of the image that are featureless have no meaning, they are purely due to noise in the imaging process. This means we need some way of identifying real angles and meaningless angles. Real angles occur where we have edges, so if we pass both original images through an edge detector such as edge(canny) or edge(sobel) and then 'make the indicated edges slightly fatter using the correct morphology operator, then it follows that if we lay this modified edge image over its corresponding angle image, then angle values corresponding to the pixels hilighted in the edge image correspond to angles we can believe in. Those in areas where there are no edges cannot be trusted.

Now you need to make 2 histograms (one from each of your angle images) of your trusted angles. What you find is that these histograms are virtually identical, with the exception that they are shifted relative to each other. This shift is equal to the relative rotation of the original images.

Hope that is clearer.

Dave Robinson

Subject: How to calculate the angle between two images?

From: khoo

Date: 3 Apr, 2009 13:49:01

Message: 5 of 43

> No I don't have any Matlab code I can give you, I make it a policy not to do this. You need to write your own software in order for you to be able to master the craft - just like all of the Guru's here had to.
>
> What I am saying in those two sentences is every pixel in the image has an angle calculated using the atan2 operation. However these angles in regions of the image that are featureless have no meaning, they are purely due to noise in the imaging process. This means we need some way of identifying real angles and meaningless angles. Real angles occur where we have edges, so if we pass both original images through an edge detector such as edge(canny) or edge(sobel) and then 'make the indicated edges slightly fatter using the correct morphology operator, then it follows that if we lay this modified edge image over its corresponding angle image, then angle values corresponding to the pixels hilighted in the edge image correspond to angles we can believe in. Those in areas where there are no edges cannot be trusted.
>
> Now you need to make 2 histograms (one from each of your angle images) of your trusted angles. What you find is that these histograms are virtually identical, with the exception that they are shifted relative to each other. This shift is equal to the relative rotation of the original images.
>

i ady done the in front part but stuck at the modified edge image over corresponding angle..i dunno wat function need to be used..i using time but got error..

this is the code i currently obtained so far

im1=imread('image1.jpg');
im11=rgb2gray(im1);
im111=im2double(im11);
[dx,dy]=gradient(im111);
p1=atan2(dy,dx);
im1111=edge(p1,'canny');
bw1 = bwmorph(im1111,'dilate');
im=p1*bw1;
imshow(im)

this is the error messange i got
??? Error using ==> mtimes
Inner matrix dimensions must agree.

so wat the function i need to used??

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 3 Apr, 2009 14:15:04

Message: 6 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gr544d$5vk$1@fred.mathworks.com>...
> > No I don't have any Matlab code I can give you, I make it a policy not to do this. You need to write your own software in order for you to be able to master the craft - just like all of the Guru's here had to.
> >
> > What I am saying in those two sentences is every pixel in the image has an angle calculated using the atan2 operation. However these angles in regions of the image that are featureless have no meaning, they are purely due to noise in the imaging process. This means we need some way of identifying real angles and meaningless angles. Real angles occur where we have edges, so if we pass both original images through an edge detector such as edge(canny) or edge(sobel) and then 'make the indicated edges slightly fatter using the correct morphology operator, then it follows that if we lay this modified edge image over its corresponding angle image, then angle values corresponding to the pixels hilighted in the edge image correspond to angles we can believe in. Those in areas where there are no edges cannot be trusted.
> >
> > Now you need to make 2 histograms (one from each of your angle images) of your trusted angles. What you find is that these histograms are virtually identical, with the exception that they are shifted relative to each other. This shift is equal to the relative rotation of the original images.
> >
>
> i ady done the in front part but stuck at the modified edge image over corresponding angle..i dunno wat function need to be used..i using time but got error..
>
> this is the code i currently obtained so far
>
> im1=imread('image1.jpg');
> im11=rgb2gray(im1);
> im111=im2double(im11);
> [dx,dy]=gradient(im111);
> p1=atan2(dy,dx);
> im1111=edge(p1,'canny');
> bw1 = bwmorph(im1111,'dilate');
> im=p1*bw1;
> imshow(im)
>
> this is the error messange i got
> ??? Error using ==> mtimes
> Inner matrix dimensions must agree.
>
> so wat the function i need to used??

I am a bit busy to study your code closely right at the moment, but what seem to be a problem is
         im=p1*bw1;
I would have thought that it should read
        im=p1.*bw1;

The dot before the * is kind of crucial check the help files.

By the way I am impressed with the speed at which you converted my description into code, it might not have worked first time, but this is the only way to really get to grips with a very powerful problem solving tool

Well done

Dave Robinson

Subject: How to calculate the angle between two images?

From: Skeptic

Date: 3 Apr, 2009 14:20:02

Message: 7 of 43

Dave Robinson wrote a nice algorithm:
> One method that I found for easily determining the relative
> angle of 'twist' between a known reference image and a rotated image was accomplished very reliably even for arbitrary images.
>
[snip]
> Dave Robinson
-------------------------------------------------------
Dave:
Nice. Clever yet makes good intuitive sense. Does it still perform
well under stress conditions such as:
1. image contrasts don't match
2. rotations, such as 45 degrees, where some information is rotated
out and other new info is rotated in (think of an aerial photo rotated
45 degrees).
3. rotation is not about the center of the image.
-ImageAnalyst

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 3 Apr, 2009 14:39:01

Message: 8 of 43

Skeptic <skeptic@mailinator.com> wrote in message <6dc4d317-8b62-4e03-8bea-662e35f6eb67@d38g2000prn.googlegroups.com>...
> Dave Robinson wrote a nice algorithm:
> > One method that I found for easily determining the relative
> > angle of 'twist' between a known reference image and a rotated image was accomplished very reliably even for arbitrary images.
> >
> [snip]
> > Dave Robinson
> -------------------------------------------------------
> Dave:
> Nice. Clever yet makes good intuitive sense. Does it still perform
> well under stress conditions such as:
> 1. image contrasts don't match
> 2. rotations, such as 45 degrees, where some information is rotated
> out and other new info is rotated in (think of an aerial photo rotated
> 45 degrees).
> 3. rotation is not about the center of the image.
> -ImageAnalyst

1) My application had relatively controlled lighting, but as the angle is computed indirectly via a ratio then it should be fairly robust against variable contrast

2) Again this wasn't a problem to me, my 'credit cards' were on a featureless conveyor belt. In general I think you have a good point.

3) The angle of rotation is translation independent, so apart from point 2) where you bring in other image regions you are OK.

Thanks for your welcome input, it is nice to be able to bounce ideas with someone of your reputation

Regards

Dave Robinson

Subject: How to calculate the angle between two images?

From: khoo

Date: 3 Apr, 2009 16:29:02

Message: 9 of 43

> > > Now you need to make 2 histograms (one from each of your angle images) of your trusted angles. What you find is that these histograms are virtually identical, with the exception that they are shifted relative to each other. This shift is equal to the relative rotation of the original images.
> > >
> >
> > i ady done the in front part but stuck at the modified edge image over corresponding angle..i dunno wat function need to be used..i using time but got error..
> >
> > this is the code i currently obtained so far
> >
> > im1=imread('image1.jpg');
> > im11=rgb2gray(im1);
> > im111=im2double(im11);
> > [dx,dy]=gradient(im111);
> > p1=atan2(dy,dx);
> > im1111=edge(p1,'canny');
> > bw1 = bwmorph(im1111,'dilate');
> > im=p1*bw1;
> > imshow(im)
> >
> > this is the error messange i got
> > ??? Error using ==> mtimes
> > Inner matrix dimensions must agree.
> >
> > so wat the function i need to used??
>
> I am a bit busy to study your code closely right at the moment, but what seem to be a problem is
> im=p1*bw1;
> I would have thought that it should read
> im=p1.*bw1;
>
> The dot before the * is kind of crucial check the help files.
>
> By the way I am impressed with the speed at which you converted my description into code, it might not have worked first time, but this is the only way to really get to grips with a very powerful problem solving tool
>
> Well done
>
> Dave Robinson


now i ady done until get the histogram, but how to get the angle between two image?? i no understand this statement

What you find is that these histograms are virtually identical, with the exception that they are shifted relative to each other. This shift is equal to the relative rotation of the original images.

this is my code currently..i dunno how to find the angle out between two image....

clear all;clc

im1=imread('image1.jpg');
im11=rgb2gray(im1);
im111=im2double(im11);
[dx,dy]=gradient(im111);
p1=atan2(dy,dx);
im1111=edge(p1,'canny');
bw1 = bwmorph(im1111,'dilate');
im11111=p1.*bw1;
[max_angle1]=find(max(max(im11111)));
subplot(2,1,1);hist(im11111)


im2=imread('image2.jpg');
im22=rgb2gray(im2);
im222=im2double(im22);
[dx,dy]=gradient(im222);
p2=atan2(dy,dx);
im2222=edge(p2,'canny');
bw2 = bwmorph(im2222,'dilate');
im22222=p2.*bw2;
[max_angle2]=find(max(max(im22222)));
subplot(2,1,2);hist(im22222)

the max angle i try find for each image..but all is same...is 1..what i need to do next??

Subject: How to calculate the angle between two images?

From: khoo

Date: 3 Apr, 2009 16:57:01

Message: 10 of 43

i want find out how many the angle for the image2 to rotate until the same angle like image 1..so that the two image can be do phase correlation...

Subject: How to calculate the angle between two images?

From: Roger Stafford

Date: 3 Apr, 2009 17:47:01

Message: 11 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gr4ten$oks$1@fred.mathworks.com>...
> Hi all, currently i face the problem to find the rotation angle...i got one image is rotate in unknown angle compare with the original image, but i not have idea how to calculate the angle cause the angle is a variable angle..can anyone senior teach me here,...need it urgently...and i quite noob in image processing..

  If you can identify and locate a number of corresponding points in the two images, you could use the Statistics Toolbox function 'procrustes' to find the least squares best rotation between them.

Roger Stafford

Subject: How to calculate the angle between two images?

From: khoo

Date: 3 Apr, 2009 18:00:17

Message: 12 of 43

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gr5i2l$p34$1@fred.mathworks.com>...
> "khoo" <jim_khoo@hotmail.com> wrote in message <gr4ten$oks$1@fred.mathworks.com>...
> > Hi all, currently i face the problem to find the rotation angle...i got one image is rotate in unknown angle compare with the original image, but i not have idea how to calculate the angle cause the angle is a variable angle..can anyone senior teach me here,...need it urgently...and i quite noob in image processing..
>
> If you can identify and locate a number of corresponding points in the two images, you could use the Statistics Toolbox function 'procrustes' to find the least squares best rotation between them.
>
> Roger Stafford

izit u mean get the reference point in the image?? but how to get the reference point??

Subject: How to calculate the angle between two images?

From: Roger Stafford

Date: 3 Apr, 2009 18:24:01

Message: 13 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gr5irh$g8m$1@fred.mathworks.com>...
> izit u mean get the reference point in the image?? but how to get the reference point??

  I'm afraid I can't help you with that, Khoo. I'm not an expert on image processing.

  I do have an understanding of the procrustes problem, though. It works quite well if supplied with matching sets of points. It can not only find the best rotation angle but also the best translation vector and the best rescaling.

Roger Stafford

Subject: How to calculate the angle between two images?

From: Image Analyst

Date: 3 Apr, 2009 20:30:04

Message: 14 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message
> izit u mean get the reference point in the image?? but how to get the reference point??
-----------------------------------------------------------
first of all, do you have the statistics toolbox like Roger mentioned? If you do, you can manually specify corresponding pairs of landmarks on the images by using the impoint() function.

Subject: How to calculate the angle between two images?

From: khoo

Date: 4 Apr, 2009 03:09:03

Message: 15 of 43

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <gr5rkc$pr0$1@fred.mathworks.com>...
> "khoo" <jim_khoo@hotmail.com> wrote in message
> > izit u mean get the reference point in the image?? but how to get the reference point??
> -----------------------------------------------------------
> first of all, do you have the statistics toolbox like Roger mentioned? If you do, you can manually specify corresponding pairs of landmarks on the images by using the impoint() function.

ya..i got the statistics toolbox like roger mentioned...but i prefer use dave idea..but i dunno how to compare two histogram, dave pls help..but i will try to use your idea also...

Subject: How to calculate the angle between two images?

From: khoo

Date: 4 Apr, 2009 04:09:02

Message: 16 of 43

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <gr5rkc$pr0$1@fred.mathworks.com>...
> "khoo" <jim_khoo@hotmail.com> wrote in message
> > izit u mean get the reference point in the image?? but how to get the reference point??
> -----------------------------------------------------------
> first of all, do you have the statistics toolbox like Roger mentioned? If you do, you can manually specify corresponding pairs of landmarks on the images by using the impoint() function.

my matlab not have impoint function...jz got the getpoint function..

Subject: How to calculate the angle between two images?

From: khoo

Date: 4 Apr, 2009 16:40:03

Message: 17 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gr4v19$60a$1@fred.mathworks.com>...
> > One method that I found for easily determining the relative
> > angle of 'twist' between a known reference image and a rotated image was accomplished very reliably even for arbitrary images.
> >
> > 1) Compute the vertical and horizontal gradient of every
> > pixel
> > 2) Use the atan2 function to convert the image to an angle
> > image. Note that pixels on a featureless region returns
> > angles that are purely dictated by noise.
> > 3) Calculate an edge image - using edge, Canny is a good
> > choice, but Sobel will probably work - depending on the
> > complexity of the image.
> > 4) Fatten up the edge lines using morphology operators
> > dilate/erode. Binarize into a mask image
> > 5) Mask the angle image with above mask, thus removing
> > meaningless angle pixels. We are only looking at gradients
> > that are real.
> > 6) Apply this to the reference image, and the target image
> > 7) Histogram the results. Remember as the pixels represent
> > angles, the histogram essentially wraps round 0 mapping to
> > 2Pi radians.
> > 8) Compare the histograms, I found correlation a good way
> > to do this, the peak on the correlogram provides you with
> > the relative rotation between the two images.
> >
> > One danger point is that if your image suffers badly from
> > digital stepping (aliasing) you can get peaks at 0 and 90
> > degrees on both images. So you need to do something
> > intelligent at removing this from your histograms prior to
> > doing your comparison.
> >
> > One of the fun things that I tried, was to use fast
> > correlation to do the matching. I FFT'd both histograms,
> > then extended the spectra by zero packing, done the
> > multiplication in the frequency domain, then followed up by
> > the IFFT. This generated a very nicely interpolated measure
> > of the relative rotation between the two images.
> >
> > Hope that this is helpful, and makes sense
> >
> > Regards
> >
> > Dave Robinson
>

I FFT'd both histograms,
> > then extended the spectra by zero packing, done the
> > multiplication in the frequency domain, then followed up by
> > the IFFT. This generated a very nicely interpolated measure
> > of the relative rotation between the two images.


bro, i no understand wat this mean?? and how to extend the spctra by zero packing??can explain and teach me?

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 4 Apr, 2009 17:49:02

Message: 18 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gr82h2$d9h$1@fred.mathworks.com>...
> "khoo" <jim_khoo@hotmail.com> wrote in message <gr4v19$60a$1@fred.mathworks.com>...
> > > One method that I found for easily determining the relative
> > > angle of 'twist' between a known reference image and a rotated image was accomplished very reliably even for arbitrary images.
> > >
> > > 1) Compute the vertical and horizontal gradient of every
> > > pixel
> > > 2) Use the atan2 function to convert the image to an angle
> > > image. Note that pixels on a featureless region returns
> > > angles that are purely dictated by noise.
> > > 3) Calculate an edge image - using edge, Canny is a good
> > > choice, but Sobel will probably work - depending on the
> > > complexity of the image.
> > > 4) Fatten up the edge lines using morphology operators
> > > dilate/erode. Binarize into a mask image
> > > 5) Mask the angle image with above mask, thus removing
> > > meaningless angle pixels. We are only looking at gradients
> > > that are real.
> > > 6) Apply this to the reference image, and the target image
> > > 7) Histogram the results. Remember as the pixels represent
> > > angles, the histogram essentially wraps round 0 mapping to
> > > 2Pi radians.
> > > 8) Compare the histograms, I found correlation a good way
> > > to do this, the peak on the correlogram provides you with
> > > the relative rotation between the two images.
> > >
> > > One danger point is that if your image suffers badly from
> > > digital stepping (aliasing) you can get peaks at 0 and 90
> > > degrees on both images. So you need to do something
> > > intelligent at removing this from your histograms prior to
> > > doing your comparison.
> > >
> > > One of the fun things that I tried, was to use fast
> > > correlation to do the matching. I FFT'd both histograms,
> > > then extended the spectra by zero packing, done the
> > > multiplication in the frequency domain, then followed up by
> > > the IFFT. This generated a very nicely interpolated measure
> > > of the relative rotation between the two images.
> > >
> > > Hope that this is helpful, and makes sense
> > >
> > > Regards
> > >
> > > Dave Robinson
> >
>
> I FFT'd both histograms,
> > > then extended the spectra by zero packing, done the
> > > multiplication in the frequency domain, then followed up by
> > > the IFFT. This generated a very nicely interpolated measure
> > > of the relative rotation between the two images.
>
>
> bro, i no understand wat this mean?? and how to extend the spctra by zero packing??can explain and teach me?

Doing it at matching points, as Roger suggests is one good way of doing it, however unless you have ImageAnalysts point 2) problem, this shouldn't be necessary. However without seeing your images - it is difficult to say.

First consider your two histograms, these represent the number of meaningful angle pixels binned into a 'x axis' position representing an angle. The complete x axis of the histogram represents an angle of 0 ->2pi. as your image rotates, so the predominant edges will rotate by that angle, and thus comparing the two histograms, what you should see is the same general pattern in both histograms but being shifted one way or other by their relative rotation angle, as measured on the x axis of the histograms.

Now ignoring zero packing for a moment, if you do see this general similarity, then one way to find the angle of offset is by applying correlation, not to the images -but to the histograms themselves.

Fast convolution using the FFT is particularly suited for this operation as bot FFT correlation, and the histograms themselves can be considered cylindrical, namely details that get shifted say left off of the histogram don't get lost - they reappear at the right side of the histogram, (remember we are on an angular scale) FFT correlation has exactly the same effect, and normally you have to work like blazes to remove the wrap around effect from your computation, here it is just what we want.

The computation itself should be easy, simply FFT both histograms - remember this results in complex answers - don't throw away the phase, do complex arithmetic for the next phase which is

multiply the spectrum of one histogram by the complex conjugate of the second spectrum (remember to .* not *) then find the real part of the inverse transform of the product.

Look at this resulting vector, and hopefully you should find a peak value corresponding to the relative rotation angle between the two images. If you do, come back and we will discuss the zero packing to get better angular resolution, if not then we need to see the type of image you are working with.

Regards

Dave Robinson

Subject: How to calculate the angle between two images?

From: khoo

Date: 4 Apr, 2009 18:53:01

Message: 19 of 43

> The computation itself should be easy, simply FFT both histograms - remember this results in complex answers - don't throw away the phase, do complex arithmetic for the next phase which is
>
> multiply the spectrum of one histogram by the complex conjugate of the second spectrum (remember to .* not *) then find the real part of the inverse transform of the product.
>
> Look at this resulting vector, and hopefully you should find a peak value corresponding to the relative rotation angle between the two images. If you do, come back and we will discuss the zero packing to get better angular resolution, if not then we need to see the type of image you are working with.
>
> Regards
>
> Dave Robinson


bro, i face some error at the times there...

this is the error code i get
??? Error using ==> times
Matrix dimensions must agree.

and this is the code currently..which do until the .* you teach me..

clear all;clc

im1=imread('image1.jpg');
im11=rgb2gray(im1);
im111=im2double(im11);
[dx,dy]=gradient(im111);
p1=atan2(dy,dx);
im1111=edge(p1,'canny');
bw1 = bwmorph(im1111,'dilate');
im11111=p1.*bw1;
im6=hist(im11111);
im66=fft(im6);

im2=imread('image2.jpg');
im22=rgb2gray(im2);
im222=im2double(im22);
[dx,dy]=gradient(im222);
p2=atan2(dy,dx);
im2222=edge(p2,'canny');
bw2 = bwmorph(im2222,'dilate');
im22222=p2.*bw2;
im7=hist(im22222);
im77=fft(im7);
im777=conj(im77);

im8=im66.*im777;
imshow(im8)

and this is both image i try to find the angles


[IMG]http://i278.photobucket.com/albums/kk119/khoo011/image1.jpg[/IMG]
[IMG]http://i278.photobucket.com/albums/kk119/khoo011/image2.jpg[/IMG]

Subject: How to calculate the angle between two images?

From: khoo

Date: 5 Apr, 2009 02:56:01

Message: 20 of 43

> > bro, i no understand wat this mean?? and how to extend the spctra by zero packing??can explain and teach me?
>
> Doing it at matching points, as Roger suggests is one good way of doing it, however unless you have ImageAnalysts point 2) problem, this shouldn't be necessary. However without seeing your images - it is difficult to say.
>
> First consider your two histograms, these represent the number of meaningful angle pixels binned into a 'x axis' position representing an angle. The complete x axis of the histogram represents an angle of 0 ->2pi. as your image rotates, so the predominant edges will rotate by that angle, and thus comparing the two histograms, what you should see is the same general pattern in both histograms but being shifted one way or other by their relative rotation angle, as measured on the x axis of the histograms.
>
> Now ignoring zero packing for a moment, if you do see this general similarity, then one way to find the angle of offset is by applying correlation, not to the images -but to the histograms themselves.
>
> Fast convolution using the FFT is particularly suited for this operation as bot FFT correlation, and the histograms themselves can be considered cylindrical, namely details that get shifted say left off of the histogram don't get lost - they reappear at the right side of the histogram, (remember we are on an angular scale) FFT correlation has exactly the same effect, and normally you have to work like blazes to remove the wrap around effect from your computation, here it is just what we want.
>
> The computation itself should be easy, simply FFT both histograms - remember this results in complex answers - don't throw away the phase, do complex arithmetic for the next phase which is
>
> multiply the spectrum of one histogram by the complex conjugate of the second spectrum (remember to .* not *) then find the real part of the inverse transform of the product.
>
> Look at this resulting vector, and hopefully you should find a peak value corresponding to the relative rotation angle between the two images. If you do, come back and we will discuss the zero packing to get better angular resolution, if not then we need to see the type of image you are working with.
>
> Regards
>
> Dave Robinson


bro Dave, pls help fast..today is my last day to complete oit ady...pls help me...urgent..

Subject: How to calculate the angle between two images?

From: khoo

Date: 5 Apr, 2009 10:24:01

Message: 21 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gr96k1$cgv$1@fred.mathworks.com>...
> > > bro, i no understand wat this mean?? and how to extend the spctra by zero packing??can explain and teach me?
> >
> > Doing it at matching points, as Roger suggests is one good way of doing it, however unless you have ImageAnalysts point 2) problem, this shouldn't be necessary. However without seeing your images - it is difficult to say.
> >
> > First consider your two histograms, these represent the number of meaningful angle pixels binned into a 'x axis' position representing an angle. The complete x axis of the histogram represents an angle of 0 ->2pi. as your image rotates, so the predominant edges will rotate by that angle, and thus comparing the two histograms, what you should see is the same general pattern in both histograms but being shifted one way or other by their relative rotation angle, as measured on the x axis of the histograms.
> >
> > Now ignoring zero packing for a moment, if you do see this general similarity, then one way to find the angle of offset is by applying correlation, not to the images -but to the histograms themselves.
> >
> > Fast convolution using the FFT is particularly suited for this operation as bot FFT correlation, and the histograms themselves can be considered cylindrical, namely details that get shifted say left off of the histogram don't get lost - they reappear at the right side of the histogram, (remember we are on an angular scale) FFT correlation has exactly the same effect, and normally you have to work like blazes to remove the wrap around effect from your computation, here it is just what we want.
> >
> > The computation itself should be easy, simply FFT both histograms - remember this results in complex answers - don't throw away the phase, do complex arithmetic for the next phase which is
> >
> > multiply the spectrum of one histogram by the complex conjugate of the second spectrum (remember to .* not *) then find the real part of the inverse transform of the product.
> >
> > Look at this resulting vector, and hopefully you should find a peak value corresponding to the relative rotation angle between the two images. If you do, come back and we will discuss the zero packing to get better angular resolution, if not then we need to see the type of image you are working with.


bro dave, this is the code i do until currently..but i dunno how to find the max radian between two images


clear all;clc

im1=imread('image1.jpg');
im11=rgb2gray(im1);
im111=im2double(im11);
[dx,dy]=gradient(im111);
p1=atan2(dy,dx);
im1111=edge(p1,'canny');
bw1 = bwmorph(im1111,'dilate');
im11111=p1.*bw1;

%subplot(2,1,1);hist(im11111);
im6=hist(im11111);
%im66=gaussian(im6,5);
im66=fft(im6);


im=imread('image2.jpg');
im2=imresize(im,size(im11),'bicubic');
im22=rgb2gray(im2);
im222=im2double(im22);
[dx,dy]=gradient(im222);
p2=atan2(dy,dx);
im2222=edge(p2,'canny');
bw2 = bwmorph(im2222,'dilate');
im22222=p2.*bw2;

%subplot(2,1,2);hist(im22222);
im7=hist(im22222);
im77=conj(fft(im7));
im8=real(ifft(im66.*im77))
max_value=max(max(im8));
[max_angle1,]=find(im8==max_value)
hist(im8)


the max_angle i find is 1 every time..but when i check im8 value, got other value max than 1..so how i need to do the find the peak value?

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 5 Apr, 2009 10:42:02

Message: 22 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gra0s1$7d0$1@fred.mathworks.com>...
> "khoo" <jim_khoo@hotmail.com> wrote in message <gr96k1$cgv$1@fred.mathworks.com>...

> im8=real(ifft(im66.*im77))
> max_value=max(max(im8));
> [max_angle1,]=find(im8==max_value)
> hist(im8)
>
>
> the max_angle i find is 1 every time..but when i check im8 value, got other value max than 1..so how i need to do the find the peak value?

The correlation you should get from doing the Re(IFFT ....) is a one dimensional graph, whereas the max(max... you are applying is for a 2 dimensional matrix.

Try to plot what you have called im8 and see if you end up with a graph showing a peak.

When I try to access your image I get a message indicating it has been deleted or removed.

Sorry about your dead line, but it is Sunday here, and it is only just luck I caught this mail, as I have family responsibilities to deal with on a weekend.

Good Luck & Regards

Dave Robinson

Subject: How to calculate the angle between two images?

From: khoo

Date: 5 Apr, 2009 11:12:01

Message: 23 of 43

"Dave Robinson" <dave.robinson@somewhere.biz> wrote in message
> > the max_angle i find is 1 every time..but when i check im8 value, got other value max than 1..so how i need to do the find the peak value?
>
> The correlation you should get from doing the Re(IFFT ....) is a one dimensional graph, whereas the max(max... you are applying is for a 2 dimensional matrix.
>
> Try to plot what you have called im8 and see if you end up with a graph showing a peak.
>
> When I try to access your image I get a message indicating it has been deleted or removed.
>
> Sorry about your dead line, but it is Sunday here, and it is only just luck I caught this mail, as I have family responsibilities to deal with on a weekend.
>
> Good Luck & Regards
>
> Dave Robinson

i will try to figure out what you teach..but if u got free time, try help me out here..i will keep update my process at here..

this is the image :
http://i278.photobucket.com/albums/kk119/khoo011/image2.jpg
http://i278.photobucket.com/albums/kk119/khoo011/image1.jpg

Subject: How to calculate the angle between two images?

From: khoo

Date: 5 Apr, 2009 13:54:02

Message: 24 of 43

> > the max_angle i find is 1 every time..but when i check im8 value, got other value max than 1..so how i need to do the find the peak value?
>
> The correlation you should get from doing the Re(IFFT ....) is a one dimensional graph, whereas the max(max... you are applying is for a 2 dimensional matrix.
>
> Try to plot what you have called im8 and see if you end up with a graph showing a peak.
>
> When I try to access your image I get a message indicating it has been deleted or removed.
> > Sorry about your dead line, but it is Sunday here, and it is only just luck I caught this mail, as I have family responsibilities to deal with on a weekend.
>
> Good Luck & Regards

>
> Dave Robinson
Bro Dave, this is the code i do until currently...the max value i find is very large...i no sure i got do what wrong o not...but pls teach me how to find the correct angle..


clear all;clc

im1=imread('image1.jpg');
im11=rgb2gray(im1);
im111=im2double(im11);
[dx,dy]=gradient(im111);
p1=atan2(dy,dx);
im1111=edge(p1,'canny');
bw1 = bwmorph(im1111,'dilate');
im11111=p1.*bw1;
im6=hist(im11111);
im66=fft(im6);


im=imread('image2.jpg');
im2=imresize(im,size(im11),'bicubic');
im22=rgb2gray(im2);
im222=im2double(im22);
[dx,dy]=gradient(im222);
p2=atan2(dy,dx);
im2222=edge(p2,'canny');
bw2 = bwmorph(im2222,'dilate');
im22222=p2.*bw2;
%subplot(2,1,2);hist(im22222);
im7=(hist(im22222));
im77=conj(fft(im7));
im8=real(ifft(im66.*im77));
im9=transform(im8);
max_value=max(im9)
hist(im8);

below is the function i try to convert the image to 1 Dimensional matric
function b=transform(a)

[row,column]=size(a);
b=[];

for i=1:row
    for j=1:column
            b=[b a(i,j)];
    end
end

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 5 Apr, 2009 14:15:04

Message: 25 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <grad5q$cai$1@fred.mathworks.com>...


> below is the function i try to convert the image to 1 Dimensional matric
> function b=transform(a)
>
> [row,column]=size(a);
> b=[];
>
> for i=1:row
> for j=1:column
> b=[b a(i,j)];
> end
> end

Why on earth are you trying to convert an image into a 1 dimensional vector? you are correlating 2 histograms - histograms are essentially graphs - in your case angle along the x axis, number of pixels of that angle in y direction. If you have say 256 angles, then each bin represents (2*pi)/256 radians. Thus the histogram will be a vector of 256 numbers, each number representing the number of pixels of that angle.

I suggest you carefully read help hist(), doesn't that default to 10 bins - you will need to have much better resolution than that.

I have had a look at your images, and would have thought they were ideally suited to this methodology.

Regards

Dave Robinson

Subject: How to calculate the angle between two images?

From: khoo

Date: 5 Apr, 2009 17:59:01

Message: 26 of 43

> Why on earth are you trying to convert an image into a 1 dimensional vector? you are correlating 2 histograms - histograms are essentially graphs - in your case angle along the x axis, number of pixels of that angle in y direction. If you have say 256 angles, then each bin represents (2*pi)/256 radians. Thus the histogram will be a vector of 256 numbers, each number representing the number of pixels of that angle.
>
> I suggest you carefully read help hist(), doesn't that default to 10 bins - you will need to have much better resolution than that.
>
> I have had a look at your images, and would have thought they were ideally suited to this methodology.
>
> Regards
>
> Dave Robinson

bro Dave, i no understand what you explain..can u explain in more detail??i get the angle is very large..is around 250k..how can like that?? how i need to take the the max angle rotation from the corr graph??

Subject: How to calculate the angle between two images?

From: khoo

Date: 6 Apr, 2009 08:25:03

Message: 27 of 43

> Why on earth are you trying to convert an image into a 1 dimensional vector? you are correlating 2 histograms - histograms are essentially graphs - in your case angle along the x axis, number of pixels of that angle in y direction. If you have say 256 angles, then each bin represents (2*pi)/256 radians. Thus the histogram will be a vector of 256 numbers, each number representing the number of pixels of that angle.
>
> I suggest you carefully read help hist(), doesn't that default to 10 bins - you will need to have much better resolution than that.
>
> I have had a look at your images, and would have thought they were ideally suited to this methodology.
>
> Regards
>
> Dave Robinson

Bro Dave, this is my code currently...and i ady change the hist not to default 10 binsand set to 256..


clear all;clc

im1=imread('image1.jpg');
im11=rgb2gray(im1);
im111=im2double(im11);
[dx,dy]=gradient(im111);
p1=atan2(dy,dx);
im1111=edge(p1,'canny');
bw1 = bwmorph(im1111,'dilate');
im11111=p1.*bw1;
im6=hist(im11111(:),256);
im66=fft(im6);

im2=imread('image2.jpg');
im22=rgb2gray(im2);
im222=im2double(im22);
im333=zeros(size(im111));
im333(1:size(im2,1),1:size(im2,2))=im222;
[dx,dy]=gradient(im333);
p2=atan2(dy,dx);
im2222=edge(p2,'canny');
bw2 = bwmorph(im2222,'dilate');
im22222=p2.*bw2;
im7=hist(im22222(:),256);
im77=conj(fft(im7));
im99=im66.*im77;
im8=real(ifft(im66.*im77));
XXX=hist(im8(:),256);
stem(1:256,XXX, 'Marker', 'none');

and this is my new image2..
http://i278.photobucket.com/albums/kk119/khoo011/image2-1.jpg

just now i check all the histogram for the im11111 and im22222..all is correct..but once i fft the both histograms..when i inverse the FFT..the histogram cannot inverse and still stay in FFt mode...can you help me check my code got any problem ? and teach me how to fix the problem..so far i find the peak is at left side which is after FFT..

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 6 Apr, 2009 08:54:02

Message: 28 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <grce8v$8g$1@fred.mathworks.com>...
> > Why on earth are you trying to convert an image into a 1 dimensional vector? you are correlating 2 histograms - histograms are essentially graphs - in your case angle along the x axis, number of pixels of that angle in y direction. If you have say 256 angles, then each bin represents (2*pi)/256 radians. Thus the histogram will be a vector of 256 numbers, each number representing the number of pixels of that angle.
> >
> > I suggest you carefully read help hist(), doesn't that default to 10 bins - you will need to have much better resolution than that.
> >
> > I have had a look at your images, and would have thought they were ideally suited to this methodology.
> >
> > Regards
> >
> > Dave Robinson
>
> Bro Dave, this is my code currently...and i ady change the hist not to default 10 binsand set to 256..
>
>
> clear all;clc
>
> im1=imread('image1.jpg');
> im11=rgb2gray(im1);
> im111=im2double(im11);
> [dx,dy]=gradient(im111);
> p1=atan2(dy,dx);
> im1111=edge(p1,'canny');
> bw1 = bwmorph(im1111,'dilate');
> im11111=p1.*bw1;
> im6=hist(im11111(:),256);
> im66=fft(im6);
>
> im2=imread('image2.jpg');
> im22=rgb2gray(im2);
> im222=im2double(im22);
> im333=zeros(size(im111));
> im333(1:size(im2,1),1:size(im2,2))=im222;
> [dx,dy]=gradient(im333);
> p2=atan2(dy,dx);
> im2222=edge(p2,'canny');
> bw2 = bwmorph(im2222,'dilate');
> im22222=p2.*bw2;
> im7=hist(im22222(:),256);
> im77=conj(fft(im7));
> im99=im66.*im77;
> im8=real(ifft(im66.*im77));
> XXX=hist(im8(:),256);
> stem(1:256,XXX, 'Marker', 'none');
>
> and this is my new image2..
> http://i278.photobucket.com/albums/kk119/khoo011/image2-1.jpg
>
> just now i check all the histogram for the im11111 and im22222..all is correct..but once i fft the both histograms..when i inverse the FFT..the histogram cannot inverse and still stay in FFt mode...can you help me check my code got any problem ? and teach me how to fix the problem..so far i find the peak is at left side which is after FFT..

Why are you histogramming im8? which should be the correlation that you are looking for - just plot im8 and if everything else is correct then you should have one dimensional distribution reaching a peak at the position corresponding to the relative rotation angle of the 2 images.

Regards

Dave Robinson

Subject: How to calculate the angle between two images?

From: khoo

Date: 6 Apr, 2009 11:58:01

Message: 29 of 43

 > Why are you histogramming im8? which should be the correlation that you are looking for - just plot im8 and if everything else is correct then you should have one dimensional distribution reaching a peak at the position corresponding to the relative rotation angle of the 2 images.
>
> Regards
>
> Dave Robinson

Bro Dave, this is the im8 graph after i plot out..but why the peak will at left side?? izit my inverse FFt do wrong?

http://i278.photobucket.com/albums/kk119/khoo011/grapg.jpg

and this is the part of code after i change..jz the below part of the code

im7=hist(im22222(:),256);
im77=conj(fft(im7));
im8=real(ifft(im66.*im77));
plot(im8)

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 6 Apr, 2009 12:15:05

Message: 30 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <grcqo9$17e$1@fred.mathworks.com>...
> > Why are you histogramming im8? which should be the correlation that you are looking for - just plot im8 and if everything else is correct then you should have one dimensional distribution reaching a peak at the position corresponding to the relative rotation angle of the 2 images.
> >
> > Regards
> >
> > Dave Robinson
>
> Bro Dave, this is the im8 graph after i plot out..but why the peak will at left side?? izit my inverse FFt do wrong?
>
> http://i278.photobucket.com/albums/kk119/khoo011/grapg.jpg
>
> and this is the part of code after i change..jz the below part of the code
>
> im7=hist(im22222(:),256);
> im77=conj(fft(im7));
> im8=real(ifft(im66.*im77));
> plot(im8)

Had you considered that the results just might be right? Think about it - when you rotated the image, do the the actual image boundaries rotate?. You had some almost circular details toward the centre of the image, rotating a circle will generate exactly the same angle image irrespective of the amount of twist. If your image contains aliasing (digital stepping in the image), these will always generate detail usually at 0 and 90 degrees. Remember that your x scale needs to be rescaled such that 0 -> 256 actually maps to 0 ->360 degrees.

Meanwhile do the other spikes bear any relationship to the actual rotation angle between your images?

If the zero degree spike offends you why not set it to zero - if you know that the two images do have a valid rotation

Regards

Dave Robinson

Subject: How to calculate the angle between two images?

From: khoo

Date: 6 Apr, 2009 13:51:01

Message: 31 of 43

> Had you considered that the results just might be right? Think about it - when you rotated the image, do the the actual image boundaries rotate?. You had some almost circular details toward the centre of the image, rotating a circle will generate exactly the same angle image irrespective of the amount of twist. If your image contains aliasing (digital stepping in the image), these will always generate detail usually at 0 and 90 degrees. Remember that your x scale needs to be rescaled such that 0 -> 256 actually maps to 0 ->360 degrees.
>
> Meanwhile do the other spikes bear any relationship to the actual rotation angle between your images?
>
> If the zero degree spike offends you why not set it to zero - if you know that the two images do have a valid rotation
>
> Regards
>
> Dave Robinson

Bro Dave, when i rotate the images, i don't think the actual image boundaries will rotate...ABout the other spikes bear, i no sure about it but how to set the zero degree spike to zero??

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 6 Apr, 2009 14:32:01

Message: 32 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <grd1c5$mk8$1@fred.mathworks.com>...
> > Had you considered that the results just might be right? Think about it - when you rotated the image, do the the actual image boundaries rotate?. You had some almost circular details toward the centre of the image, rotating a circle will generate exactly the same angle image irrespective of the amount of twist. If your image contains aliasing (digital stepping in the image), these will always generate detail usually at 0 and 90 degrees. Remember that your x scale needs to be rescaled such that 0 -> 256 actually maps to 0 ->360 degrees.
> >
> > Meanwhile do the other spikes bear any relationship to the actual rotation angle between your images?
> >
> > If the zero degree spike offends you why not set it to zero - if you know that the two images do have a valid rotation
> >
> > Regards
> >
> > Dave Robinson
>
> Bro Dave, when i rotate the images, i don't think the actual image boundaries will rotate...ABout the other spikes bear, i no sure about it but how to set the zero degree spike to zero??

im8(0) = 0;

Maybe

Regards

Dave Robinson

Subject: How to calculate the angle between two images?

From: Matt

Date: 6 Apr, 2009 14:47:01

Message: 33 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <gr4ten$oks$1@fred.mathworks.com>...
> Hi all, currently i face the problem to find the rotation angle...i got one image is rotate in unknown angle compare with the original image, but i not have idea how to calculate the angle cause the angle is a variable angle..can anyone senior teach me here,...need it urgently...and i quite noob in image processing..

Another approach is to use the radon() function (assuming you have the image proc toolbox) of both images. The rotations of the 2 images will manifest as a shift in their radon projections. You can then use correlation techniques to compute the shift. Because this employs integration operations instead of differentiation, it might be more robust to noise than the gradient-angle technique.

Subject: How to calculate the angle between two images?

From: khoo

Date: 6 Apr, 2009 14:48:01

Message: 34 of 43

> > Bro Dave, when i rotate the images, i don't think the actual image boundaries will rotate...ABout the other spikes bear, i no sure about it but how to set the zero degree spike to zero??
>
> im8(0) = 0;
>
> Maybe
>
> Regards
>
> Dave Robinson

Bro Dave, still cannot...this is my currently code after i set zero spike to zero...but the graph still like the graph i get jz now..


clear all;clc

im1=imread('image1.jpg');
im11=rgb2gray(im1);
im111=im2double(im11);
[dx,dy]=gradient(im111);
p1=atan2(dy,dx);
im1111=edge(p1,'canny');
bw1 = bwmorph(im1111,'dilate');
im11111=p1.*bw1;
im6=hist(im11111(:),256);
im66=fft(im6);

im2=imread('image2.jpg');
im22=rgb2gray(im2);
im222=im2double(im22);
im333=zeros(size(im111));
im333(1:size(im2,1),1:size(im2,2))=im222;
[dx,dy]=gradient(im333);
p2=atan2(dy,dx);
im2222=edge(p2,'canny');
bw2 = bwmorph(im2222,'dilate');
im22222=p2.*bw2;
im7=hist(im22222(:),256);
im77=conj(fft(im7));
im8=real(ifft(im66.*im77));
plot(im8)
im8(1)=0;

izit my FFT there got problem?? cause normally FFT graph, the peak will at left side..but after IFFT the graph should be back like before FFT..but my graph's peak still at left side..can help me check my code got wrong at anywhere??

Subject: How to calculate the angle between two images?

From: khoo

Date: 6 Apr, 2009 15:14:01

Message: 35 of 43

> > Bro Dave, when i rotate the images, i don't think the actual image boundaries will rotate...ABout the other spikes bear, i no sure about it but how to set the zero degree spike to zero??
>
> im8(0) = 0;
>
> Maybe
>
> Regards
>
> Dave Robinson

Bro Dave, i know i wrong where ady..this is my update code...

clear all;clc
im1=imread('image1.jpg');
im11=rgb2gray(im1);
im111=im2double(im11);
[dx,dy]=gradient(im111);
p1=atan2(dy,dx);
im1111=edge(p1,'canny');
bw1 = bwmorph(im1111,'dilate');
im11111=p1.*bw1;
im6=hist(im11111(:),256);
im66=fft(im6);

im2=imread('image2.jpg');
im22=rgb2gray(im2);
im222=im2double(im22);
im333=zeros(size(im111));
im333(1:size(im2,1),1:size(im2,2))=im222;
[dx,dy]=gradient(im333);
p2=atan2(dy,dx);
im2222=edge(p2,'canny');
bw2 = bwmorph(im2222,'dilate');
im22222=p2.*bw2;
im7=hist(im22222(:),256);
im77=conj(fft(im7));
im8=real(ifft(im66.*im77));
im8(1)=0;
plot(im8)

by the way, how to get the get the peak by checking the graph?? izit the peak there state the relative rotation angle for two image?? and how to convert the radian to degree??

Subject: How to calculate the angle between two images?

From: khoo

Date: 6 Apr, 2009 15:16:02

Message: 36 of 43

"Matt " <xys@whatever.com> wrote in message <grd4l5$m4s$1@fred.mathworks.com>...
> "khoo" <jim_khoo@hotmail.com> wrote in message <gr4ten$oks$1@fred.mathworks.com>...
> > Hi all, currently i face the problem to find the rotation angle...i got one image is rotate in unknown angle compare with the original image, but i not have idea how to calculate the angle cause the angle is a variable angle..can anyone senior teach me here,...need it urgently...and i quite noob in image processing..
>
> Another approach is to use the radon() function (assuming you have the image proc toolbox) of both images. The rotations of the 2 images will manifest as a shift in their radon projections. You can then use correlation techniques to compute the shift. Because this employs integration operations instead of differentiation, it might be more robust to noise than the gradient-angle technique.

the radon() function no need theta to to work on??

Subject: How to calculate the angle between two images?

From: khoo

Date: 6 Apr, 2009 17:02:02

Message: 37 of 43

> > > Had you considered that the results just might be right? Think about it - when you rotated the image, do the the actual image boundaries rotate?. You had some almost circular details toward the centre of the image, rotating a circle will generate exactly the same angle image irrespective of the amount of twist. If your image contains aliasing (digital stepping in the image), these will always generate detail usually at 0 and 90 degrees. Remember that your x scale needs to be rescaled such that 0 -> 256 actually maps to 0 ->360 degrees.
> > >
> > > Meanwhile do the other spikes bear any relationship to the actual rotation angle between your images?
> > >
> > > If the zero degree spike offends you why not set it to zero - if you know that the two images do have a valid rotation
> > >
> > > Regards
> > >
> > > Dave Robinson
> >

Bro Dave, i think i ady get the graph i want, but how to take out peak point from the graph?? and how to convert the radian in the graph to the degree??please continue teach me, i dunno how to convert and take the peak radian out....and this is the graph i got...

http://i278.photobucket.com/albums/kk119/khoo011/graph2.jpg

Subject: How to calculate the angle between two images?

From: Matt

Date: 6 Apr, 2009 17:30:18

Message: 38 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <grd6bi$bsi$1@fred.mathworks.com>...
> "Matt " <xys@whatever.com> wrote in message <grd4l5$m4s$1@fred.mathworks.com>...
> > "khoo" <jim_khoo@hotmail.com> wrote in message <gr4ten$oks$1@fred.mathworks.com>...
> > > Hi all, currently i face the problem to find the rotation angle...i got one image is rotate in unknown angle compare with the original image, but i not have idea how to calculate the angle cause the angle is a variable angle..can anyone senior teach me here,...need it urgently...and i quite noob in image processing..
> >
> > Another approach is to use the radon() function (assuming you have the image proc toolbox) of both images. The rotations of the 2 images will manifest as a shift in their radon projections. You can then use correlation techniques to compute the shift. Because this employs integration operations instead of differentiation, it might be more robust to noise than the gradient-angle technique.
>
> the radon() function no need theta to to work on??

Yes. You would choose RADON's vector argument THETA to suit the range and sampling fineness of rotation angles that you want.

 

Subject: How to calculate the angle between two images?

From: khoo

Date: 7 Apr, 2009 07:28:03

Message: 39 of 43

Bro Dave, i ady get the radians for the images..but now i got one problem show in my program..i use several images with different angles to test the code..but all i get is same radian..how come will like this?? and this is my code currently:


clear all;clc

im1=imread('image1.jpg');
im11=rgb2gray(im1);
im111=im2double(im11);
[dx,dy]=gradient(im111);
p1=atan2(dy,dx);
im1111=edge(p1,'canny');
bw1 = bwmorph(im1111,'dilate');
im11111=p1.*bw1;
%subplot(2,1,1);hist(im11111);
im6=hist(im11111(:),256);
im66=fft(im6);


im2=imread('image2.jpg');
im22=rgb2gray(im2);
im222=im2double(im22);
im333=zeros(size(im111));
im333(1:size(im2,1),1:size(im2,2))=im222;
[dx,dy]=gradient(im333);
p2=atan2(dy,dx);
im2222=edge(p2,'canny');
bw2 = bwmorph(im2222,'dilate');
im22222=p2.*bw2;
%subplot(2,1,2);hist(im22222);
im7=hist(im22222(:),256);
im77=conj(fft(im7));
im8=real(ifft(im66.*im77));
im8(1)=0;
plot(im8);
[y,x]=max(im8);
rel_deg=((x*(2*pi/256))*180)/pi;

if rel_deg>180;
    new_rel_deg=rel_deg-180
else
    new_rel_deg=rel_deg
end

pls help me..and what is zero packing??izit i need to use zero packing to get the correct angle??

Subject: How to calculate the angle between two images?

From: khoo

Date: 7 Apr, 2009 13:48:01

Message: 40 of 43

Bro Dave, pls continue to teach me..i dun wan give up with this method in half way...now only you can help me..i already email the mfile to your email...i now dunno my code wrong where and izit i get the correct final graph or not?? and why different angle image still show the same radian..

Subject: How to calculate the angle between two images?

From: Dave Robinson

Date: 7 Apr, 2009 16:11:01

Message: 41 of 43

"khoo" <jim_khoo@hotmail.com> wrote in message <grflih$arh$1@fred.mathworks.com>...
> Bro Dave, pls continue to teach me..i dun wan give up with this method in half way...now only you can help me..i already email the mfile to your email...i now dunno my code wrong where and izit i get the correct final graph or not?? and why different angle image still show the same radian..

There is good news and there is bad news.

The good news is I have run your two images through my own software which effectively does the process that you are trying to do, and as I had originally thought, the information regarding your rotation angle is there good and clear. The problem you are having is due to image edge, and aliasing problems. Look at your two histograms im6 & im7 by plotting them, what you should see is a nice relatively smooth almost Gaussian curve, which peaks in a totally different place in both graphs this is the information regarding the rotation. However in addition you have some very large spikes occurring in exactly the same position on both graphs ( at 0 and 90 degrees etc.). When you do your FFT based correlation, these spikes give you the 'same radian' reading that you are observing. You need to remove them from your original histograms before you do your FFT correlation.

Think carefully how you do this, you cannot simply set those angles to zero as this will will introduce spikes going in the other direction, towards zero, not towards a bigger number. You essentially need to replace them with the 'average' value of the smooth curve around them, so it looks continuous.

I can make a suggestion that you do some research on a 1 dimensional median filter, which should do exactly what you want. Don't try and smooth the curve - that just don't work.

Unfortunately spending so much time with this thread I am getting behind with my own work. I am afraid I am going to have to leave you to do the final steps (don't worry about zero packing - that is just a cosmetic step to increase the resolution of the angle measurement). You now know the data you are seeking is there, all you need is the removal of the spikes and away you go. I urge you to look at those histograms. As far as I can see your code is more or less OK.

Regards

Dave Robinson

Subject: How to calculate the angle between two images?

From: Jeff

Date: 9 Dec, 2012 04:02:08

Message: 42 of 43

I know this is an old topic, but Dave, are you still reading? Anyone really. Dave spoke about zero packing and I would like to know more, but my online searches have not proven fruitful. I am performing the same function as the original poster, finding the rotation angle between images. How does zero packing increase angular resolution and where can I learn how to implement it?
Thanks!

Subject: How to calculate the angle between two images?

From: Michael Meyers

Date: 25 Jan, 2013 15:23:07

Message: 43 of 43

"Jeff" wrote in message <ka12g0$eu4$1@newscl01ah.mathworks.com>...
> I know this is an old topic, but Dave, are you still reading? Anyone really. Dave spoke about zero packing and I would like to know more, but my online searches have not proven fruitful. I am performing the same function as the original poster, finding the rotation angle between images. How does zero packing increase angular resolution and where can I learn how to implement it?
> Thanks!

Look up padding in frequency domain equals interpolation in spatial domain. By padding the frequency domain image properly, when you do the ifft, it will interpolate new values. The increase in the size of the final image provides increased resolution.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us