opencv-图片处理-实现傅里叶变换的窗口函数

来源:互联网 发布:犀牛地方门户源码官网 编辑:程序博客网 时间:2024/05/21 07:59

常见的窗口函数:

hanning



opencv中有实现的函数createHanningWindow,但是函数的Size.width和Size.height必须均大于1,我们重写该函数.

void myCreateHanningWindow(OutputArray _dst, cv::Size winSize,int type){    CV_Assert( type == CV_32FC1 || type == CV_64FC1 );_dst.create(winSize, type);    Mat dst = _dst.getMat();int rows = dst.rows;    int cols = dst.cols;    if(dst.depth() == CV_32F)    {        if(rows == 1 && cols ==1){dst.at<float>(0,0) = 1;}else if(rows ==1 && cols > 1){float* dstData = dst.ptr<float>(0);for(int j = 0;j < cols;j++){dstData[j] = 0.5 * (1.0 - cos(2.0 * CV_PI * (double)j / (double)(cols - 1)));}}else if(rows > 1 && cols == 1){for(int i = 0;i < rows; i++){float* dstData =  dst.ptr<float>(i);dstData[0] =  0.5 * (1.0 - cos(2.0 * CV_PI * (double)i / (double)(rows - 1)));}}else{for(int i = 0; i < rows; i++){float* dstData = dst.ptr<float>(i);double wr =  0.5 * (1.0 - cos(2.0 * CV_PI * (double)i / (double)(rows - 1)));for(int j = 0; j < cols; j++){double wc = 0.5 * (1.0 - cos(2.0 * CV_PI * (double)j / (double)(cols - 1)));dstData[j] = (float)(wr * wc);}           }sqrt(dst,dst);        }}else{if(rows ==1 && cols == 1){dst.at<double>(0,0) = 1;}else if(rows == 1 && cols > 1){double* dstData =  dst.ptr<double>(0);for(int j = 0;j < cols;j++){dstData[j] =  0.5 * (1.0 - cos(2.0 * CV_PI * (double)j / (double)(cols - 1)));}}else if(rows > 1 && cols == 1){for(int i = 0;i < rows; i++){double* dstData =  dst.ptr<double>(i);dstData[0] = 0.5 * (1.0 - cos(2.0 * CV_PI * (double)i / (double)(rows - 1)));}}else{for(int i = 0; i < rows; i++){double* dstData = dst.ptr<double>(i);double wr = 0.5 * (1.0 - cos(2.0 * CV_PI * (double)i / (double)(rows - 1)));for(int j =0 ; j < cols;j++){double wc = 0.5 * (1.0 - cos(2.0 * CV_PI * (double)j / (double)(cols - 1)));dstData[j] = (double)(wr * wc);}}sqrt(dst,dst);}}}

hamming:


opencv的代码如下:


void createHammingWindow(OutputArray _dst, cv::Size winSize,int type){    CV_Assert( type == CV_32FC1 || type == CV_64FC1 );_dst.create(winSize, type);    Mat dst = _dst.getMat();int rows = dst.rows;    int cols = dst.cols;    if(dst.depth() == CV_32F)    {        if(rows == 1 && cols ==1){dst.at<float>(0,0) = 1;}else if(rows ==1 && cols > 1){float* dstData = dst.ptr<float>(0);for(int j = 0;j < cols;j++){dstData[j] = 0.54 - 0.46 * cos(2.0 * CV_PI * (double)j / (double)(cols - 1));}}else if(rows > 1 && cols == 1){for(int i = 0;i < rows; i++){float* dstData =  dst.ptr<float>(i);dstData[0] = 0.54 - 0.46 * cos(2.0 * CV_PI * (double)i / (double)(rows - 1));}}else{for(int i = 0; i < rows; i++){float* dstData = dst.ptr<float>(i);double wr = 0.54  -  0.46 * cos(2.0f * CV_PI * (double)i / (double)(rows - 1));for(int j = 0; j < cols; j++){double wc = 0.54 - 0.46 * cos(2.0f * CV_PI * (double)j / (double)(cols - 1));dstData[j] = (float)(wr * wc);}           }sqrt(dst,dst);        }}else{if(rows ==1 && cols == 1){dst.at<double>(0,0) = 1;}else if(rows == 1 && cols > 1){double* dstData =  dst.ptr<double>(0);for(int j = 0;j < cols;j++){dstData[j] = 0.54 - 0.46 * cos(2.0 * CV_PI * (double)j / (double)(cols - 1));}}else if(rows > 1 && cols == 1){for(int i = 0;i < rows; i++){double* dstData =  dst.ptr<double>(i);dstData[0] = 0.54 - 0.46 * cos(2.0 * CV_PI * (double)i / (double)(rows - 1));}}else{for(int i = 0; i < rows; i++){double* dstData = dst.ptr<double>(i);double wr = 0.54  -  0.46 * cos(2.0f * CV_PI * (double)i / (double)(rows - 1));for(int j =0 ; j < cols;j++){double wc = 0.54 - 0.46 * cos(2.0f * CV_PI * (double)j / (double)(cols - 1)); dstData[j] = (double)(wr * wc);}}sqrt(dst,dst);}}}


1 0
原创粉丝点击