Image subtraction negative pixels rounded to 0

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.

 Accepted 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')]

More Answers (2)

Sounds like what you really need to do is color classification, not subtraction. You could simply call rgb2hsv and look for certain hues. Or call rgb2ind() and tell it to find 4 colors. See my File Exchange for color segmentation demos: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
newimg = abs(double(image) - double(backgroundImage))

5 Comments

That made things REALLY weird when thresholding, but it seems to work. Is there any best way to recognize foreground objects if I do background subtraction?
I'm trying to recognize 3 objects, a red, green and blue one, on the foreground image, and label them appropriately.
Thanks.
When I do this using int8 or int16 I get the following error: RGB images must be uint8, uint16, single, or double.
double works but the picture comes out funny (pixelly and can't even tell what's going on). Any suggestions.
Please attach your image with the green and brown frame icon. Don't make us work blind.
I have fixed the issue with double looking weird (I used abs(im2double(image) - im2double(background)). This returns me the image minus the background and the colours of the actual objects are showing (they didn't previously with greyscale). Is using im2double fine?
I never use it. I don't like how it scales the range of my images to 0-1. I like to leave the ranges in the original range, not scaled and shifted. I don't know how you're seeing the original colors. A subtraction image will not show original colors. For example a reddish object colored with RGB = [200, 100, 50] and another reddish object at [190,90, 40] will have a difference of [10,10,10] which is dark gray, not red.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!