opencv - 如何为彩色图像添加噪声 - Opencv

标签 opencv image-processing noise

我正在尝试向图像添加噪声,然后对其进行降噪以测试我的降噪算法!所以对于基准,我指的是这个 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);

我的输入图像 enter image description here

带噪声的输出图像 enter image description here

问题:

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

我想得到尽可能近的结果to this one ! enter image description here

到目前为止我尝试了什么!

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

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

  2. 仅在 UV 颜色 channel 中添加噪声并保持 Y channel 不变。

    结果非常糟糕,图像的整体颜色发生了变化!如果需要,将添加代码!

因此非常感谢任何关于此的建议!可能会给我一些向图像添加噪声的公式!

最佳答案

谢谢@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;
}  

关于opencv - 如何为彩色图像添加噪声 - Opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32005094/

相关文章:

c - 如何使用openCV提取颜色

java - 使用 OpenCV/JavaCV 的透视图像

R:如何产生一个嘈杂的正弦函数

java - Tile Worley 示例

machine-learning - 假设数据正常但噪声均匀,如何从一组数据点中去除噪声?

opencv - 如何使用 protobuf 序列化 cv::Mat 对象?

python - Scikit-image:如何减少断线段、重叠线?

python - 我找不到适合新图像零点轮廓的方法

c++ - 如何在 WinCE 中创建 .bmp

android - 从 Camera2 图像中裁剪出一个矩形