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

常见的窗口函数:

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);
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值