Image Processing with data of type single

10 views (last 30 days)
Hi,
I need your help in this issue. I have an image(X) with data type 'single'. When I am trying to add noise to this image using "imnoise"(say J = imnoise(X,'salt & pepper',0.5);), my output(noise corrupted) image(J) is loosing the all the decimal components(well the name of the data type is still single, but it looses all the decimal parts of the data) and data is getting rounded to 0's and 1's.
Is there any way that I can add noise to the image and can still preserve the decimal part, so that I do not loose any information?.
Kindly help me out.
Thanks in advance.
  4 Comments
Walter Roberson
Walter Roberson on 12 Nov 2018
How do you know that the image() is getting converted to integer?
L C
L C on 12 Nov 2018
May be the range of the input image X is not in [0 1].
You can do this X = X/255;
or
X = im2single(X);

Sign in to comment.

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 12 Nov 2018
For sake of simplicity - convert every image to double if you possibly can memory-wise. That way you won't have to deal with obscure implicit typecastings and other numerical issues (integer divisions ,ffts on and on...).
HTH

Guillaume
Guillaume on 12 Nov 2018
Edited: Guillaume on 12 Nov 2018
Despite your statement, at some point, the image must have been converted from integer type to single, and the conversion wasn't done properly.
When images are of type single or double matlab expects that the intensities are in the range [0-1]. Anything above 1 is the same as 1 as far as matlab is concerned. So if you were to convert a uint8 image whose range is [0-255] to single naively with single(), the original range would be preserved and for matlab intensity [1-254] would all be the same: 1. The proper way to convert an image to single is with im2single() which will divide the intensities by 255 (for uint8 images) to get the right range.
Indeed, if you apply imnoise to a single image whose intensities are integer in the range [0-255], you'll just get 0s and 1s in your output.
The best solution to your problem is to go back to your code, find where this incorrect conversion occurs and fix it. A workaround is to rescale the intensities yourself. Assuming that the intensity range of your single image is [0-255] (the most common, but check yourself with min and max), then:
X = X/255; %make sure range of intensity is [0-1]
J = imnoise(X,'salt & pepper',0.5)
  6 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 14 Nov 2018
True, the majority will be such images, and therein lies "des pudels kern".
In addition to 8-bit integers there are 16-bit integers and some standard image formats (tiff) allow that and FITS allows 32 and 64 bit int as well.
On you other points I completely agree - the image processing toolbox seems to have "two parrents" or a bit of a "split personality", and perhaps that is understandable though not preferable.
Guillaume
Guillaume on 14 Nov 2018
For info, I've submitted an enhancement request for the documentation of imnoise so that it details the image requirements for the 'salt & pepper' option, the same way it does for all the other options.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!