#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
void add_salt(Mat image, Mat &img, int n)
{
image.copyTo(img);
for (int k = 0; k < n; k++)
{
int i = rand() % img.cols;
int j = rand() % img.rows;
if (img.channels() == 1) {
img.at<uchar>(i, j) = 255;
}
else
{
img.at<Vec3b>(i, j)[0] = 255;
img.at<Vec3b>(i, j)[1] = 255;
img.at<Vec3b>(i, j)[2] = 255;
}
}
}
int main() {
Mat img = imread("G:/image/xs.jpg");
cvtColor(img, img, COLOR_BGR2GRAY);
Mat noise, gaussian, median, element;
resize(img, img, Size(256, 256), INTER_AREA);
add_salt(img, noise, 1000);
int kernel_size = 3;
element = getStructuringElement(MORPH_RECT, Size(5, 5));
GaussianBlur(noise, gaussian, Size(kernel_size, kernel_size),1);
medianBlur(noise, median, kernel_size);
imshow("Original image", img);
imshow("noise_image", noise);
imshow("gaussian_image", gaussian);
imshow("median_image", median);
waitKey(0);
return 0;
}