zoukankan      html  css  js  c++  java
  • 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"
  • 相关阅读:
    Postfix邮件服务器搭建及配置
    利用linux漏洞进行提权
    NFS部署和优化
    LAMP环境搭建
    Apache2.4.6服务器安装及配置
    linux笔记_防止ddos攻击
    CentOS6.5恢复误删除的文件
    linux计划任务
    linux软连接和硬链接
    linux用户和用户组的基本操作
  • 原文地址:https://www.cnblogs.com/Vince-Wu/p/11833171.html
Copyright © 2011-2022 走看看