why I fully lose dicom image after adding noise to it?

Dear all,
I am trying to add both Gaussian and salt & pepper noise to a dicom image using imnoise() but in any densities I completely lose the image it becomes fully black with white spots in it.
I would be glad if someone could help me.
The below is my image and my code.
img = dicomread('1.dcm');
density = 0.01;
%img = imnoise(img,'gaussian',density);
img = imnoise(img,'salt & pepper',density);

 Accepted Answer

imnoise expects pixel values of data type double and single to be in the range [0, 1]. You can use the rescale function to adjust pixel values to the expected range. If your image is type double or single with values outside the range [0,1], then imnoise clips input pixel values to the range [0, 1] before adding noise.
J = rescale(img);
img_noise = imnoise(J,'salt & pepper',0.01);

5 Comments

No worry. What you need is an additional work on scaling.
Following code shows the scaling using the maximum and minimum values from the original DICOM image.
The subtraction of two images (final vs original) in class uint16 shows the noise only, so the original image information is still there,
I think there are some other methods to achieve the same result, but may be I am too lazy and still use function rescale to do this. :-)
clear; clc;
filename = unzip('https://www.mathworks.com/matlabcentral/answers/uploaded_files/910755/1.zip');
I = dicomread(filename{1});
maxValue = double(max(max(I)));
minValue = double(min(min(I)));
%
J = rescale(I);
Jnoise = imnoise(J,'salt & pepper',0.01);
Irestore = uint16((Jnoise.*(maxValue-minValue))+minValue);
subplot(2,2,1)
imshow(I,[]);
title('Original DICOM Image');
subplot(2,2,2)
imshow(Jnoise,[]);
title('Rescale Image with noise');
subplot(2,2,3)
imshow(Irestore,[])
title('Image back to class uint16');
subplot(2,2,4)
Idiff = imabsdiff(Irestore,I);
imshow(Idiff,[]);
title('Subtraction of Image');
Thank you, it helped me to achive what I wanted
thank you, i m also have this doubt.
Thank you so much for indicating the scale function, for past 2 days I have this error and could not complete the scheduled task. Now its clear thank you very much simon chan

Sign in to comment.

More Answers (2)

class() has probably changed.
im2double(img) and imnoise() the results and im2uint8 or as appropriate to return to the original type.
There is a possibility that your image is int16 with signed data, especially if it is CT, and it might take a slight bit more work to get back to signed

5 Comments

my image is uint16, and it's MRI image
salt and pepper noise sets pixels to either their minimum or their maximum. Suppose that your useful data is in the range 4000 to 12000, but you add noise that is either 0 or 65535, then if you use imagesc() or imshow() with the [] option to rescale your image, then instead of the plot being based omn 12000-4000 = 8000 values, it is suddenly based on 65536 values, and the useful data would get washed out.
img = sort(sort(randi([1000 2000], 48, 64, 'uint16'),1),2);
density = 0.01;
%img = imnoise(img,'gaussian',density);
img2 = imnoise(img,'salt & pepper',density);
whos
Name Size Bytes Class Attributes density 1x1 8 double img 48x64 6144 uint16 img2 48x64 6144 uint16
[min(img(:)), min(img2(:))]
ans = 1×2
1000 0
[max(img(:)), max(img2(:))]
ans = 1×2
2000 65535
imshow(img)
imshow(img2)
what solution do you suggest me in order to work both with the gaussian and salt & pepper noises ?
Since your images are in the range 0-256, I'd just immediately cast them to uint8 right after you read them in
img = dicomread('1.dcm');
img = uint8(img);
After that, everything should be fine.
Thank you, It worked just fine

Sign in to comment.

Try this:
img = dicomread('1.dcm');
subplot(2, 2, 1);
imshow(img, []);
subplot(2, 2, 2);
imhist(img);
% Show min and max are 0 and 256,
% nowhere close to the uint16 range of 0 to 65,535.
min(img(:))
max(img(:))
density = 0.01;
%img = imnoise(img,'gaussian',density);
noisyImage = imnoise(img,'salt & pepper',density);
whos noisyImage
subplot(2, 2, 3);
imshow(noisyImage, [])
% Now rescale
noisyImage = imnoise(mat2gray(img),'salt & pepper',density);
subplot(2, 2, 4);
imshow(noisyImage, [])

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!