本文介绍了TV模型的基本原理,并给出了C++代码实现。
一、TV模型原理
二、C++实现
关于Matlab的程序实现,有一个经典的主页: http://visl.technion.ac.il/~gilboa/PDE-filt/tv_denoising.html
有博主改成了C++代码:见经典的变分法图像去噪的C++实现
另有博主改成了更简洁的版本:见【图像处理】全分发TV图像去噪
本文代码基本参照上面的版本
void CImageObj::Total_Variation(int iter, double dt, double epsilon, double lambda) { int i, j; double ep2 = epsilon * epsilon; double** I_t = NewDoubleMatrix(m_width, m_height); for (i = 0; i < m_height; i++) for (j = 0; j < m_width; j++) I_t[i][j] = m_imgData[i][j]; for (int t = 0; t < iter; t++) { for (i = 1; i < m_height - 1; i++) { for (j = 1; j < m_width - 1; j++) { double tmp_x = (I_t[i][j + 1] - I_t[i][j - 1]) / 2.0; double tmp_y = (I_t[i + 1][j] - I_t[i - 1][j]) / 2.0; double tmp_xx = I_t[i][j + 1] + I_t[i][j - 1] - 2 * I_t[i][j]; double tmp_yy = I_t[i + 1][j] + I_t[i - 1][j] - 2 * I_t[i][j]; double tmp_xy = (I_t[i + 1][j + 1] + I_t[i - 1][j - 1] - I_t[i - 1][j] - I_t[i][j - 1]) / 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_t[i][j] += dt*(tmp_num / tmp_den + lambda*(m_imgData[i][j] - I_t[i][j])); } } } // 迭代结束 // 给图像赋值 for (i = 0; i < m_height; i++) for (j = 0; j < m_width; j++) { double tmp = I_t[i][j]; <span style="white-space:pre"> </span>tmp = max(0,min(tmp, 255)); m_imgData[i][j] = (unsigned char)tmp; } DeleteDoubleMatrix(I_t, m_width, m_height); }
时间: 2024-10-29 19:17:39