Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Image rotation detection probelm
Replies: 32   Last Post: Aug 8, 2013 10:46 PM

 Messages: [ Previous | Next ]
 Darren g Posts: 10 Registered: 11/14/12
Re: Image rotation detection probelm
Posted: Nov 26, 2012 1:38 AM

Hi Phillip,

Many thanks. Yes. My code is now working after adopting your suggestion for finding the column and row in which the maximum peak intensity is found. For finding the centre of mass of the image to measure the angle of rotation smaller than one degree I am trying the regionprobs but not yet found a way on how to integrate this within my code. Is this the one you mentioned for finding to centre of mass or any other easy way of doing? Your help for this is much appreciated.

Darren

"Phillip" wrote in message <k8hjkn\$69s\$1@newscl01ah.mathworks.com>...
> Hi Darren,
>
> Try something like
>
> correlationMatrix = xcorr2(image1,image2);
> figure; imagesc(correlationMatrix)
>
> You will see a bright patch on a dark background. If the 2 input images were the same, the patch would be centered in the array. If the 2 input images are displaced relative to each other, the bright patch will be displaced by that amount. To quantify the amount, you can start with:
>
> [~,maxColumn] = max(max(correlationMatrix));
> [~,maxRow] = max(correlationMatrix(:,maxColumn));
>
> This will tell you the column and row that the brightest point of the image was found in. You can then compare this to the (half) size of the whole array to quantify the displacement.
>
> If you want a more precise (sub-pixel) estimate of the displacement, you can look into calculating the centre of mass of the bright patch instead of just finding the peak.
>
> "Darren g" wrote in message <k8h0dd\$4uj\$1@newscl01ah.mathworks.com>...

> > Hi Phillip,
> >
> > Many thanks. Yes. It is now working with your suggession. The problem I have encountered with those optional arguments is that I tried without quote as below
> > pim = polartrans(im, nrad, ntheta,cx,cy, log, valid);
> >
> > Now it is working with quote for the 'log' and 'valid' as below.
> > pim = polartrans(im, nrad, ntheta,cx,cy, 'log', 'valid');
> > So far got to work up to transforming the two images to polatranform and can clearly see a shift on the two transformed images.
> > I have not understand the xcorre2 for cross-correlation that you mentioned earlier and the multiplication to calculate the angle of rotation and would appreciate any suggestion or part of the code if you can remember now.
> >
> > Thanks.
> > Darren
> >
> >
> > "Phillip" wrote in message <k8em3u\$eut\$1@newscl01ah.mathworks.com>...

> > > Hi Darren,
> > >
> > > When you have optional arguments like that, you have to omit them if you don't want to use them. Otherwise the function thinks you are trying to specify them, but because you haven't defined cx, cy, linlog, shape it gives an error. i.e. you need to change the call to polartrans to:
> > >
> > > pim = polartrans(im, nrad, ntheta);
> > >
> > >
> > > "Darren g" wrote in message <k8duk1\$kjn\$1@newscl01ah.mathworks.com>...

> > > > Hi Phillip,
> > > >
> > > > Thanks for the detailed information. I am not an expert in matlab but currently learning for this image rotation measurement. I have tried the polartrans.m function. But, when calling this function what values I need to supply within the code. I tried nrad=256 and ntheta=360, are the width of my image and a full range of theta. My understanding is that the other parameters cx, cy, linlog, shape should take some default values but when running the code it show error "??? Undefined function or variable 'cx'". Here is the code I tried for polar transform.
> > > >
> > > > a=1;
> > > > image = strcat('',num2str(a,'%04d'),'.png');
> > > > im = imread(image);
> > > > figure(1);
> > > > imagesc(im);
> > > > axis image;
> > > > nrad = 256; ntheta = 360;
> > > > pim = polartrans(im, nrad, ntheta, cx, cy, linlog, shape);
> > > > figure(2);
> > > > imagesc(pim);title('Polar transform of image');
> > > > axis image;
> > > >
> > > > Any error in this code?
> > > >
> > > > Thanks in advance.
> > > > Darren
> > > >
> > > >
> > > > "Phillip" wrote in message <k81qc1\$app\$1@newscl01ah.mathworks.com>...

