本系列代码托管于:https://github.com/chintsan-code/opencv4-tutorials

本篇使用的项目为:gen_noise

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, const char** argv) {
	Mat src = imread("../sample/lena512.bmp");
	if (src.empty()) {
		cout << "could not load image..." << endl;
		return -1;
	}
	namedWindow("input", WINDOW_AUTOSIZE);
	imshow("input", src);

	// salt and pepper noise
	RNG rng(12345);
	Mat image = src.clone();
	int h = src.rows;
	int w = src.cols;
	int nums = 10000;
	for (int i = 0; i < nums; i++) {
		int x = rng.uniform(0, w);
		int y = rng.uniform(0, h);
		if (i % 2 == 1) {
			src.at<Vec3b>(y, x) = Vec3b(255, 255, 255);  //盐噪声
		}
		else {
			src.at<Vec3b>(y, x) = Vec3b(0, 0, 0);  //胡椒噪声
		}
	}
	imshow("salt and pepper noise", src);

	// gaussian noise
	Mat noise = Mat::zeros(image.size(), image.type());
	randn(noise, Scalar(25, 15, 45), Scalar(60, 40, 30)); // 产生正态分布的随机数
	imshow("noise", noise);
	Mat dst;
	add(image, noise, dst);
	imshow("gaussian noise", dst);

	waitKey(0);
	destroyAllWindows();
	return 0;
}

噪声分类

(1)椒盐噪声:噪声不是255(盐噪声)就是0(胡椒噪声)
opencv4入门笔记(21):图像噪声-萤火
(2)高斯噪声:符合高斯正态分布的噪声
opencv4入门笔记(21):图像噪声-萤火
(3)泊松噪声:符合泊松分布地噪声
(4)柏林噪声:符合柏林分布地噪声。常见于游戏领域