Ask Your Question
0

add gaussian noise to colour image

asked 2015-12-28 03:27:32 -0500

Hossain Md Shakhawat gravatar image

I want to add gaussian noise to colour image where the standard deviation of gaussian noise were varied from 0.2 to 2 at 0.2 intervals.

edit retag flag offensive close merge delete

Comments

And what have you tried?

LorenaGdL gravatar imageLorenaGdL ( 2015-12-28 03:30:04 -0500 )edit

I have tried to separate the colour channels and add noise to each of them and merge them. And succeed to do so. But I want to separate the Y Cr Cb components and then add noise to only Y. Finally merge the components but could not. My code for colour channels:

for (int i=0;i<3;i++) {

    Mat noise = Mat(channel[i].size(),CV_64F);

    normalize(channel[i], result[i], 0.0, 1.0, CV_MINMAX, CV_64F);
    randn(noise, 0, .2);
    result[i] = result[i] + noise;
    normalize(result[i], result[i], 0.0, 1.0, CV_MINMAX, CV_64F);

    result[i].convertTo(result[i], CV_32F, 255, 0);
        }


merge(result,3,input);
imwrite("/Users/hossainmdshakhawat/developer/subimg/Noisy/merge.jpg",input);
Hossain Md Shakhawat gravatar imageHossain Md Shakhawat ( 2015-12-28 03:41:17 -0500 )edit

And why can't you add noise just to channel Y? Are you getting any errors or what?

LorenaGdL gravatar imageLorenaGdL ( 2015-12-28 03:53:05 -0500 )edit

I can split the Y Cr, and Cb. I can add noise to Y too. But while I am trying to merge I am getting assertion error.

Hossain Md Shakhawat gravatar imageHossain Md Shakhawat ( 2015-12-28 04:00:32 -0500 )edit

which assertion error? and with which exact code? Please, provide all the relevant information, we can't imagine things...

LorenaGdL gravatar imageLorenaGdL ( 2015-12-28 05:48:13 -0500 )edit

my code:

Mat ycrcb = input.clone(); cvtColor(input, ycrcb, CV_BGR2YCrCb);

Mat channel[3];
Mat result[3];

split(ycrcb,channel);

    Mat noise = Mat(channel[0].size(),CV_64F);


    normalize(channel[0], result[0], 0.0, 1.0, CV_MINMAX, CV_64F);
    randn(noise, 0, .1);
    result[0] = result[0] + noise;
    normalize(result[0], result[0], 0.0, 1.0, CV_MINMAX, CV_64F);

    result[0].convertTo(channel[0], CV_32F, 255, 0);

    imwrite("gaussiannoisy.jpg",channel[0]);

Mat output;
merge(channel,3,output);

imwrite("merge.jpg",output);
Hossain Md Shakhawat gravatar imageHossain Md Shakhawat ( 2015-12-28 06:13:11 -0500 )edit

This is the error OpenCV Error: Assertion failed (mv[i].size == mv[0].size && mv[i].depth() == depth) in merge, file /Users/hossainmdshakhawat/opencv-3.0.0/modules/core/src/convert.cpp, line 950 libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/hossainmdshakhawat/opencv-3.0.0/modules/core/src/convert.cpp:950: error: (-215) mv[i].size == mv[0].size && mv[i].depth() == depth in function merge

Hossain Md Shakhawat gravatar imageHossain Md Shakhawat ( 2015-12-28 06:16:50 -0500 )edit

Please suggest is there any better and simple way to add noise to colour with varying std of gaussian noise.

Hossain Md Shakhawat gravatar imageHossain Md Shakhawat ( 2015-12-28 06:23:24 -0500 )edit

You're modifying Y channel and converting it to CV_32F, but your Cr and Cb channels are still CV_8U. Therefore, you can't merge them (all of them have to have the same depth).

LorenaGdL gravatar imageLorenaGdL ( 2015-12-28 06:24:02 -0500 )edit

Thanks it fixed the error but the resulting noisy image is some how different. It is a yellowish image. And this is not my desired target. Is my way is wrong ? I am very new at opencv and image processing

Hossain Md Shakhawat gravatar imageHossain Md Shakhawat ( 2015-12-28 06:28:42 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-12-28 23:49:15 -0500

Please refer this! I think it is a possible duplicate of this question.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-12-28 03:27:32 -0500

Seen: 1,546 times

Last updated: Dec 28 '15