Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Image Background Removal

Asked by Mazvydas Tadaravicius on 26 Jan 2011
Latest activity Answered by rohith h.c on 10 Apr 2014

For my gesture recognition uni project I have developed simple code that removes background from the image. For it to work there has to be an image of background scene and an image with an object that partially covers same background scene (a hand with a background of a room scene for example).

function [outIm] = makeMask(bg, im, tol)
%bg - background image
%im - input image
%tol - tolerance
[h,w] = size(bg);
outIm = false(h, w);
for ch = 1:h
    for cw = 1:w
        imPix = im(ch,cw);
        bgPix = bg(ch,cw);
        if ((bgPix == imPix) || (bgPix > imPix && bgPix <= imPix + tol) || (bgPix < imPix && bgPix >= imPix - tol))
            outIm(ch, cw) = 0;
        else
            outIm(ch, cw) = 1;
        end
    end
end
end

As You see, code checks every single pixel and decides if it belongs to background scene. All this works well, however I want to ask is there more efficient way to achieve the same?

0 Comments

Mazvydas Tadaravicius

Products

No products are associated with this question.

6 Answers

Answer by Egon Geerardyn on 26 Jan 2011
Accepted answer

You can try vectorizing that code, but that will be most likely destroy any short-circuiting you have in there.

Just by the top of my head, following code should be able to replace your for-loops:

brighterThanBg = (im > bg + tol);
darkerThanBg = (im < bg - tol);
outIm = (brighterThanBg | darkerThanBg);

That will also check every pixel but it will be faster as MATLAB is quite slow on loops. Also, by inverting your logic (you check for background, split into 3 regions, 2 of which require 2 conditions to be satisfied), while this codes only takes care of 2 regions (pixels too bright to be background, pixels too dark to be background), each only requiring a single condition.

1 Comment

Walter Roberson on 26 Jan 2011

Newer versions of Matlab have improved loop speed considerably, apparently.

Egon Geerardyn
Answer by Siddharth Shankar on 26 Jan 2011

Have you considered using the imabsdiff method. The difference of the two images is basically the object obstructing the background.

1 Comment

Mazvydas Tadaravicius on 27 Jan 2011

I could use imabsdiff method, however it does not tkae into consideration the change of lighting when object covers background. Also, it is way slower than my method or method suggested by Egon.

>> tic; imabsdiff(bgg, ig); im2bw(ans, graythresh(ans)); toc;
Elapsed time is 0.115902 seconds.

>> tic; imabsdiff(bgg, ig); im2bw(ans, graythresh(ans)); toc;
Elapsed time is 0.030604 seconds.

Siddharth Shankar
Answer by Mazvydas Tadaravicius on 26 Jan 2011
function [outIm] = makeMask2(bg, im, tol)
outIm = ((im > bg + tol) | (im < bg - tol));
end

This function does the job so much faster, have a look at these figs:

>> tic; m1 = makeMask(bgg, ig, 20); toc; tic; m2 = makeMask2(bgg, ig, 20); toc;
Elapsed time is 0.004686 seconds.
Elapsed time is 0.001663 seconds.
>> tic; m1 = makeMask(bgg, ig, 20); toc; tic; m2 = makeMask2(bgg, ig, 20); toc;
Elapsed time is 0.004764 seconds.
Elapsed time is 0.001655 seconds.
>> tic; m1 = makeMask(bgg, ig, 20); toc; tic; m2 = makeMask2(bgg, ig, 20); toc;
Elapsed time is 0.005040 seconds.
Elapsed time is 0.001660 seconds.

Thanks for the tip Egon Geerardyn.

0 Comments

Mazvydas Tadaravicius
Answer by Pavel M. on 14 Nov 2011

I've interested do you have background detection algorithm on an image = object + background w/o having background apriori? I am currently developing such algorithm ( background detection by special histogram processing ). Have you tried to find centroid of an image histogram ( y coordinate )? If somebody have code of computing centroid of an area under 2D discrete function ( histogram ) will appreciate to look at. Thanks.

0 Comments

Pavel M.
Answer by flora26 on 27 Feb 2014

I am doing project on fruit defect detection can anyone help to subtract background from image if i dont have separate background image and how to extrcat specified color as defected area from fruit...

0 Comments

flora26
Answer by rohith h.c on 10 Apr 2014

this is my original image

below is the image after applying median filtering

i want to perform another 2 steps,pls can anyone help in coding?? for step1: background cancellation step2:image segmentation pls answer as soon as possible

0 Comments

rohith h.c

Contact us