• OpenCV每日函数 计算摄影模块(2) 图像去噪算法


    一、函数参考

    1、Primal-dual算法

            Primal-dual algorithm是一种用于解决特殊类型的变分问题的算法(即找到一个函数来最小化一些泛函)。

            特别是由于图像去噪可以看作是变分问题,因此可以使用原始对偶算法进行去噪,这正是该算法所实现的。

    cv::denoise_TVL1 (const std::vector< Mat > &observations, Mat &result, double lambda=1.0, int niters=30)
    observations该数组应包含要恢复的图像的一个或多个噪声版本。
    result这里将存储去噪图像。 无需预先分配存储空间,必要时会自动分配。
    lambda对应于上述公式中的 λ。 当它被放大时,平滑(模糊)的图像比细节(但可能有更多噪点)的图像更受欢迎。 粗略地说,随着它变小,结果会更加模糊,但会去除更多的异常值。
    niters算法将运行的迭代次数。 当然,越多的迭代越好,但是这个说法很难量化细化,所以就使用默认值,如果结果不好就增加它。

    2、非局部均值去噪算法

            使用非局部均值去噪算法,该方法基于一个简单的原理:将像素的颜色替换为相似像素颜色的平均值。 但是与给定像素最相似的像素根本没有理由靠近。 因此,扫描图像的大部分以寻找真正类似于想要去噪的像素的所有像素是合法的。执行图像去噪,并进行了多种计算优化。 噪声预期为高斯白噪声。

    1. cv::cuda::fastNlMeansDenoising (InputArray src, OutputArray dst, float h, int search_window=21, int block_size=7, Stream &stream=Stream::Null())
    2. cv::fastNlMeansDenoising (InputArray src, OutputArray dst, float h=3, int templateWindowSize=7, int searchWindowSize=21)
    3. cv::fastNlMeansDenoising (InputArray src, OutputArray dst, const std::vector< float > &h, int templateWindowSize=7, int searchWindowSize=21, int normType=NORM_L2)

            针对彩色图像的 fastNlMeansDenoising 函数。

    1. cv::cuda::fastNlMeansDenoisingColored (InputArray src, OutputArray dst, float h_luminance, float photo_render, int search_window=21, int block_size=7, Stream &stream=Stream::Null())
    2. cv::fastNlMeansDenoisingColored (InputArray src, OutputArray dst, float h=3, float hColor=3, int templateWindowSize=7, int searchWindowSize=21)

            针对图像序列的 fastNlMeansDenoising 函数。

    1. cv::fastNlMeansDenoisingColoredMulti (InputArrayOfArrays srcImgs, OutputArray dst, int imgToDenoiseIndex, int temporalWindowSize, float h=3, float hColor=3, int templateWindowSize=7, int searchWindowSize=21)
    2. cv::fastNlMeansDenoisingMulti (InputArrayOfArrays srcImgs, OutputArray dst, int imgToDenoiseIndex, int temporalWindowSize, float h=3, int templateWindowSize=7, int searchWindowSize=21)
    3. cv::fastNlMeansDenoisingMulti (InputArrayOfArrays srcImgs, OutputArray dst, int imgToDenoiseIndex, int temporalWindowSize, const std::vector< float > &h, int templateWindowSize=7, int searchWindowSize=21, int normType=NORM_L2)

            执行纯非局部方法去噪,没有任何简化,因此速度不快。

    cv::cuda::nonLocalMeans (InputArray src, OutputArray dst, float h, int search_window=21, int block_size=7, int borderMode=BORDER_DEFAULT, Stream &stream=Stream::Null())

    三、OpenCV源码

    1、源码路径

    opencv\modules\photo\src\denoise_tvl1.cpp

    2、源码代码

    1. #include "precomp.hpp"
    2. #include <vector>
    3. #include <algorithm>
    4. #define ABSCLIP(val,threshold) MIN(MAX((val),-(threshold)),(threshold))
    5. namespace cv{
    6. class AddFloatToCharScaled{
    7. public:
    8. AddFloatToCharScaled(double scale):_scale(scale){}
    9. inline double operator()(double a,uchar b){
    10. return a+_scale*((double)b);
    11. }
    12. private:
    13. double _scale;
    14. };
    15. using std::transform;
    16. void denoise_TVL1(const std::vector<Mat>& observations,Mat& result, double lambda, int niters){
    17. CV_Assert(observations.size()>0 && niters>0 && lambda>0);
    18. const double L2 = 8.0, tau = 0.02, sigma = 1./(L2*tau), theta = 1.0;
    19. double clambda = (double)lambda;
    20. double s=0;
    21. const int workdepth = CV_64F;
    22. int i, x, y, rows=observations[0].rows, cols=observations[0].cols,count;
    23. for(i=1;i<(int)observations.size();i++){
    24. CV_Assert(observations[i].rows==rows && observations[i].cols==cols);
    25. }
    26. Mat X, P = Mat::zeros(rows, cols, CV_MAKETYPE(workdepth, 2));
    27. observations[0].convertTo(X, workdepth, 1./255);
    28. std::vector< Mat_<double> > Rs(observations.size());
    29. for(count=0;count<(int)Rs.size();count++){
    30. Rs[count]=Mat::zeros(rows,cols,workdepth);
    31. }
    32. for( i = 0; i < niters; i++ )
    33. {
    34. double currsigma = i == 0 ? 1 + sigma : sigma;
    35. // P_ = P + sigma*nabla(X)
    36. // P(x,y) = P_(x,y)/max(||P(x,y)||,1)
    37. for( y = 0; y < rows; y++ )
    38. {
    39. const double* x_curr = X.ptr<double>(y);
    40. const double* x_next = X.ptr<double>(std::min(y+1, rows-1));
    41. Point2d* p_curr = P.ptr<Point2d>(y);
    42. double dx, dy, m;
    43. for( x = 0; x < cols-1; x++ )
    44. {
    45. dx = (x_curr[x+1] - x_curr[x])*currsigma + p_curr[x].x;
    46. dy = (x_next[x] - x_curr[x])*currsigma + p_curr[x].y;
    47. m = 1.0/std::max(std::sqrt(dx*dx + dy*dy), 1.0);
    48. p_curr[x].x = dx*m;
    49. p_curr[x].y = dy*m;
    50. }
    51. dy = (x_next[x] - x_curr[x])*currsigma + p_curr[x].y;
    52. m = 1.0/std::max(std::abs(dy), 1.0);
    53. p_curr[x].x = 0.0;
    54. p_curr[x].y = dy*m;
    55. }
    56. //Rs = clip(Rs + sigma*(X-imgs), -clambda, clambda)
    57. for(count=0;count<(int)Rs.size();count++){
    58. transform<MatIterator_<double>,MatConstIterator_<uchar>,MatIterator_<double>,AddFloatToCharScaled>(
    59. Rs[count].begin(),Rs[count].end(),observations[count].begin<uchar>(),
    60. Rs[count].begin(),AddFloatToCharScaled(-sigma/255.0));
    61. Rs[count]+=sigma*X;
    62. min(Rs[count],clambda,Rs[count]);
    63. max(Rs[count],-clambda,Rs[count]);
    64. }
    65. for( y = 0; y < rows; y++ )
    66. {
    67. double* x_curr = X.ptr<double>(y);
    68. const Point2d* p_curr = P.ptr<Point2d>(y);
    69. const Point2d* p_prev = P.ptr<Point2d>(std::max(y - 1, 0));
    70. // X1 = X + tau*(-nablaT(P))
    71. x = 0;
    72. s=0.0;
    73. for(count=0;count<(int)Rs.size();count++){
    74. s=s+Rs[count](y,x);
    75. }
    76. double x_new = x_curr[x] + tau*(p_curr[x].y - p_prev[x].y)-tau*s;
    77. // X = X2 + theta*(X2 - X)
    78. x_curr[x] = x_new + theta*(x_new - x_curr[x]);
    79. for(x = 1; x < cols; x++ )
    80. {
    81. s=0.0;
    82. for(count=0;count<(int)Rs.size();count++){
    83. s+=Rs[count](y,x);
    84. }
    85. // X1 = X + tau*(-nablaT(P))
    86. x_new = x_curr[x] + tau*(p_curr[x].x - p_curr[x-1].x + p_curr[x].y - p_prev[x].y)-tau*s;
    87. // X = X2 + theta*(X2 - X)
    88. x_curr[x] = x_new + theta*(x_new - x_curr[x]);
    89. }
    90. }
    91. }
    92. result.create(X.rows,X.cols,CV_8U);
    93. X.convertTo(result, CV_8U, 255);
    94. }
    95. }

    四、效果图像示例

    原图
    denoise_TVL1 
    fastNlMeansDenoising

     

  • 相关阅读:
    GB/T 24721.1-2023 公路用玻璃纤维增强塑料产品检测
    吴恩达2022机器学习专项课程C2W2:实验SoftMax
    什么是千行代码缺陷率?
    Undefined reference to pthread_create in Linux
    仿游戏热血江湖游戏类31
    ElasticSearch的集群、节点、索引、分片和副本
    Webpack 5 新特性
    Linux 0.11源码的内存管理和进程创建&&Linux0.99改进方法简述
    MQTT(详解)
    ​基于AI的脑电信号独立成分的自动标记工具箱
  • 原文地址:https://blog.csdn.net/bashendixie5/article/details/125358922