本系列代码托管于: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;
}
评论 (0)