3

C++ で OpenCV を使用して逆 DFT を実装しようとしています。完全な dft の例を docs.opencv.org にダウンロードし、数行を逆に調整するだけです。

私の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;
}
4

2 に答える 2

1

何らかの理由で、指定された idft ブロックで正規化関数を使用しても、目的の元の画像が正確に得られません (コントラストに関して)。代わりに convertTo を使用してください。正確には、正規化関数を次の行に置き換えます。

inverseTransform.convertTo(逆変換、CV_8U);

于 2014-06-14T13:11:25.133 に答える