#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;
using namespace cv;
Mat FuncSaltNoise(const Mat srcImg, int NoiseValue);//添加椒盐噪声
int main(int argc, char** argv)
{
Mat srcImg = imread("1.jpg");
Mat DstImg = Mat::zeros(srcImg.cols, srcImg.rows, srcImg.type());
Mat result;
DstImg = FuncSaltNoise(srcImg,10000);
hconcat(srcImg, DstImg, result);
imshow("图像对比显示", result);
waitKey(0);
return 0;
}
/* --------------------------------------------------------------------------
添加椒盐噪声
--------------------------------------------------------------------------*/
Mat FuncSaltNoise(const Mat srcImg, int NoiseValue)
{
Mat resultImage = srcImg.clone();
for (int k = 0; k < NoiseValue; k++)//添加NoiseValue个噪声点
{
int i = rand() % resultImage.cols;
int j = rand() % resultImage.rows;
if (resultImage.channels() == 1)
{
resultImage.at<uchar>(j,i) = 255;
}
else
{
resultImage.at<Vec3b>(j, i)[0] = 255;
resultImage.at<Vec3b>(j, i)[1] = 255;
resultImage.at<Vec3b>(j, i)[2] = 255;
}
}
return resultImage;
}
?
|