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



你可能感兴趣的:(opencv-实现傅里叶变换的窗口函数)