码迷,mamicode.com
首页 > 编程语言 > 详细

各向异性扩散PM模型原理与C++实现

时间:2015-04-24 19:18:44      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:图像处理

本文介绍了各向异性扩散PM模型,并给出了C++代码实现。

一、PM模型原理

技术分享


技术分享

其中,       技术分享                                              技术分享


技术分享

技术分享

二、C++代码实现

MATLAB代码可参考:http://www.csse.uwa.edu.au/~pk/research/matlabfns/Spatial/anisodiff.m
http://www.mathworks.com/matlabcentral/fileexchange/14995-anisotropic-diffusion-perona-malik/content/anisodiff_Perona-Malik/anisodiff2D.m

void CImageObj::Perona_Malik(int iter, double dt, double kappa, int option)
{
	int i, j;
	int nx = m_width, ny = m_height;

	double** I_t = NewDoubleMatrix(nx, ny);
	double** I_tmp = NewDoubleMatrix(nx, ny);
	for (i = 0; i < ny; i++)
		for (j = 0; j < nx; j++)
			I_t[i][j] = I_tmp[i][j] = m_imgData[i][j];

	for (int t = 0; t < iter; t++)
	{
		for (i = 0; i < ny; i++)
		{
			for (j = 0; j < nx; j++)
			{
				int iUp = i - 1, iDown = i + 1;
				int jLeft = j - 1, jRight = j + 1;    // 边界处理
				if (0 == i) iUp = i; if (ny - 1 == i) iDown = i;
				if (0 == j) jLeft = j; if (nx - 1 == j) jRight = j;

				double deltaN = I_t[iUp][j] - I_t[i][j];
				double deltaS = I_t[iDown][j] - I_t[i][j];
				double deltaE = I_t[i][jRight] - I_t[i][j];
				double deltaW = I_t[i][jLeft] - I_t[i][j];

				double cN, cS, cE, cW;
				if (1 == option)
				{
					cN = exp(-(deltaN / kappa) * (deltaN / kappa));
					cS = exp(-(deltaS / kappa) * (deltaS / kappa));
					cE = exp(-(deltaE / kappa) * (deltaE / kappa));
					cW = exp(-(deltaW / kappa) * (deltaW / kappa));
				}
				else if (2 == option)
				{
					cN = 1.0 / (1 + (deltaN / kappa) * (deltaN / kappa));
					cS = 1.0 / (1 + (deltaS / kappa) * (deltaS / kappa));
					cE = 1.0 / (1 + (deltaE / kappa) * (deltaE / kappa));
					cW = 1.0 / (1 + (deltaW / kappa) * (deltaW / kappa));
				}

				I_tmp[i][j] += dt * (cN * deltaN + cS * deltaS + cE * deltaE + cW * deltaW);
			}
		}  // 一次迭代

		for (i = 0; i < ny; i++)
			for (j = 0; j < nx; j++)
			{
				I_t[i][j] = I_tmp[i][j];
			}

	} // 迭代结束

	// 给图像赋值
	for (i = 0; i < ny; i++)
		for (j = 0; j < nx; j++)
		{
			double tmp = I_t[i][j];
			tmp = max(0, min(tmp, 255));
			m_imgData[i][j] = (unsigned char)tmp;
		}

	DeleteDoubleMatrix(I_t, nx, ny);
	DeleteDoubleMatrix(I_tmp, nx, ny);
}



各向异性扩散PM模型原理与C++实现

标签:图像处理

原文地址:http://blog.csdn.net/cyh706510441/article/details/45248049

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