Image subtraction negative pixels rounded to 0
Show older comments
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
More Answers (2)
Image Analyst
on 17 Feb 2016
1 vote
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
Matt J
on 17 Feb 2016
newimg = abs(double(image) - double(backgroundImage))
5 Comments
Faizan Tahir
on 17 Feb 2016
Edited: Faizan Tahir
on 17 Feb 2016
Faizan Tahir
on 17 Feb 2016
Image Analyst
on 17 Feb 2016
Please attach your image with the green and brown frame icon. Don't make us work blind.
Faizan Tahir
on 17 Feb 2016
Image Analyst
on 17 Feb 2016
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.
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!