MATLAB: Image subtraction negative pixels rounded to 0

absolute valuebackgroundextractionimageImage Processing Toolboxnegativeprocessing

Hi.
I'm doing some background modelling. I have a set of images with the same background but the foreground is changing. I'm taking the median pixels from the set of images to retreive the background and the background extraction is successful.
I am then concatenating the 3 channels for the extracted background as follows:
backgroundImage = uint8(cat(3,median_bg_RedValues,median_bg_GreenValues,median_bg_BlueValues));
However, when I try to do image – backgroundImage, any values that are negative are rounded to 0, resulting in a black background.
How can I take the absolute value of the pixel rather than rounding to 0? (..e:
newimg = abs(image - backgroundImage)
(which doesn't work – all negative values are rounded to 0)
I'm loading the other image as standard:
image = imread('test.jpg')
I've even tried changing uint8 to int8 but I get an error:
Error using – Integers can only be combined with integers of the same class, or scalar doubles.
How can I do this nicely?
Thanks for any help.

Best Answer

Your backgroundImage is stored in uint8. What possible values can be stored in a uint8 variable?
intmin('uint8'):intmax('uint8')
The Image Processing Toolbox has a function named IMABSDIFF, or you could choose a different data type that covers the range of values in your image as well as negative values. Perhaps converting both your original image and your backgroundImage to int8 will do?
intmin('int8'):intmax('int8')
If not there's always int16:
[intmin('int16') intmax('int16')]