用OpenCV的RNG类为图像添加高斯噪声

来源:互联网 发布:在线反馈系统php源码 编辑:程序博客网 时间:2024/05/17 05:52
本文转载自 http://opencv66.net/thread-28-1-1.html
在OpenCV中,可以用RNG类来产生均匀分布和正态分布(高斯分布)的随机数。RNG的英文全称是Random number generator,从名字上大家就可以看出这个类的用途。
我们可以用这个类的成员函数fill来为图像添加高斯噪声。RNG:fill的原型如下:
C++: void RNG::fill(InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange=false )
参数意义如下
mat:二维或多维矩阵,目前版本中,维数不能超过4。
distType:随机数的分布类型,有两种,即RNG::UNIFORM 或 RNG::NORMAL,前者表示均匀分布,后者表示正态分布(即高斯分布)。
a:分布规律参数之一。在均匀分布中表示下限(包含),在正态分布(即高斯分布)中,表示均值。
b:分布规律参数之一。在均匀分布中表示上限(包含),在正态分布(即高斯分布)中,表示标准差。
saturateRange:这个参数只对均匀分布时有用。具体是什么含义,笔者现在也没搞清楚,等搞清楚了再来通知大家吧。官方原文实在没有看懂,摘录如下(如果有朋友看懂了,记得告诉笔者一声哦,笔者的QQ号2034196302)
saturateRange – pre-saturation flag; for uniform distribution only; if true, the method will first convert a and b to the acceptable value range (according to the mat datatype) and then will generate uniformly distributed random numbers within the range [saturate(a), saturate(b)), if saturateRange=false, the method will generate uniformly distributed random numbers in the original range [a, b) and then will saturate them, it means, for example, that theRNG().fill(mat_8u, RNG::UNIFORM, -DBL_MAX, DBL_MAX) will likely produce array mostly filled with 0’s and 255’s, since the range (0, 255) is significantly smaller than [-DBL_MAX, DBL_MAX).
用这个成员函数为图像添加高斯噪声的代码如下:

代码请大家到原帖中查看,原帖地址 http://opencv66.net/thread-28-1-1.html

代码请大家到原帖中查看,原帖地址 http://opencv66.net/thread-28-1-1.html

代码请大家到原帖中查看,原帖地址 http://opencv66.net/thread-28-1-1.html

运行结果如下图所示


本文转载自 http://opencv66.net/thread-28-1-1.html

原创粉丝点击