使用OpenCV的2个移位图像的C ++互相关

时间:2018-07-15 11:14:34

标签: c++ opencv

我想对2个移位图像进行互相关。通常,我会这样: -加载2张图片 -使用这2张图片进行dft -使用mulSpectrum(opencv)将这些图像彼此相乘 -对乘法结果求反dft -显示结果-结果图像中必须有频率偏移,即真实图像的偏移。 我已经用openCV做到了:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>


using namespace std;
using namespace cv;

void fft_shift(Mat &I, Mat &magI) //shift the origin to the center of the image (taken from OpenCV example of dft)
{
    Mat padded;                            //expand input image to optimal size
    int m = getOptimalDFTSize(I.rows);
    int n = getOptimalDFTSize(I.cols); // on the border add zero values
    copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));

    Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };
    Mat complexI;
    merge(planes, 2, complexI);         // Add to the expanded another plane with zeros

    dft(complexI, complexI);            // this way the result may fit in the source matrix

                                        // compute the magnitude and switch to logarithmic scale
                                        // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
    split(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
    magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
    magI = planes[0];

    magI += Scalar::all(1);                    // switch to logarithmic scale
    log(magI, magI);

    // crop the spectrum, if it has an odd number of rows or columns
    magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));

    // rearrange the quadrants of Fourier image  so that the origin is at the image center
    int cx = magI.cols / 2;
    int cy = magI.rows / 2;

    Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
    Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
    Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
    Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right

    Mat tmp;                           // swap quadrants (Top-Left with Bottom-Right)
    q0.copyTo(tmp);
    q3.copyTo(q0);
    tmp.copyTo(q3);

    q1.copyTo(tmp);                    // swap quadrant (Top-Right with Bottom-Left)
    q2.copyTo(q1);
    tmp.copyTo(q2);


}

int main()
{

//load images and convert them to greyscale
    Mat I = imread("original_Image.png");
    cv::cvtColor(I, I, CV_BGR2GRAY);
    Mat II = imread("shifted_Image.png");
    cv::cvtColor(II, II, CV_BGR2GRAY);
    if (I.empty())
        return -1;

    // call the fft_shift function and multiply this to spectrum
    Mat mag1, mag1_shift, mag3,mag4;
    fft_shift(I,mag1);
    fft_shift(II, mag1_shift);
    mulSpectrums(mag1, mag1_shift,mag3, 0, 1);

    //perform an inverse dft and shift it, then normalize is for displaying
    cv::dft(mag3, mag3, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT);
    fft_shift(mag3, mag4);    
    normalize(mag4, mag4, 0, 1, CV_MINMAX);    
    imshow("spectrum shift", mag4);
    waitKey();

    return 0;
} 

以下是此计算的结果:结果

这是我预期的结果:预期结果

此结果是从以下位置的python程序中提取的:http://scikit-image.org/docs/0.11.x/auto_examples/plot_register_translation.html我尝试将此代码转换为C ++,即上面的代码,但无法正常工作。有人知道吗,我在这里做错了吗?

3 个答案:

答案 0 :(得分:0)

我已经从此页的第二篇文章中找到了解决方案: http://answers.opencv.org/question/1624/phase-correlation-for-image-registrationimage-stitching/ 这段代码的结果是:

Result of the taken code from the link above

现在,我必须对这张图像进行归一化处理,以便仅查看子节点。

因此,在进行ift之前,必须规范多谱(从上面的链接中摘录的代码)的结果:

mulSpectrums(fft1,fft2,fft1,0,true);
fft1 = fft1/abs(fft1) //-->new
idft(fft1,fft1);

此后,您必须交换象限,就像在openCV示例中一样:

// crop the spectrum, if it has an odd number of rows or columns
    fft1 = fft1(Rect(0, 0, fft1.cols & -2, fft1.rows & -2));
    // rearrange the quadrants of Fourier image  so that the origin is at the image center
    int cx = fft1.cols / 2;
    int cy = fft1.rows / 2;

    Mat q0(fft1, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
    Mat q1(fft1, Rect(cx, 0, cx, cy));  // Top-Right
    Mat q2(fft1, Rect(0, cy, cx, cy));  // Bottom-Left
    Mat q3(fft1, Rect(cx, cy, cx, cy)); // Bottom-Right

    Mat tmp;                           // swap quadrants (Top-Left with Bottom-Right)
    q0.copyTo(tmp);
    q3.copyTo(q0);
    tmp.copyTo(q3);

    q1.copyTo(tmp);                    // swap quadrant (Top-Right with Bottom-Left)
    q2.copyTo(q1);
    tmp.copyTo(q2);

现在结果看起来像是python代码中的结果:

final result

答案 1 :(得分:0)

或者我只能使用:

Point2d phaseCorrelate(InputArray src1,InputArray src2,InputArray window = noArray())

那是为我做的所有事情

答案 2 :(得分:0)

由于mulSpectrums的存在,您可能会在fft的反比例尺上出错,因此除标准化以外,您需要除以(width * height)^ 2以获得正确的结果。

您可以服用我的食谱:

cv::Mat XCorrelation(cv::Mat const& I, cv::Mat const& I1) 
    {
        int width = cv::getOptimalDFTSize(std::max(I.cols,I1.cols));
        int height = cv::getOptimalDFTSize(std::max(I.rows,I1.rows));
        cv::Mat fft1;
        cv::Mat fft2;

        cv::copyMakeBorder(I, fft1, 0, height - I.rows, 0, width - I.cols, cv::BORDER_CONSTANT, cv::Scalar::all(0));
        cv::copyMakeBorder(I1, fft2, 0, height - I.rows, 0, width - I.cols, cv::BORDER_CONSTANT, cv::Scalar::all(0));

        fft1.convertTo(fft1, CV_32F);
        fft2.convertTo(fft2, CV_32F);

        cv::dft(fft1,fft1,0,I.rows);
        cv::dft(fft2,fft2,0,I1.rows);

        cv::mulSpectrums(fft1,fft2,fft1,0,true);
        // here cv::DFT_SCALE divide `width*height` 1 times
        cv::idft(fft1,fft1,cv::DFT_SCALE|cv::DFT_REAL_OUTPUT);
        Rearrange(fft1, fft1);
        // here divide another times
        return cv::abs(fft1)/(width*height);
    }

Rearrange功能与您的fft_shift相同,如下所示:

void Rearrange(cv::Mat& src, cv::Mat& dst)
    {
        int cx = src.cols / 2;
        int cy = src.rows / 2;
        cv::Mat tmp;
        tmp.create(src.size(), src.type());
        src(cv::Rect(0, 0, cx, cy)).copyTo(tmp(cv::Rect(cx, cy, cx, cy)));
        src(cv::Rect(cx, cy, cx, cy)).copyTo(tmp(cv::Rect(0, 0, cx, cy)));
        src(cv::Rect(cx, 0, cx, cy)).copyTo(tmp(cv::Rect(0, cy, cx, cy)));
        src(cv::Rect(0, cy, cx, cy)).copyTo(tmp(cv::Rect(cx, 0, cx, cy)));
        dst = tmp;
    }

对于著名的莉娜(dx = 30,dy = 20),我得到的结果图像与您的python输出类似:

Lena3020