OpenCV是开源的,使用非常广泛的图像处理库,由于其出色的图像处理能力,使用范围非常广泛。如何添加高斯噪声和平均噪声,在某些领域有特殊的用途,现将基础的处理方法,它采用的对象为cv::RNG,相关用法,发布如下:
/*Each of the methods fills the matrix with the random values from the
specified distribution. As the new numbers are generated, the RNG state
is updated accordingly. In case of multiple-channel images, every
channel is filled independently, which means that RNG cannot generate
samples from the multi-dimensional Gaussian distribution with
non-diagonal covariance matrix directly. To do that, the method
generates samples from multi-dimensional standard Gaussian distribution
with zero mean and identity covariation matrix, and then transforms them
using transform to get samples from the specified Gaussian distribution.
RNG 生成随机分布
*/
void fill( InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange = false );
使用范例:
cv::Mat img = imgsrc.clone();
img.convertTo(img, CV_32F);
cv::Mat noiseMat = img.clone();
cv::RNG rng((unsigned)time(NULL));
if (noisetype == "高斯模糊")
{
rng.fill(noiseMat, cv::RNG::NORMAL, 0, noise_val);
}
else if (noisetype == "平均模糊")
{
rng.fill(noiseMat, cv::RNG::UNIFORM, -noise_val, noise_val);
}
cv::Mat imgDst = img + noise;
imgDst.convertTo(imgDst, CV_8U);
合理的脚本代码可以有效的提高工作效率,减少重复劳动。