DFT函数源代码:(使用opencv。 using namespace cv)

#pragma mark - DFT
- (cv::Mat)dft2FromMat:(cv::Mat)srcMat 
{    
    // init
    Mat srcMat8uc1(srcMat.rows, srcMat.cols, CV_8UC1);
    cvtColor(srcMat, srcMat8uc1, CV_RGB2GRAY);
    
    Mat MatRe(srcMat.rows, srcMat.cols, CV_64FC1);
    srcMat8uc1.convertTo(MatRe, CV_64F, 1.0/255.0, 0);
    Mat MatIm = Mat::zeros(srcMat.rows, srcMat.cols, CV_64FC1);
    Mat srcMatMerge(srcMat.rows, srcMat.cols, CV_64FC2);
    vector<Mat> mergeVectorOfReAndIm;
    mergeVectorOfReAndIm.push_back(MatRe);
    mergeVectorOfReAndIm.push_back(MatIm);
    merge(mergeVectorOfReAndIm, srcMatMerge);
    
    // Do the work of DFT
    Mat dftMergeOutput(srcMat.rows, srcMat.cols, CV_64FC2);   
    dft(srcMatMerge, dftMergeOutput);
    split(dftMergeOutput, mergeVectorOfReAndIm);
    
    // Re^2 + Im^2 = amplitude^2
    // So you get amplitude here
    Mat tmp1_64fc1 = MatRe.clone();
    Mat tmp2_64fc1 = MatRe.clone();
    pow(MatRe, 2.0, tmp1_64fc1);
    pow(MatIm, 2.0, tmp2_64fc1);
    Mat amplitude = tmp1_64fc1 + tmp2_64fc1;
    pow(amplitude, 0.5, amplitude);
        
    MatRe.release();
    MatIm.release();
    srcMatMerge.release();
    dftMergeOutput.release();
    tmp2_64fc1.release();
    tmp1_64fc1.release();
    srcMat8uc1.release();
        
    return amplitude;
}

效果图如下:

posted on 2013-03-31 21:20  Shawn Hu  阅读(508)  评论(0)    收藏  举报