Image Background Removal
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mazvydas Tadaravicius
am 26 Jan. 2011
Verschoben: DGM
am 12 Feb. 2023
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 Kommentare
Akzeptierte Antwort
Egon Geerardyn
am 26 Jan. 2011
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.
2 Kommentare
Walter Roberson
am 26 Jan. 2011
Newer versions of Matlab have improved loop speed considerably, apparently.
Weitere Antworten (1)
Siddharth Shankar
am 26 Jan. 2011
Have you considered using the imabsdiff method. The difference of the two images is basically the object obstructing the background.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!