在opencv c ++

时间:2017-03-28 23:37:33

标签: c++ opencv image-processing fft zooming

我正在尝试使用opencv c ++中的DFT(离散傅立叶变换)进行图像缩放。这个概念是对图像进行DFT和DFT,然后向其添加零边界,然后进行DFT的反转。我在结果中遇到了问题。我猜它是在填充DFT的结果为零。这是我的代码。任何有想法如何解决它的人,请帮助我,谢谢你的进步。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include<opencv2/opencv.hpp>
#include<opencv2/opencv_modules.hpp>
#include<iostream>
#include<string.h>


using namespace cv;
using namespace std;


void calculeDFT(Mat& source , Mat& destination)
{

    Mat origincomplex [2] = {source , Mat::zeros(source.size(),CV_32F)};

    Mat dtfready;

    merge(origincomplex, 2, dtfready);

    Mat origindft;

    dft(dtfready, origindft , DFT_COMPLEX_OUTPUT);

    destination = origindft ;

}
void Ajoutbor(Mat& source, Mat& destination)
{
    copyMakeBorder(source,source,50,50,50,50, BORDER_CONSTANT,0);

    Mat splitTab[2] = {Mat::zeros(source.size(),CV_32F),Mat::zeros(source.size(),CV_32F)};

    split(source,splitTab);

    //Mat dftMag;

    copyMakeBorder(splitTab[0],splitTab[0],50,50,50,50, BORDER_CONSTANT,0);

    copyMakeBorder(splitTab[1],splitTab[1],50,50,50,50, BORDER_CONSTANT,0);

    merge(splitTab, 2 , destination);

    //magnitude(splitTab[0], splitTab[1], dftMag);

    //dftMag += Scalar::all(1);

    //log(dftMag, dftMag);

    //normalize(dftMag, dftMag, 0, 1, CV_MINMAX);

    //CenterDFT(dftMag);

    //imshow("DFT",dftMag);

    //destination = dftMag ;

    waitKey();

}

void CenterDFT(Mat& source)
{
    int cx = source.cols/2;
    int cy = source.rows/2;

    Mat q0(source, Rect(0, 0, cx, cy));
    Mat q1(source, Rect(cx, 0, cx, cy));
    Mat q2(source, Rect(0, cy, cx, cy));
    Mat q3(source, Rect(cx, cy, cx, cy));

    Mat tmp;
    q0.copyTo(tmp);
    q3.copyTo(q0);
    tmp.copyTo(q3);

    q1.copyTo(tmp);
    q2.copyTo(q1);
    tmp.copyTo(q2);

}
void afficheDFT(Mat& source)
{
    Mat splitTab[2] = {Mat::zeros(source.size(),CV_32F),Mat::zeros(source.size(),CV_32F)};

    split(source,splitTab);

    Mat dftMag;

    magnitude(splitTab[0], splitTab[1], dftMag);

    dftMag += Scalar::all(1);

    log(dftMag, dftMag);

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

    CenterDFT(dftMag);

    imshow("DFT",dftMag);

    waitKey();

}

void InverseDFT(Mat& source,Mat& destination)
{
    Mat inverse;

    dft(source, inverse, DFT_INVERSE | DFT_REAL_OUTPUT | DFT_SCALE);

    destination = inverse ;

}
int main()
{


Mat img = imread("bn.jpg", CV_LOAD_IMAGE_GRAYSCALE);

Mat imgfloat;

img.convertTo(imgfloat ,CV_32FC1 , 1.0 /255.0);

imshow("Image Original",imgfloat);

Mat imgdft;

calculeDFT(imgfloat,imgdft);

Mat imgap;



afficheDFT(imgdft);

Ajoutbor(imgdft,imgap);

Mat inversdft;

InverseDFT(imgap,inversdft);


imshow("inverse ", inversdft);

cvWaitKey();

}

以下是外观:

0 个答案:

没有答案