The so-called salt and pepper early refers to randomly set some pixels to white or black. During transmission, the noise can occur when some pixels of the image are lost.
#include <iostream> #include <string> #include <opencv2\core\core.hpp> #include <opencv2\highgui
\highgui.hpp> using namespace CV;
Read into the picture Mat Readimage (String imgname);
Show picture void ShowImage (String windowname, Mat ima);
Salt and pepper function void salt (cv::mat &image, int n);
Save picture void SaveImage (String imageName, Mat ima);
void Main () {String imgname = "381066140_1536_864.jpg";
Open Image Mat img = readimage (imgname);
Set salt and pepper to increase the noise point (IMG, 40000);
ShowImage ("Salt", IMG);
Save Picture SaveImage ("Salt.jpg", IMG);
}//Salt and pepper function, set to white noise void salt (cv::mat &image, int n) {for (int k = 0; k < n; k++) {//Generate random number of rows and columns
int icol = rand ()% Image.cols;
int jrow = rand ()% Image.rows;
Determine if the grayscale image if (1 = = Image.channels ()) {image.at<uchar> (Jrow, icol) = 255; }//Determine if it is a color image else if (3 = = Image.channels ()) {image.at<cv::vec3b> (Jrow, Icol) [0] = 25
5; Image.at<cv::vec3b> (Jrow, Icol) [1] = 255;
Image.at<cv::vec3b> (Jrow, Icol) [2] = 255;
}}}//Show picture void ShowImage (String windowname, Mat ima) {imshow (windowname, IMA);
Waitkey (0);
}//Read into the picture Mat readimage (String imgname) {Mat ima = imread (imgname);
Cvnamedwindow ("Charater2");
Imshow ("C2", IMA);
Waitkey (0);
return ima;
}//Save picture void SaveImage (String imageName, Mat ima) {imwrite (imageName, IMA);}
The operation results are as follows
Original
Add the image after the salt and pepper noise point
-
-