【问题标题】:How to add Noise to Color Image - Opencv如何为彩色图像添加噪点 - Opencv
【发布时间】:2015-11-07 09:59:47
【问题描述】:

我正在尝试为图像添加噪点,然后对其进行降噪以测试我的降噪算法!所以对于基准我指的是这个Online Test samples。我正在尝试复制噪声模型。

参考这个线程 1 ,2 我正在为这样的图像添加噪音!

Mat mSource_Bgr;
mSource_Bgr= imread(FileName_S,1);

double m_NoiseStdDev=10;

Mat mNoise_Bgr = mSource_Bgr.clone();
Mat mGaussian_noise = Mat(mSource_Bgr.size(),CV_8UC3);

randn(mGaussian_noise,0,m_NoiseStdDev);
mNoise_Bgr += mGaussian_noise;

normalize(mNoise_Bgr,mNoise_Bgr,0, 255, CV_MINMAX, CV_8UC3);

imshow("Output Window",mNoise_Bgr);
//imshow("Gaussian Noise",mGaussian_noise);

我的输入图像

输出带噪点的图像

问题:

向图像添加噪点改变图像的整体亮度,这反过来又改变了我的最终结果 PSNR!

我想得到尽可能接近to this one 的结果!

到目前为止我已经尝试过什么!

我尝试只在颜色通道中添加噪点。

  1. 将输入图像转换为 YUV 颜色空间

  2. 仅在 UV 颜色通道中添加噪波并保持 Y 通道不变。

    结果非常糟糕,图像的整体颜色正在改变!如果需要,将添加代码!

因此,非常感谢您对此提出任何建议!可能会给我一些公式来为图像添加噪点!

【问题讨论】:

    标签: opencv image-processing noise


    【解决方案1】:

    感谢@Andrey Smorodov 的见解! 我让它工作了!这是我在彩色图像中添加噪声的更新代码。希望这对某人有用!

    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/highgui/highgui.hpp"
    
    #include <iostream>
    
    using namespace cv;
    using namespace std;
    
    inline BYTE Clamp(int n)
    {
        n = n>255 ? 255 : n;
        return n<0 ? 0 : n;
    }
    
    bool AddGaussianNoise(const Mat mSrc, Mat &mDst,double Mean=0.0, double StdDev=10.0)
    {
        if(mSrc.empty())
        {
            cout<<"[Error]! Input Image Empty!";
            return 0;
        }
    
        Mat mGaussian_noise = Mat(mSrc.size(),CV_16SC3);
        randn(mGaussian_noise,Scalar::all(Mean),Scalar::all(StdDev));
    
        for (int Rows = 0; Rows < mSrc.rows; Rows++)
        {
            for (int Cols = 0; Cols < mSrc.cols; Cols++)
            {
                Vec3b Source_Pixel= mSrc.at<Vec3b>(Rows,Cols);
                Vec3b &Des_Pixel= mDst.at<Vec3b>(Rows,Cols);
                Vec3s Noise_Pixel= mGaussian_noise.at<Vec3s>(Rows,Cols);
    
                for (int i = 0; i < 3; i++)
                {
                    int Dest_Pixel= Source_Pixel.val[i] + Noise_Pixel.val[i];
                    Des_Pixel.val[i]= Clamp(Dest_Pixel);
                }
            }
        }
    
        return true;
    }
    
    bool AddGaussianNoise_Opencv(const Mat mSrc, Mat &mDst,double Mean=0.0, double StdDev=10.0)
    {
        if(mSrc.empty())
        {
            cout<<"[Error]! Input Image Empty!";
            return 0;
        }
        Mat mSrc_16SC;
        Mat mGaussian_noise = Mat(mSrc.size(),CV_16SC3);
        randn(mGaussian_noise,Scalar::all(Mean), Scalar::all(StdDev));
    
        mSrc.convertTo(mSrc_16SC,CV_16SC3);
        addWeighted(mSrc_16SC, 1.0, mGaussian_noise, 1.0, 0.0, mSrc_16SC);
        mSrc_16SC.convertTo(mDst,mSrc.type());
    
        return true;
    }
    
    
    int main(int argc, const char* argv[])
    {
        Mat mSource= imread("input.png",1); 
        imshow("Source Image",mSource);
    
        Mat mColorNoise(mSource.size(),mSource.type());
    
        AddGaussianNoise(mSource,mColorNoise,0,10.0);
    
        imshow("Source + Color Noise",mColorNoise); 
    
    
        AddGaussianNoise_Opencv(mSource,mColorNoise,0,10.0);//I recommend to use this way!
    
        imshow("Source + Color Noise OpenCV",mColorNoise);  
    
        waitKey();
        return 0;
    }  
    

    【讨论】:

      【解决方案2】:

      看起来您的噪声矩阵无法获得负值,因为它具有 unsigned char 元素类型。尝试使用实值矩阵进行操作,它应该会有所帮助。

      【讨论】:

      • 正态分布以零为中心的噪声范围将是正反两面。 CV_8UC3 会修剪负片部分,所以平均值会大于零,这会改变图像的平均亮度。
      【解决方案3】:

      主要有两种方法可以将 awgn 噪声(平均值 = 0,标准差 = 30)添加到彩色图像。

      首先:您可以将均值 = 0、标准差 = 30 的 awgn 噪声分别添加到红色、绿色和蓝色通道中的每个通道(或任何其他颜色模型 - HSI、YUV、Lab);然后结合噪声通道形成彩色噪声图像。

      第二:使用内置功能直接为彩色图像添加噪点。例如。 Matlab 中的 imnoise()。

      我尝试了这两种方法(imnoise 和独立),我得到了相同的结果。

      【讨论】:

        【解决方案4】:

        您提到“我尝试仅在颜色通道中添加噪点。 将输入图像转换为 YUV 颜色空间 仅在 UV 颜色通道中添加噪波,并保持 Y 通道不变。”

        如果您使用的是 YUV 颜色模型,我建议您反其道而行之。保持 U 和 V 通道不变,仅向 Y 通道添加噪声。

        【讨论】:

          猜你喜欢
          • 2012-07-08
          • 1970-01-01
          • 1970-01-01
          • 2015-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-12
          • 1970-01-01
          相关资源
          最近更新 更多