簡體   English   中英

2 個移位圖像與 OpenCV 的 C++ 互相關

[英]C++ Cross Correlation of 2 shifted images with 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;
} 

這是這個計算的結果: result

這是我預期的結果:預期結果

這個結果是從 python 程序中取出的: http : //scikit-image.org/docs/0.11.x/auto_examples/plot_register_translation.html我試着把這段代碼翻譯成 C++,這是上面的代碼,但它是不工作。 有誰知道,我在這里做錯了什么?

我從本頁的第二篇文章中找到了一個解決方案: http : //answers.opencv.org/question/1624/phase-correlation-for-image-registrationimage-stitching/這段代碼的結果是:

從上面的鏈接中獲取的代碼的結果

現在我必須標准化這個圖像,只看到 shiftet 點。

因此,在進行 ifft 之前,您必須對多頻譜的結果進行歸一化(從上面的鏈接中截取的代碼):

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 代碼中的結果:

最后結果

或者我可以只使用:

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

那為我做所有的事情

您可能會在反 fft 的比例上出錯,因為您mulSpectrums ,您需要除以 (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;
    }

對於著名的 Lena 換檔 (dx=30, dy=20),我得到的結果圖像與您的 Python 輸出相似:

麗娜3020

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM