
Re: How to calculate the angle between two images?
Posted:
Apr 4, 2009 1:49 PM


"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

