salt noise

2018-11-21  本文已影响0人  zeem0ny
#include<opencv2/opencv.hpp>
#include<iostream>
#include<cstdlib> 
#include<vector>
using namespace std;
using namespace cv;

#define WINDOW_1 "椒盐噪声点1"
#define WINDOW_2 "椒盐噪声点2"

void salt(Mat&, int);
int main()
{

    Mat srcImage = imread("D://vvoo//lena.jpg");

    Mat grayImage;

    cvtColor(srcImage, grayImage/*, CV_8UC1*/, CV_RGB2GRAY);

    imshow("原图", srcImage);

 

    namedWindow(WINDOW_1, WINDOW_AUTOSIZE);

    Mat g_dstImage = grayImage.clone();

    Mat dstImage = srcImage.clone();

    salt(g_dstImage, 3000);

    salt(dstImage, 3000);

    

    imshow(WINDOW_1, g_dstImage);

    imshow(WINDOW_2, dstImage);

    waitKey(0);

    return 0;

}

void salt(Mat&dst, int Saltnum)

{

    int x,y;

    for (int i = 0;i < Saltnum; i++)

    {

        x = rand() % dst.cols;//保证x和y都在src的行数和列数范围内

        y = rand() %dst.rows;

        if (dst.channels() == 1)

        {

            dst.at<uchar>(y, x) = 0;

        }

        if (dst.channels() ==3)

        {

            dst.at<Vec3b>(y, x)[0] = 0;

            dst.at<Vec3b>(y, x)[1] = 0;

            dst.at<Vec3b>(y, x)[2] = 0;

        }

    }

}
上一篇下一篇

猜你喜欢

热点阅读