Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Image Registration,met a trouble in transform the dft to log polar

Image Registration Using Fourier Transform and Phase Correlation

I know that opencv has a function to get the shift value using phase correlation ,but can't get the scale variation and Angle of Rotation .

C++: Point2d phaseCorrelate(InputArray src1, InputArray src2, InputArray window=noArray())

when I tried to modify the code . I met a trouble in transform the dft to log polar.


here is the original code I tried to modify


\OpenCV2.4.3\opencv\modules\imgproc\src\phasecorr.cpp

cv::Point2d cv::phaseCorrelateRes(InputArray _src1, InputArray _src2, InputArray _window, double* response) { Mat src1 = _src1.getMat(); Mat src2 = _src2.getMat(); Mat window = _window.getMat();

CV_Assert( src1.type() == src2.type());
CV_Assert( src1.type() == CV_32FC1 || src1.type() == CV_64FC1 );
CV_Assert( src1.size == src2.size);

if(!window.empty())
{
    CV_Assert( src1.type() == window.type());
    CV_Assert( src1.size == window.size);
}

int M = getOptimalDFTSize(src1.rows);
int N = getOptimalDFTSize(src1.cols);

Mat padded1, padded2, paddedWin;

if(M != src1.rows || N != src1.cols)
{
    copyMakeBorder(src1, padded1, 0, M - src1.rows, 0, N - src1.cols, BORDER_CONSTANT, Scalar::all(0));
    copyMakeBorder(src2, padded2, 0, M - src2.rows, 0, N - src2.cols, BORDER_CONSTANT, Scalar::all(0));

    if(!window.empty())
    {
        copyMakeBorder(window, paddedWin, 0, M - window.rows, 0, N - window.cols, BORDER_CONSTANT, Scalar::all(0));
    }
}
else
{
    padded1 = src1;
    padded2 = src2;
    paddedWin = window;
}

Mat FFT1, FFT2, P, Pm, C;

// perform window multiplication if available
if(!paddedWin.empty())
{
    // apply window to both images before proceeding...
    multiply(paddedWin, padded1, padded1);
    multiply(paddedWin, padded2, padded2);
}

// execute phase correlation equation
// Reference: http://en.wikipedia.org/wiki/Phase_correlation
dft(padded1, FFT1, DFT_REAL_OUTPUT);
dft(padded2, FFT2, DFT_REAL_OUTPUT);

mulSpectrums(FFT1, FFT2, P, 0, true);

magSpectrums(P, Pm);
divSpectrums(P, Pm, C, 0, false); // FF* / |FF*| (phase correlation equation completed here...)

idft(C, C); // gives us the nice peak shift location...

fftShift(C); // shift the energy to the center of the frame.

// locate the highest peak
Point peakLoc;
minMaxLoc(C, NULL, NULL, NULL, &peakLoc);

// get the phase shift with sub-pixel accuracy, 5x5 window seems about right here...
Point2d t;
t = weightedCentroid(C, peakLoc, Size(5, 5), response);

// max response is M*N (not exactly, might be slightly larger due to rounding errors)
if(response)
    *response /= M*N;

// adjust shift relative to image center...
Point2d center((double)padded1.cols / 2.0, (double)padded1.rows / 2.0);

return (center - t);

}


after the dft(padded1, FFT1, DFT_REAL_OUTPUT) , I tried cvLogPolar ,but I can't get the right results as I try in MATLAB.

void cvLogPolar(const CvArr* src, CvArr* dst, CvPoint2D32f center, double M, int flags) (C function, in Geometric Image Transformations)

here is the Proposed Algorithm

(i) First two input images IR (x, y) and IS(x, y) (reference and sense) are loaded.

(ii) The approximate value of scale between the images is estimated, if there is a scale between the images then downscale the sense image by the factor of 2. After down scaling, apply zero padding to downscaled sense image IDS(x,y) to keep the size of both (reference and sense) images same.

(iii) 2D-FFT of both the images (reference image and downscaled sense image) is calculated.This step will give two 2-D arrays of FFT co-efficient FR and FSD one for the reference image and other for the sense image. (iv) The magnitude of FFT co-efficient (the arrays as obtained in step-iii.) with a high pass emphasis filter is convolved. Now it is required that modified arrays (as obtained after step iv) be arranged in the log polar space so as to calculate the scale and rotation changes between the images.

(v) The modified FFT arrays to log polar space ( ρ,θ) is transformed. (a) Define a log space according to required scale of log axis. (b) Define a theta axis between 00 and 3600 with interval based on image size. Since the axes of the log polar space are now defined, we need to find the equivalent Cartesian coordinates so that we can interpolate the modified FFT arrays, (from step(iv)) to convert the image to log polar domain. (c) Based on the above two ( ρ,θ) parameters convert the polar coordinates to Cartesian coordinates. (d) Interpolate the image accordingly. After this step there will be two arrays in which the FFT coefficients of the images will be arranged in log polar domain.

can I do that with OpenCV?

Thank you. Regards, Pei.

reference Jignesh N Sarvaiya,Suprava Patnaik,Kajal Kothari,Image Registration Using Log Polar Transform and Phase Correlation to Recover Higher Scale