1 前备知识

  图像噪声产生的原因很复杂,有的可能是数字信号在传输过程中发生了丢失或者受到干扰,有的是成像设备或者环境本身导致成像质量不稳定,反应到图像上就是图像的亮度与颜色呈现某种程度的不一致性。从噪声的类型上,常见的图像噪声可以分为如下几种:

- 椒盐噪声, 是一种随机在图像中出现的稀疏分布的黑白像素点, 对椒盐噪声一种有效的去噪手段就是图像中值滤波。

- 高斯噪声,符合高斯分布 一般会在数码相机的图像采集(acquisition)阶段发生,这个时候它的物理/电/光等各种信号都可能导致产生高斯分布噪声。

- 均匀分布噪声,均匀/规则噪声一般都是因为某些规律性的错误导致的。

2 所用到的主要OpenCv API

/** @brief Fills the array with normally distributed random numbers.
The function cv::randn fills the matrix dst with normally distributed random numbers with the specified
mean vector and the standard deviation matrix. The generated random numbers are clipped to fit the
value range of the output array data type.
@param dst output array of random numbers; the array must be pre-allocated and have 1 to 4 channels.
@param mean mean value (expectation) of the generated random numbers.
@param stddev standard deviation of the generated random numbers; it can be either a vector (in
which case a diagonal standard deviation matrix is assumed) or a square matrix.
@sa RNG, randu
*/
CV_EXPORTS_W void randn(InputOutputArray dst, InputArray mean, InputArray stddev);

3 程序代码

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

void add_salt_pepper_noise(Mat &image);
void gaussian_noise(Mat &image);
int main(int artc, char** argv) {
    Mat src = imread("images/cos.jpg");
    Mat dstGauseNoise;
    Mat dstSaultNoise;
    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input", CV_WINDOW_AUTOSIZE);
    imshow("input", src);
    gaussian_noise(src);
    add_salt_pepper_noise(src);

    waitKey(0);
    return 0;
}

void add_salt_pepper_noise(Mat &image) {
    Mat _image = image.clone();
    RNG rng(12345);
    int h = _image.rows;
    int w = _image.cols;
    int nums = 10000;
    for (int i = 0; i < nums; i++) {
        int x = rng.uniform(0, w);
        int y = rng.uniform(0, h);
        if (i % 2 == 1) {
            _image.at<Vec3b>(y, x) = Vec3b(255, 255, 255);
        }
        else {
            _image.at<Vec3b>(y, x) = Vec3b(0, 0, 0);
        }
    }
    imshow("salt pepper", _image);
}

void gaussian_noise(Mat &image) {
    Mat noise = Mat::zeros(image.size(), image.type());
    randn(noise, (15, 15, 15), (30, 30, 30));
    Mat dst;
    add(image, noise, dst);
    imshow("gaussian noise", dst);
}

4 运行结果

原图:

5 扩展及注意事项

none

6*目前只做大概了解,知道有这一算法,后续具体使用再做具体分析

01-02 12:17