opencv给图像添加椒盐噪声和高斯噪声
作者:互联网
U1和U2可以是随机值,经过上式产生的随机变量Z0,Z1满足标准高斯分布
实现代码如下:
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
#include<iostream>
using namespace std;
using namespace cv;
//给图像添加椒盐噪声
Mat addSaltNoise(Mat src, int n)
{
Mat result = src.clone();
for (int k = 0; k < n; k++)
{
//随机选取行列值
int i = rand() % result.cols;
int j = rand() % result.rows;
if (result.channels() == 1)
{
result.at<uchar>(j, i) = 255;
}
else
{
result.at<Vec3b>(j, i)[0] = 255;
result.at<Vec3b>(j, i)[1] = 255;
result.at<Vec3b>(j, i)[2] = 255;
}
}
return result;
}
//给图像添加高斯噪声
double generateGaussianNoise(double mu, double sigma)
{
//定义最小值
double epsilon = numeric_limits<double>::min();
double z0=0, z1=0;
bool flag = false;
flag = !flag;
if (!flag)
return z1*sigma + mu;
double u1, u2;
do
{
u1 = rand()*(1.0 / RAND_MAX);
u2 = rand()*(1.0/RAND_MAX);
} while (u1 <= epsilon);
z0 = sqrt(-2.0*log(u1))*cos(2*CV_PI*u2);
z1 = sqrt(-2.0*log(u1))*sin(2 * CV_PI*u2);
return z0*sigma + mu;
}
Mat addGaussianNoise(Mat& src)
{
Mat result = src.clone();
int channels = result.channels();
int nRows = result.rows;
int nCols = result.cols*channels;
if (result.isContinuous())
{
nCols = nCols*nRows;
nRows = 1;
}
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
int val = result.ptr<uchar>(i)[j] + generateGaussianNoise(2, 0.8)*32;
if (val < 0)
val = 0;
if (val > 255)
val = 255;
result.ptr<uchar>(i)[j] = (uchar)val;
}
}
return result;
}
int main()
{
//imread中0表示灰度返回,1表示原图返回
Mat src = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower3.jpg",0);
if (!src.data)
return -1;
Mat dst;
//dst=addSaltNoise(src, 100);
dst = addGaussianNoise(src);
//imshow("SaltNoise",dst);
imshow("GaussianNoise", dst);
waitKey(0);
return(0);
}
结果如图:
标签:src,椒盐,double,dst,噪声,opencv,result,return,255 来源: https://blog.csdn.net/love_image_xie/article/details/88093579