Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Adding Gaussian Noise in OpenCV Mat Image

Hi Everyone! I have been trying to add Additive White Gaussian Noise in my Mat image(Using Qt 5.2.0) using the following piece of code, but i am getting the original image only. No noise is being added! Please can anyone help me by telling where I am going wrong!

    cv::Mat gaussianD;
    cv::Mat gaussian = grImage.clone();
    gaussian.convertTo(gaussianD, CV_32F);
    cv::Mat mult = cv::Mat::zeros(grImage.rows, grImage.cols, CV_32F);
    cv::randn(mult, 0, ui->doubleSpinBox_sDev->value());
    gaussianD = gaussianD + sqrt(ui->doubleSpinBox_sDev->value())*mult;
    gaussianD.convertTo(gaussianD, CV_8U);
    cv::namedWindow("Gaussian Noise");
    cv::imshow("Gaussian Noise", gaussianD);
    cv::imwrite("gaussian.jpg", gaussianD);
    gauss.load("gaussian.jpg");
    ui->label_NoiseImg->setPixmap(QPixmap::fromImage(gauss.scaled(ui->label_NoiseImg->size(), Qt::KeepAspectRatio)));

Adding Gaussian Noise in OpenCV Mat Image

Hi Everyone! I have been trying to add Additive White Gaussian Noise in my Mat image(Using Qt 5.2.0) using the following piece of code, but i am getting the original image only. No noise is being added! Please can anyone help me by telling where I am going wrong!

    cv::Mat gaussianD;
    cv::Mat gaussian = grImage.clone();
    gaussian.convertTo(gaussianD, CV_32F);
    cv::Mat mult = cv::Mat::zeros(grImage.rows, grImage.cols, CV_32F);
    cv::randn(mult, 0, ui->doubleSpinBox_sDev->value());
    gaussianD = gaussianD + sqrt(ui->doubleSpinBox_sDev->value())*mult;
    gaussianD.convertTo(gaussianD, CV_8U);
    cv::namedWindow("Gaussian Noise");
    cv::imshow("Gaussian Noise", gaussianD);
    cv::imwrite("gaussian.jpg", gaussianD);
    gauss.load("gaussian.jpg");
    ui->label_NoiseImg->setPixmap(QPixmap::fromImage(gauss.scaled(ui->label_NoiseImg->size(), Qt::KeepAspectRatio)));

Here grImage is my original grayscale image, double spinbox s_Dev gives the value of the variance defined by the user, and mult is the array of gaussian random nos.