• OpenCv 024---图像噪声


    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 &src, Mat &dst);
    void gaussian_noise(Mat &src, Mat &dst);
    int main(int artc, char** argv) {
        Mat src = imread("images/cos.jpg");
        Mat dstGauseNoise = src.clone();
        Mat dstSaultNoise =src.clone();
        //dstSaultNoise.create(src.rows, src.cols, src.type());
        if (src.empty()) {
            printf("could not load image...
    ");
            return -1;
        }
        namedWindow("input", CV_WINDOW_AUTOSIZE);
        imshow("input", src);
        gaussian_noise(src, dstGauseNoise);
        add_salt_pepper_noise(src, dstSaultNoise);
    
        imshow("salt pepper", dstSaultNoise);
        imshow("gaussian noise", dstGauseNoise);
    
        waitKey(0);
        return 0;
    }
    
    void add_salt_pepper_noise(Mat &src,Mat &dst) {
        RNG rng(12345);
        int h = src.rows;
        int w = src.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) {
                dst.at<Vec3b>(y, x) = Vec3b(255, 255, 255);
            }
            else {
                dst.at<Vec3b>(y, x) = Vec3b(0, 0, 0);
            }
        }
        //imshow("salt pepper", src);
    }
    
    void gaussian_noise(Mat &src,Mat &dst) {
        Mat noise = Mat::zeros(src.size(), src.type());
        randn(noise, (15, 15, 15), (30, 30, 30));
        add(src, noise, dst);
        //imshow("gaussian noise", dst);
    }

    4 运行结果

    原图:

     

    5 扩展及注意事项

    none

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

    One day,I will say "I did it"
  • 相关阅读:
    三层架构之解耦
    自动升级 组件
    C语言常量与指针
    ASP.NET MVC Model元数据
    Web层后端权限模块
    java中文排序问题(转)
    JDWP
    bat执行java程序的脚本解析
    jdom dom4j解析xml不对dtd doctype进行验证(转)
    Dom4j SAXReader Constructors
  • 原文地址:https://www.cnblogs.com/Vince-Wu/p/11833171.html
Copyright © 2020-2023  润新知