Matlab中psf2otf()函数在opencv中的实现

在Matlab中有个psf2otf()函数,可以将小尺寸的点扩散函数,扩大尺寸,并作二维傅里叶变换,opencv中没有这个函数,所以编了这么个函数:

/*****************************
Mat psf2otf(const Mat&psf, Size outSize=Size(3,3))
参数说明:
psf——输入的点扩散函数;
outSize——是输出otf的尺寸;

在本程序中,还调用了circShift()函数,该函数具体参见:
http://www.cnblogs.com/phoenixdsg/p/8425336.html

本程序中,还调用了mymax()函数和myMagnitude()函数,难度不大自己想办法吧。

*******************************/

下面是psf2otf()函数的定义:

{

    Mat otf;
    if(countNonZero(psf)&&!psf.empty())
    {
        Size psfSize=psf.size();

        Size paddSize=outSize-psfSize;
        copyMakeBorder(psf,otf,paddSize.height/2,paddSize.height/2,
                       paddSize.width/2 ,paddSize.width/2,
                       BORDER_CONSTANT,Scalar(0));
        circShift(otf,-otf.size().height/2,-otf.size().width/2);
        otf=fft(otf);
        //计算psf的元素个数
        int nElem=psf.cols*psf.rows;

        double nOps=0;

        int nfft=nElem/psfSize.width;
        nOps +=nfft+psfSize.width*log2(psfSize.width)*nfft;
        nfft=nElem/psfSize.height;
        nOps +=nfft+psfSize.width*log2(psfSize.height)*nfft;
        //将复数otf分解成实部real和虚部imagin
        Mat planes[2];
        split(otf,planes);
        double imagin_max=mymax(abs(planes[1]));
        double mag_max=mymax(myMagnitude(otf));

        if((imagin_max/mag_max)<eps)
            return planes[0];

    }
    return otf;
}

下面是对该函数的测试程序:

int main()
{
    Mat mat=(Mat_<double>(3,3)<<
             0,-1,0,
             -1,4,-1,
             0,-1,0);
    Mat otf=psf2otf(mat);
    cout<<otf<<endl;
    return 0;
}

输出结果如下:

 

posted @ 2018-05-18 21:10  凤凰_1  阅读(3416)  评论(0编辑  收藏  举报