OpenCV image access and salt and pepper noise

Source: Internet
Author: User

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
-
-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.