
Re: Image rotation detection probelm
Posted:
Dec 3, 2012 7:02 PM


Hi Phillip,
I think the problem is in the Angle of rotation calculation equation but I could not figure out how to change it. Here is the code I used to check with the code you provided to introducing the image shift.
clear all; image1 = imread('0001.png');
xShift = 0; yShift = 20; translatingElement = translate(strel(1), [yShift xShift]); image2 = imdilate(image1,translatingElement);
image1 = image1(1+yShift:endyShift,1+xShift:endxShift); % get rid of Infs from the imdilate function image2 = image2(1+yShift:endyShift,1+xShift:endxShift);
nrad = 128; ntheta = 360; cx = (283/2)+0.5; cy = (290/2)+0.5; % cx & cy for a 283x290 pixels image
pim1 = polartrans(image1, nrad, ntheta, cx, cy, 'linear', 'valid');
pim2 = polartrans(image2, nrad, ntheta, cx, cy, 'linear', 'valid');
pim1 = pim1  mean(mean(pim1)); pim2 = pim2  mean(mean(pim2));
cc_Matrix = xcorr2(pim1, pim2); figure (1); imagesc(cc_Matrix); axis image; [max_cc_Matrix, maxColumn] = max(max(cc_Matrix)) [max_cc_Matrix, maxRow] = max(cc_Matrix(:, maxColumn))
roiDiam = 20; cc1_Matrix = cc_Matrix(maxRowroiDiam/2:maxRow+roiDiam/2, maxColumnroiDiam/2:maxColumn+roiDiam/2); figure (2); imagesc(cc1_Matrix); axis image; [a,b] = find_com(cc1_Matrix) % calling function
Angle_of_rotation = (a + maxColumn  length(cc1_Matrix)/2) * 360/ntheta
Could you pls have a look of this with any of your random image?
Thanks. Darren
"Phillip" wrote in message <k9gnhk$d5h$1@newscl01ah.mathworks.com>... > Hi Darren, > > In regards to the first part, the mid point for a 255 x 719 matrix is 127, 359. So that would imply some sort of error. Try subtracting the mean of both images just before the call to xcorr2, I think this is necessary for the correlation to work. i.e. > > pim1 = pim1  mean(mean(pim1)); > pim2 = pim2  mean(mean(pim2)); > > You might want to test your correlation routine out on a simple translation first. eg the code below will give you a 20 pixel vertical translation > > xShift = 0; > yShift = 20; > translatingElement = translate(strel(1), [yShift xShift]); > image2 = imdilate(image1,translatingElement); > > image1 = image1(1+yShift:endyShift,1+xShift:endxShift); % get rid of Infs from the imdilate function > image2 = image2(1+yShift:endyShift,1+xShift:endxShift); > > For your other questions: > > Would you mean maxColumn and maxRow by rowCentre and colCentre in the following centre of mass (COM) calculation? Is roiDiam is the width of the square area considered for this COM calculation? > > Yes. > > > Is xSize is the diameter of the cc_Matrix in the following? > > > > Angle_of_rotation = (a + colCentre  xSize/2) * 360/ntheta > > Yes. > > Hope this helps. > > Phillip > > "Darren g" wrote in message <k99436$9u7$1@newscl01ah.mathworks.com>... > > Hi Phillip, > > > > I am completely messed up with the calculation now. Shall we forget about the centre of mass calculation for a moment and find the angle of rotation for one degree precision. > > > > The test images I used were 283x290 pixels size and one was deliberately rotated with respect to other by 1.4 degrees. > > > > I used the polartrans function with the following values for these images nrad = 128; > > > > ntheta = 360; cx = (283/2)+0.5; cy = (290/2)+0.5; > > pim1 = polartrans(image1, nrad, ntheta, cx, cy, 'linear', 'valid'); > > pim2 = polartrans(image2, nrad, ntheta, cx, cy, 'linear', 'valid'); > > > > and > > cc_Matrix = xcorr2(pim1, pim2); > > > > Now the size(cc_Matrix) gave > > ans= > > 255 719 > > When I use the following for calculating the maximum intensity and its corresponding column and row it gave > > > > maxColumn=361 and > > maxRow=128 > > > > Since there is no translation and the second image is only rotated with respect to the first (by 1.4 degrees in this case), for the angular rotation calculation we only need the column value in which the maximum intensity is found which is 361 (Is it correct?). > > > > [max_cc_Matrix, maxColumn] = max(max(cc_Matrix)) > > [max_cc_Matrix, maxRow] = max(cc_Matrix(:, maxColumn)) > > > > So the Angle of rotation = maxColumncentre of the cc_matrix column > > =361360 > > Which resulted in the 1 degree. > > Is anything wrong with this calculation? > > > > Now move on to the centre of mass calculation. > > > > Would you mean maxColumn and maxRow by rowCentre and colCentre in the following centre of mass (COM) calculation? Is roiDiam is the width of the square area considered for this COM calculation? > > > > cc1_Matrix = cc_Matrix(rowCentre  roiDiam/2 : rowCentre + roiDiam/2, colCentre  roiDiam/2 : colCentre + roiDiam/2); > > > > Is xSize is the diameter of the cc_Matrix in the following? > > > > Angle_of_rotation = (a + colCentre  xSize/2) * 360/ntheta > > > > Sorry for these lengthy explanation and clarification but I hope this will give an idea to find out the error in my thinking. > > > > Thanks. > > Darren > >

