Post by mkWell, its not directly in the imresize function...
[...]
Post by mkto make a long story short, I then did a floor of these unrounded
values and compared it to matlab's built in function, and it came out
exact.
I guess I don't understand where the floor is coming from. Here is
the "quantization" code from grayto8.c
for (k = 0; k < numElements; k++)
{
val = *pr++;
if (mxIsNaN(val)) {
*qr++ = 0;
}
else {
val = val * 255.0 + 0.5;
if (val > 255.0) val = 255.0;
if (val < 0.0) val = 0.0;
*qr++ = val;
}
}
Could the floor be performed because the value .5 is being added to
each quantized pixel value? (val = val * 255.0 + 0.5;)
It is precisely this 0.5 which makes the operation round INSTEAD of
floor. In C, casting from a floating-point to an integer is always a
truncation (towards zero). Adding 0.5 before truncating is equivalent
to rounding (at least for positive values).
What code do you use for converting [0,1] image data to 8-bit
representation?
Now on second thought, something seems funny about this conversion.
The *255 means that half as many floats around 0.0 map to uint8 0, and
half as many around 1.0 map to uint8 255, compared to all the other
integers.
[0 , .5/255] => 0
[0.5/255, 1.5/255] => 1
[1.5/255, 2.5/255] => 2
...
[254.5/255, 1] => 255
That's a range of .5/255 for results 0 and 255, and 1/255 for all the
others. Plot this to see what I'm talking about:
x=linspace(0,1,1e5); v=floor(255*x+0.5); plot(x,v);
I think I'd rather map [0,1/256] => 0, up to [255/256,256] => 255,
which is more uniform.
Can anyone explain the preference for the code above?
--
Peter Boettcher <***@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/