-1

I have MATLAB code which is adding some noise to image. Now, I want to show histogram of the noise. I can perform a histogram of the original image and the noisy image and then I can find the difference between them but I'm not sure if that is right.

Here is my code:

O=im2double(rgb2gray(imread('image2.jpg')));
G=imnoise(O,'salt & pepper',0.1);
%imshow([O G]);
%imhist(O);
imhist(G);

How can I show just the noise which I added to image?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
nec
  • 51
  • 1
  • 8
  • 1
    What have you tried? Playing around with Matlab is half the fun. Did you subtract the 2 images or histograms? Did you divide the 2 images? What about taking the eigenvalues of the inverses of the images? What about applying the noise to an empty image? All kinds of goofy things can be attempted. – Matt Dec 01 '16 at 14:47

1 Answers1

1

It is possible, but it requires a bit of a trick to do it. Also because this is impulsive noise, this is a much simpler problem. What you have to do is seed the random generator first, then corrupt your image with the noise. After that, reseed the random generator, and corrupt a gray image with the same kind of noise. You use a gray image because impulsive or salt and pepper noise corrupts your image with black and white pixels and so whatever isn't gray would be noise. You would then find the histogram of this image, then set the gray value frequency to 0. What is left would be the noise you desire.

By seeding the random generator and because the noise generation is random, you are guaranteeing that the same noise sequence is output after you seed.

Let's take the cameraman image for example:

O = im2double(imread('cameraman.tif'));

It looks like:

imshow(O);

enter image description here

Now, let's seed the random generator, corrupt the image, reseed the random generator and corrupt a gray image:

rng(123); % Seed random generator
G = imnoise(O, 'salt & pepper', 0.1); % Corrupt image
rng(123); % Reseed random generator
H = imnoise(0.5*size(O), 'salt & pepper', 0.1); % Corrupt gray image

Let's see what the corrupted image and the noise on the gray image looks like:

figure;
imshow(G); 
figure;
imshow(H);

enter image description here

enter image description here

If you compare them side-by-side, you'll see that the noise profiles are exactly the same between both images. Now, do a histogram of the gray noisy image, then remove the bin at 128, 0.5 or gray:

h = imhist(H);
h(129) = 0;
bar(h);

We now get:

enter image description here

rayryeng
  • 102,964
  • 22
  • 184
  • 193