Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background subtracting in MATLAB

I'm looking to do background subtracting on an image. I'm new to MATLAB and new to image processing/analysis, so sorry if any of this sounds stupid. 1) Other than imsubtract() are there other ways to do background subtracting (besides comparing one image to another)? 2) In the Math Works explanation for imsubtract() why do they make their structuring element a disk? This seems rather difficult so far because every time I try something, I end up not only subtracting the noisy background but also losing the parts of the image I want to look at!

like image 707
eiphyomin Avatar asked Jun 09 '10 17:06

eiphyomin


People also ask

What is background subtraction method?

The background subtraction method (BSM) is one of the most popular approaches to detecting objects. This algorithm works by comparing moving parts of a video to a background image and foreground image.

How do you subtract function in Matlab?

C = A - B subtracts array B from array A by subtracting corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.

What is image subtraction in image processing?

Image subtraction or pixel subtraction is a process whereby the digital numeric value of one pixel or whole image is subtracted from another image. This is primarily done for one of two reasons – levelling uneven sections of an image such as half an image having a shadow on it, or detecting changes between two images.

How to subtract the background image from the image in MATLAB?

To subtract the background image, you need a model of the background. The simplest model is an image captured as the background along with some allowable deviation (+/- 0-255). Then, background subtraction in MATLAB is pretty simple:

What is the difference between image subtraction and Background subtraction?

Consider a white background. Now, a black ball moves across the image. Image subtraction would always occlude the object or distort the color (depending on the implementation), whereas background subtraction should not affect pixels sufficiently different from the background model (foreground). – Anthony Dec 12 '11 at 15:02

Can I run code in the background using MATLAB?

See Binary Element-Wise Operations with Single and Double Operands (MATLAB Coder). Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool. This function fully supports thread-based environments.

How to subtract row and column vectors in MATLAB?

Subtract Row and Column Vectors. Create a 1-by-2 row vector and 3-by-1 column vector and subtract them. The result is a 3-by-2 matrix, where each (i,j) element in the matrix is equal to a(j) - b(i): a=[a1 a2], b=[b1b2b3], a- b=[a1-b1a2-b1a1-b2a2-b2a1-b3a2-b3].


2 Answers

What kind of image do you work with?

Background subtraction is easy. If you want to subtract a constant value, or a background with the same size as your image, you simply write img = img - background. imsubtract simply makes sure that the output is zero wherever the background is larger than the image.

Background estimation is hard. There you need to know what kind of image you're looking at, otherwise, the background estimation will fail.

If you have, for example, spot or line features that are either all dark on bright or bright on dark background, you can pass through with a local maximum filter (imdilate) or a local minimum filter (imerode), respectively, that is larger than your features, so that wherever you place the filter mask, there are some pixels that cover background. Also, you want the filter to have somewhat similar shape as the features. In your case, if you lose part of your image, you may want to try and make the filter larger (but not too large).

Instead of subtracting maximum or minimum, subtracting the median can work well, though you have to choose the filter size such that there's usually a majority of background pixels inside the filter mask. Unfortunately, median filtering is rather slow.

like image 159
Jonas Avatar answered Oct 29 '22 22:10

Jonas


To subtract the background image, you need a model of the background. The simplest model is an image captured as the background along with some allowable deviation (+/- 0-255). Then, background subtraction in MATLAB is pretty simple:

image( find(abs(image-background) <= threshold) ) = 0;

It becomes more difficult when you use a statistical model, but essentially subtracting the background is pretty easy. imsubtract is NOT background subtraction; it is a subtraction filter like you would find in photoshop. It does not care about background versus foreground, which then defeats the point.

Since background subtraction itself is pretty easy, the question becomes more about background estimation. This is a bit more complicated, and generally requires more frames and training to build statistical models of the background (for example, looking at pixels as Gaussian distributions or mixtures of Gaussians, or looking instead at optical flow to determine what's not moving).

If you have access to technical articles (via work or school), "Pfinder: Real-Time Tracking of the Human Body" by Wren and others gives a pretty simple approach. Or you can just search google for single Gaussian background subtraction. There are a number of methods implemented with OpenCV here --> http://dparks.wikidot.com/source-code <-- that you might find useful.

like image 25
Anthony Avatar answered Oct 29 '22 22:10

Anthony