zoukankan      html  css  js  c++  java
  • [转]图像去噪的OpenCV添加噪声和去噪

    图像去噪的OPenCV添加噪声和去噪

    原文地址:http://blog.csdn.net/margaret_wangrui/article/details/51939153
     

    目录(?)[+]

     

    添加噪声

    1. 添加高斯噪声
    IplImage* AddGuassianNoise(IplImage* src)    //添加高斯噪声
    {
        IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
        IplImage* noise = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
        CvRNG rng = cvRNG(-1);
        cvRandArr(&rng,noise,CV_RAND_NORMAL,cvScalarAll(0),cvScalarAll(25));
        cvAdd(src,noise,dst);
        return dst;
    }
    1. 添加椒盐噪声
    IplImage* AddPepperSaltNoise(IplImage* src)    //添加椒盐噪声,随机黑白点
    {
        IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
        cvCopy(src, dst);
        for(int k=0; k<(src->height*src->width*P); k++)
        {
            int i = rand()%src->height;
            int j = rand()%src->width;
            int m = rand()%2;
            CvScalar s = cvGet2D(src, i, j);
            if(src->nChannels == 1)
            {
               if(m==0)
               {
                   s.val[0] = 255;
               } 
               else 
               {
                   s.val[0] = 0;
               }
            }
            else if(src->nChannels==3)
            {
               if(m==0)
               {
                  s.val[0]=255;
                  s.val[1]=255;
                  s.val[2]=255;
               }
               else 
               {
                  s.val[0]=0;
                  s.val[1]=0;
                  s.val[2]=0;
               }
            }
            cvSet2D(dst, i, j, s);
        }
        return dst;
    }

    实现滤波器

    1、 算术均值滤波器

    //算术均值滤波器——模板大小5*5
    IplImage* ArithmeticMeanFilter(IplImage* src)
    {
        IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
        cvSmooth(src,dst,CV_BLUR,5); 
        return dst;
    }

    2、 几何均值滤波器

    //几何均值滤波器——模板大小5*5
    IplImage* GeometryMeanFilter(IplImage* src)
    {
        IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
        int row, col;
        int h=src->height;
        int w=src->width;
        double mul[3];
        double dc[3];
        int mn;
        //计算每个像素的去噪后color值
        for(int i=0;i<src->height;i++){
            for(int j=0;j<src->width;j++){
                mul[0]=1.0;
                mn=0;
                //统计邻域内的几何平均值,邻域大小5*5
                for(int m=-2;m<=2;m++){
                    row = i+m;
                    for(int n=-2;n<=2;n++){
                        col = j+n;
                        if(row>=0&&row<h && col>=0 && col<w){
                            CvScalar s = cvGet2D(src, row, col);
                            mul[0] = mul[0]*(s.val[0]==0?1:s.val[0]);   //邻域内的非零像素点相乘
                            mn++;
                        }
                    }
                }
                //计算1/mn次方
                CvScalar d;
                dc[0] = pow(mul[0], 1.0/mn);
                d.val[0]=dc[0];
                //统计成功赋给去噪后图像。
                cvSet2D(dst, i, j, d);
            }
        }
        return dst;
    }

    3、谐波均值滤波器

    //谐波均值滤波器——模板大小5*5
    IplImage* HarmonicMeanFilter(IplImage* src)
    {
        IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
        int row, col;
        int h=src->height;
        int w=src->width;
        double sum[3];
        double dc[3];
        int mn;
        //计算每个像素的去噪后color值
        for(int i=0;i<src->height;i++){
            for(int j=0;j<src->width;j++){
                sum[0]=0.0;
                mn=0;
                //统计邻域,5*5模板
                for(int m=-2;m<=2;m++){
                    row = i+m;
                    for(int n=-2;n<=2;n++){
                        col = j+n;
                        if(row>=0 && row<h && col>=0 && col<w){
                            CvScalar s = cvGet2D(src, row, col);
                            sum[0] = sum[0]+(s.val[0]==0?255:255/s.val[0]);
                            mn++;
                        }
                    }
                }
                CvScalar d;
                dc[0] = mn*255/sum[0];
                d.val[0]=dc[0];
                //统计成功赋给去噪后图像。
                cvSet2D(dst, i, j, d);
            }
        }
        return dst;
    }

    4、逆谐波均值滤波器

    //逆谐波均值大小滤波器——模板大小5*5
    IplImage* InverseHarmonicMeanFilter(IplImage* src)
    {
        IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
        //cvSmooth(src,dst,CV_BLUR,5); 
        int row, col;
        int h=src->height;
        int w=src->width;
        double sum[3];
        double sum1[3];
        double dc[3];
        double Q=2;
        //计算每个像素的去噪后color值
        for(int i=0;i<src->height;i++){
            for(int j=0;j<src->width;j++){
                sum[0]=0.0;
                sum1[0]=0.0;
                //统计邻域
                for(int m=-2;m<=2;m++){
                    row = i+m;
                    for(int n=-2;n<=2;n++){
                        col = j+n;
                        if(row>=0&&row<h && col>=0 && col<w){
                            CvScalar s = cvGet2D(src, row, col);
                            sum[0] = sum[0]+pow(s.val[0]/255, Q+1);
                            sum1[0] = sum1[0]+pow(s.val[0]/255, Q);
                        }
                    }
                }
                //计算1/mn次方
                CvScalar d;
                dc[0] = (sum1[0]==0?0:(sum[0]/sum1[0]))*255;
                d.val[0]=dc[0];
                //统计成功赋给去噪后图像。
                cvSet2D(dst, i, j, d);
            }
        }
        return dst;
    }
  • 相关阅读:
    Lambda
    Thread&线程池
    异常
    Map
    List and Set
    Collection和迭代器Iterator
    Object类,常用API
    (一)自定义 mybatis 之框架介绍
    Nginx三大功能及高并发分流
    http协议改为https
  • 原文地址:https://www.cnblogs.com/Crysaty/p/6495341.html
Copyright © 2011-2022 走看看