MATLAB: I get the background in white color while it should be black !

framesImage Processing Toolboxvideo

Hi ..
I have the following code:
vid = videoinput('winvideo',1);
set(vid, 'ReturnedColorspace', 'rgb');
% set(vid,'FramesPerTrigger',4);
set(vid,'TriggerRepeat',Inf);
vid.FrameGrabInterval = 6;
start(vid);
BackgroundImage = getsnapshot(vid);
I1 = rgb2gray(BackgroundImage);
figure;
subplot(3,3,1);
imshow(I1);
title('Background F1');
while 1
frames = getsnapshot(vid);
subplot(3,3,2);
imshow(frames);
title('RGB frames');
I2 = rgb2gray(frames);
subplot(3,3,3);
imshow(I2);
title('Gray frames');
Objects = I1 ~= I2;
subplot(3,3,4);
imshow(Objects);
title('Objects');
end
In this part of code should give me a black background and the objects should be white
Objects = I1 ~= I2;
subplot(3,3,4);
imshow(Objects);
title('Objects');
but I get white background ! when I tried it on images it was work just fine 😐
Could you please help me

Best Answer

Every pixel is likely to be different since it came from a video camera. So you can't compare value for value. You should see if values are within some tolerance threshold, like closer than 5 gray levels or whatever.
toleranceThreshold = 5; % Whatever works for you...
Objects = abs(double(I1) - double(I2)) > toleranceThreshold; % A binary image.
Related Question