> > > > > Hi Darren,
> > > > >
> > > > > From the link I gave above, I used the "polartrans.m" file.
> > > > >
> > > > > Once you have the two polar images, do a plain X/Y (translation) cross-correlation. If the images are just rotated and not stretched, the peak of the cross-correlation function will be displaced by some amount (I forget in which direction, but should be obvious when you display the images). Each division in the image is worth 2*pi/ntheta angular units (where ntheta is an argument that you give to the polartrans function).
> > > > >
> > > > > If you haven't done a cross-correlation before, have a look at xcorr2 (I think this requires the signal processing toolbox). Play around with images that just require a translation - once you have that figured out, do the same thing but using your polar images, and multiply by the amount given above to determine the angle.
> > > > >
> > > > > NB: In R2012 onwards the Image Processing toolbox has an "imregister" function that can apparently do all sorts of registrations, including rotations. I find it works well for some kinds of images but not others; you might want to give it a shot.
> > > > >
> > > > > "Darren g" wrote in message <k806kl\$s5m\$1@newscl01ah.mathworks.com>...

> > > > > > Hi Phillip,
> > > > > > I am also trying to measure the image rotation for very small angle (fractional as well) noisy images. Could you please let me know the function you used from the link you provided. Also, how did you calculate the angle of rotation from the polar transform. I have used polar transform function from the one got from the internet but do not know how to proceed for the angular measurement after polar transforming the two images.
> > > > > > Any help would be much appreciated.
> > > > > > Thanks.
> > > > > > Darren
> > > > > >
> > > > > > "Phillip" wrote in message <ierba4\$kko\$1@fred.mathworks.com>...

> > > > > > > > while trying this code i got this error....can you tell me WHY ?????
> > > > > > > >
> > > > > > > > ??? Error using ==> times
> > > > > > > > Matrix dimensions must agree.
> > > > > > > >

> > > > > > > > > for ii = 1:length(thetas)
> > > > > > > > > b = P2(:,ii);
> > > > > > > > > this_correlation = abs(ifft(P1.*conj(b)));
> > > > > > > > > angle_scores(ii) = max(max(this_correlation));
> > > > > > > > > end

> > > > > > >
> > > > > > > Make sure that P1 is a column vector and P2 has the same number of rows as P1.
> > > > > > >
> > > > > > > However, I've changed the way I solve this problem now. Two points to note for anybody stumbling on this:
> > > > > > >
> > > > > > > 1) What I said about only needing one radon projection for the reference might be true in theory, but I found in practice with noisy data that this was not so good. I've actually abandoned the radon approach.
> > > > > > >
> > > > > > > 2) I now solve my rotation problem in an entirely different way. I'm not sure how generally applicable this solution is, but it worked well for my application (very large images, only small angles of rotation possible, very precise estimate of rotation necessary).
> > > > > > >
> > > > > > > I find the polar transforms of the reference and acquired images, and then do a typical registration between these using cross-correlation. The shift required in the theta dimension tells you the rotation required (the other dimension has radius info which I don't use).
> > > > > > > I got a Matlab function for creating a polar transform from this page:
> > > > > > >
> > > > > > > http://www.csse.uwa.edu.au/~pk/Research/MatlabFns/
> > > > > > >
> > > > > > > This function lets you specify the sampling in both the theta and radial dimensions. I upsample quite a bit in the theta dimension (better precision on the angle estimate) and downsample in the radial dimension (to keep memory costs down - my images are quite large).
> > > > > > >
> > > > > > > Hope this approach is useful to others.

Date Subject Author
6/19/09 Matt
8/2/10 Phillip
12/21/10 chee ali
12/21/10 Phillip
12/21/10 Matt J
12/21/10 Phillip
11/14/12 Darren g
11/14/12 Phillip
11/19/12 Darren g
11/19/12 Phillip
11/20/12 Darren g
11/20/12 Phillip
11/26/12 Darren g
11/26/12 Phillip
11/27/12 Darren g
11/28/12 Phillip
11/29/12 Darren g
12/2/12 Phillip
12/3/12 Darren g
12/4/12 Phillip
12/11/12 Darren g
12/11/12 Phillip
12/12/12 Darren g
12/12/12 Steven Lord
2/6/13 Darren g
12/20/12 Phillip
12/20/12 Kev
12/20/12 Kev
7/7/13 xmlfor
8/8/13 jacaboo