码迷,mamicode.com
首页 > 其他好文 > 详细

同态滤波器

时间:2015-04-04 10:45:32      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:visual c++   opencv2   同态滤波器   冈萨雷斯   

这次使用的是经典教材——冈萨雷斯 数字图像处理(第二版)

P152-154  4.5同态滤波器   附上下载地址http://dl.dbank.com/c0atzs7h2g#

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">理论部分就不叨叨了,直接上代码,写的感觉有点问题,求指导。</span>
void HomoFilter(Mat srcImg, Mat &dst)
{
	srcImg.convertTo(srcImg, CV_64FC1);
	dst = Mat::zeros(srcImg.rows, srcImg.cols, CV_64FC1);

	// 构造滤波矩阵
	Mat H_u_v;
	double gammaH = 1.5;
	double gammaL = 0.5;
	double C = 1;
	double d0 = (srcImg.rows/2)*(srcImg.rows/2) + (srcImg.cols/2)*(srcImg.cols/2);
	double d2 = 0;
	H_u_v = Mat::zeros(srcImg.rows, srcImg.cols, CV_64FC1);
	for (int i = 0; i < srcImg.rows; i++)
	{
		double * dataH_u_v = H_u_v.ptr<double>(i);
		for (int j = 0; j < srcImg.cols; j++)
		{
			d2 = pow((i - srcImg.rows/2), 2.0) + pow((j - srcImg.cols/2), 2.0);
			dataH_u_v[j] = 	(gammaH - gammaL)*(1 - exp(-C*d2/d0)) + gammaL;  
		}
	}

	for (int i = 0; i < srcImg.rows; i++)
	{
		double* srcdata = srcImg.ptr<double>(i);
		double* logdata = dst.ptr<double>(i);
		for (int j = 0; j < srcImg.cols; j++)
		{	
			logdata[j] = log(srcdata[j]+1.0);
		}
	}

	//%%%%%%%%%%%%%%%%%%%%%%%傅里叶变换、滤波、傅里叶反变换%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	Mat planes[] = {dst, Mat::zeros(dst.size(), CV_64F)};
	Mat complexI;
	merge(planes, 2, complexI); // Add to the expanded another plane with zeros
	dft(complexI, complexI);    // this way the result may fit in the source matrix
	// compute the magnitude and switch to logarithmic scale
	// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
	split(complexI, planes); 
	Mat IDFT[] = {Mat::zeros(dst.size(), CV_64F), Mat::zeros(dst.size(), CV_64F)};
	IDFT[0] = H_u_v.mul(planes[0]);//planes[0].mul(H_u_v);
	IDFT[1] = H_u_v.mul(planes[1]);//planes[1].mul(H_u_v);

	merge(IDFT, 2, complexI); 
	idft(complexI, complexI);
	split(complexI, IDFT); 

	for (int i = 0; i < srcImg.rows; i++)
	{
		double* dataRe = IDFT[0].ptr<double>(i);
		double* dataIm = IDFT[1].ptr<double>(i);
		double* logdata = dst.ptr<double>(i);

		for (int j = 0; j < srcImg.cols; j++)
		{	
			if (dataIm[j] < 0)
			{
				logdata[j]  = dataRe[j]*dataRe[j] - dataIm[j]*dataIm[j];
			}
			else
			{
				logdata[j]  = dataRe[j]*dataRe[j] + dataIm[j]*dataIm[j];
			}
		}
	}


	normalize(dst, dst, 0, 5.545, CV_MINMAX); 	

	for (int i = 0; i < srcImg.rows; i++)
	{
		double* logdata = dst.ptr<double>(i);
		for (int j = 0; j < srcImg.cols; j++)
		{	
			logdata[j] = pow(2.718281828, logdata[j]) - 1.0;
		}
	}
	dst.convertTo(dst, CV_8UC1);;

}


同态滤波器

标签:visual c++   opencv2   同态滤波器   冈萨雷斯   

原文地址:http://blog.csdn.net/liquan1434/article/details/44871053

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!