Use NaN values in an image

23 views (last 30 days)
ivel78
ivel78 on 1 Oct 2015
Answered: Image Analyst on 3 Oct 2015
Hi all,
I am trying to put all the white values of an image to NaN so those values are not used on subsequent calculations over the image (mainly a co-ocurrence matrix)
The images are 16 bit per channel .tif files. First i normalize all the values to the interval 0,1 and then try to identify the white pixels:
imRGBn=mat2gray(imRGB,[0 65535]);
for i=1:size(imRGBn,1);
for j=1:size(imRGBn,2);
if (imRGBn(i,j,1)==1)&(imRGBn(i,j,2)==1)&(imRGBn(i,j,3)==1);
imRGBn(i,j,1)=NaN;
imRGBn(i,j,2)=NaN;
imRGBn(i,j,3)=NaN;
end
end
end
The code is pretty un-elegant I know, but i do not know where it is going wrong.
Any help is appreciated.
I need to convert this image to other colour spaces... does anyone know any issues with NaN values and colour space transformations?
Thanks in advance.
  2 Comments
Geoff Hayes
Geoff Hayes on 1 Oct 2015
ivel78 - you don't really mention what the problem is, only that but i do not know where it is going wrong. Are you observing any errors? If so, what are they? And if no errors, then what makes you think that something is incorrect?
According to mat2gray, the returned matrix I contains values in the range 0.0 (black) to 1.0 (full intensity or white). I take that to mean that, for your example, the matrix imRGBn is now two dimensional since it is grayscale. What are the dimensions for imRGBn? What does
size(imRGBn)
return.
If in fact it is two dimensional then you know what is incorrect in your code and so what will have to be replaced.
Note that it is good practice not to use i and j as indexing variables in your loops since MATLAB also uses i and j to represent the imaginary number.
Also, using == to compare two numbers is valid only if the numbers are integers. When they are floating point numbers, like in this case, you are not guaranteed that (for example) the number will be identical to 1. Instead, you should consider the difference between the number and one and compare this against a small number (tolerance): if less than this tolerance, then the number could be said to be one. For example, if we use eps be our tolerance, then the number x is considered to be one if
abs(x - 1.0) < eps
ivel78
ivel78 on 2 Oct 2015
Hi Geoff,
Thanks for your comments, i have slightly modified the code to change i and j and also have used your suggested way of comparing the values.
Before, the problem was that it was not doing anything, the values in the matrix were staying the same, now it is working.
Thanks very much for your help.
Ignacio

Sign in to comment.

Answers (2)

Geoff Hayes
Geoff Hayes on 2 Oct 2015
Moved comment to here
Note that it is good practice not to use i and j as indexing variables in your loops since MATLAB also uses i and j to represent the imaginary number.
Also, using == to compare two numbers is valid only if the numbers are integers. When they are floating point numbers, like in this case, you are not guaranteed that (for example) the number will be identical to 1. Instead, you should consider the difference between the number and one and compare this against a small number (tolerance): if less than this tolerance, then the number could be said to be one. For example, if we use eps be our tolerance, then the number x is considered to be one if

Image Analyst
Image Analyst on 3 Oct 2015
I'm not really sure how, or even if, graycomatrix() can handle NaNs. My recommendation is to just leave the image alone, use a matrix of NxN, where N is the number of unique gray levels in the image, and then crop off the last row and column, which essentially says that there were no pixels of the max value in the image. You can resize the matrix if you want it to be a different size.

Community Treasure Hunt

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

Start Hunting!