Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: issue with MATLAB returning zero as result
Replies: 2   Last Post: Apr 4, 2013 9:50 AM

 Messages: [ Previous | Next ]
 Steven Lord Posts: 18,038 Registered: 12/7/04
Re: issue with MATLAB returning zero as result
Posted: Apr 3, 2013 2:52 PM

"Tom " <thomasburrows@nhs.net> wrote in message
news:kjhi7l\$jrm\$1@newscl01ah.mathworks.com...
> Hi,
>
> This is probably really basic but I have the following code:

*snip*

>
> %individual pixel
> %hr=2.7006e+04;
> %hg=3.0236e+04;
> %hb=1.9838e+04;
>
> hr=img(175,300,1);
> hg=img(175,300,2);
> hb=img(175,300,3);
>
> dr=((rc.*(hr-ra)))./((ra+rb)-hr)
> dg=((gc.*(hg-ga)))./((ga+gb)-hg)
> db=((bc.*(hb-ba)))./((ba+bb)-hb)
>
> The coefficient values were obtained from a previous programme and I need
> to use them to calculate a value for the red green and blue channels for
> each pixel of an image. I will be optimising between the 3 channels so
> right now I am just working with one pixel. If I insert the RGB values for
> a pixel after looking them up I get sensible results (the points marked
> out with a % sign above) but if I try to have matlab look them up using
> hr=img(175,300,1) etc the dr, dg and db values are returned as zero. This
> doesnt make sense to me as they are the same value. Can anyone see
> something I cant, its probably something basic enough but I dont
> understand why it returns a zero value.

Check the class of img. I strongly suspect it will be one of:

uint8
uint16
uint32

Unsigned integers cannot store negative values; the smallest value
representable in any of those types is 0. Because img is an unsigned integer
class, all the variables hr, hg, and hb will be of that same class. Since
ra, ga, and ba are all large values (larger than the corresponding commented
out "individual pixel" values) the result of (hr-ra) [and the similar
calculations for hg and hb] likely will saturate at 0.

http://www.mathworks.com/help/matlab/matlab_prog/integers.html

When you typed in the values manually, they were treated as double precision
values and double precision CAN store negative values; there's no saturation
at 0.

--
Steve Lord
slord@mathworks.com