I have written a code for adaptive thresholding in background subtraction. Can anyone tell me what is wrong in this code?

3 views (last 30 days)
clc; close all; imtool close all; clear; workspace; fontSize = 14;
vid = videoinput('winvideo',1,'YUY2_640x480');
set(vid,'TriggerRepeat',Inf); vid.FrameGrabInterval = 1;
vid_src = getselectedsource(vid); set(vid_src,'Tag','motion detection setup');
figure(1);
start(vid); pause(4); d=getdata(vid);
while(vid.FramesAcquired<=40)
videoObject = getdata(vid);
numberOfFrames=vid.FramesAcquired;
%numberOfFrames = videoObject.NumberOfFrames;
meanGrayLevels = zeros(numberOfFrames, 1);
meanRedLevels = zeros(numberOfFrames, 1);
meanGreenLevels = zeros(numberOfFrames, 1);
meanBlueLevels = zeros(numberOfFrames, 1);
for frame = 1 : numberOfFrames
thisFrame = getdata(vid, frame);
% Display it
hImage = subplot(2, 2, 1);
imshow(thisFrame);
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow; % Force it to refresh the window.
% Calculate the mean gray level.
grayImage = rgb2gray(thisFrame); meanGrayLevels(frame) = mean(grayImage(:));
% Calculate the mean R, G, and B levels.
meanRedLevels(frame) = mean(mean(thisFrame(:, :, 1)));
meanGreenLevels(frame) = mean(mean(thisFrame(:, :, 2)));
meanBlueLevels(frame) = mean(mean(thisFrame(:, :, 3)));
% Plot the mean gray levels.
hPlot = subplot(2, 2, 2);
hold off;
plot(meanGrayLevels, 'k-', 'LineWidth', 2);
hold on;
plot(meanRedLevels, 'r-');
plot(meanGreenLevels, 'g-');
plot(meanBlueLevels, 'b-');
grid on;
% Put title back because plot() erases the existing title.
title('Mean Gray Levels', 'FontSize', fontSize); if frame == 1 xlabel('Frame Number');
ylabel('Gray Level');
[rows columns numberOfColorChannels] = size(thisFrame);
end
% differencing
alpha = 0.5;
if frame == 1
Background = thisFrame;
else
% Change background slightly at each frame
%Background(t+1)=(1-alpha)*I+alpha*Background
Background = (1-alpha)* thisFrame + alpha * Background;
end
% Display the changing/adapting background.
subplot(2, 2, 3);
imshow(Background);
title('Adaptive Background', 'FontSize', fontSize);
% Calculate a difference between this frame and the background.
differenceImage = thisFrame - uint8(Background);
% Threshold with Otsu method.
grayImage = rgb2gray(differenceImage); % Convert to gray level
thresholdLevel = graythresh(grayImage); % Get threshold.
binaryImage = im2bw( grayImage, thresholdLevel); % Do the binarization
% Plot the binary image.
subplot(2, 2, 4);
imshow(binaryImage);
title('Binarized Difference Image', 'FontSize', fontSize);
end
for frame = 1 : numberOfFrames
% Construct an output image file name.
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Read the image in from disk.
thisFrame = imread(outputFullFileName);
% Convert the image into a "movie frame" structure.
recalledMovie(frame) = im2frame(thisFrame);
end
delete(hImage);
delete(hPlot);
subPlot(1, 3, 2);
axis off; % Turn off axes numbers.
title('Object detection', 'FontSize', fontSize);
% Play the movie in the axes.
movie(recalledMovie);
end

Answers (1)

Ash
Ash on 25 Feb 2017
Hi check this answer
https://au.mathworks.com/matlabcentral/answers/57299-adaptive-background-subtraction-algorithm

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!