## 全变分（TV）模型原理与C++实现

https://blog.csdn.net/cyh706510441/article/details/45194223

void CImageObj::Total_Variation(int iter, double dt, double epsilon, double lambda)
{
int i, j;
int nx = m_width, ny = m_height;
double ep2 = epsilon * epsilon;

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] = (double)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 tmp_x = (I_t[i][jRight] - I_t[i][jLeft]) / 2.0;
double tmp_y = (I_t[iDown][j] - I_t[iUp][j]) / 2.0;
double tmp_xx = I_t[i][jRight] + I_t[i][jLeft] - 2 * I_t[i][j];
double tmp_yy = I_t[iDown][j] + I_t[iUp][j] - 2 * I_t[i][j];
double tmp_xy = (I_t[iDown][jRight] + I_t[iUp][jLeft] - I_t[iUp][jRight] - I_t[iDown][jLeft]) / 4.0;
double tmp_num = tmp_yy * (tmp_x * tmp_x + ep2) + tmp_xx * (tmp_y * tmp_y + ep2) - 2 * tmp_x * tmp_y * tmp_xy;
double tmp_den = pow(tmp_x * tmp_x + tmp_y * tmp_y + ep2, 1.5);

I_tmp[i][j] += dt*(tmp_num / tmp_den + lambda*(m_imgData[i][j] - I_t[i][j]));
}
}  // 一次迭代

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

posted on 2018-11-29 10:47 zmj 阅读(691) 评论(0)  编辑 收藏 引用