c++ - 如何在 OpenCV 中进行逆向 DFT

标签 c++ opencv

<分区>

我正在尝试在 C++ 中使用 OpenCV 实现逆向 DFT 我在 docs.opencv.org 下载了完整的 dft 示例,只需调整几行即可反转。

我的DFT代码是这样的

Mat DFT(const char* filename)
{
    Mat I = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
    if (I.empty())
    {
        Mat emty(7, 7, CV_32FC2, Scalar(1, 3));
        return emty;
    }

    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
    Mat magI = planes[0];

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



    normalize(magI, magI, 0, 1, CV_MINMAX); // Transform the matrix with float values into a
    // viewable image form (float between values 0 and 1).

    imshow("Input Image", I);    // Show the result
    imshow(filename, magI);
    //   waitKey();

    return magI;
}

并进行了 IDFT。通过将 dft 修复为 idft。但输出只是看起来像噪音。我做错了什么?我认为 dft 和 idft 是一样的....

Mat IDFT(Mat src)
{
    Mat I = src;
    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, DFT_INVERSE);            // this way the result may fit in the source matrix

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

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


    normalize(magI, magI, 0, 1, CV_MINMAX);

    imshow("forged map", magI);


    return magI;
}

最佳答案

你必须像这样重写你的代码以获得原始图像的逆 DFT 读取:

#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;

int main()
{
    Mat I = imread("test.tif", CV_LOAD_IMAGE_GRAYSCALE);
    if( I.empty())
        return -1;

    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
    Mat 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);


    normalize(magI, magI, 0, 1, CV_MINMAX); // Transform the matrix with float values into a
    normalize(phaseVals, phaseVals, 0, 1, CV_MINMAX);
                                            // viewable image form (float between values 0 and 1).

    imshow("Input Image"       , I   );    // Show the result
    imshow("Spectrum Magnitude", magI);
    waitKey();

    //calculating the idft
    cv::Mat inverseTransform;
    cv::dft(complexI, inverseTransform, cv::DFT_INVERSE|cv::DFT_REAL_OUTPUT);
    normalize(inverseTransform, inverseTransform, 0, 1, CV_MINMAX);
    imshow("Reconstructed", inverseTransform);
    waitKey();

    return 0;
}

我刚刚将这部分添加到您的代码中:

//calculating the idft
        cv::Mat inverseTransform;
        cv::dft(complexI, inverseTransform, cv::DFT_INVERSE|cv::DFT_REAL_OUTPUT);
        normalize(inverseTransform, inverseTransform, 0, 1, CV_MINMAX);
        imshow("Reconstructed", inverseTransform);
        waitKey();

关于c++ - 如何在 OpenCV 中进行逆向 DFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19761526/

相关文章:

c++ - 我对找到图像中大多数线的交点感到震惊

python - 从扫描图像中识别手写数字

spring - tomcat 找不到外部 jar - java.lang.NoClassDefFoundError : org/opencv/core/Core

c++ - 如何通过 val 从 std::deque 中删除函数指针?

c++ - 区 block 链 Hyperledger 私钥漏洞

python - 在 mac 上为 python 3.6 安装 opencv3

c++ - OpenCV 中 findFundamentalMat 函数错误

c++ - 如何强制所有派生类实现虚方法?

c++ - 如何在动态库(/MD)项目中使用静态库(/MT)?

c++ - 数值转换