Clear Filters
Clear Filters

Sir could i trouble you in asking for your help to look into my code and give a reason to why i dont get my needed output?

1 view (last 30 days)
I'm doing my final year project on MOVING OBJECT DETECTION and with all the help from the internet finally finished my code and i jst wont get the Moving Object output. The video I'm feeding is a live video through my webcam.
function realVideo() NumberFrameDisplayPerSecond=10; hFigure=figure(1); source= videoinput('winvideo', 1); set(source,'FramesPerTrigger',1); set(source,'TriggerRepeat',Inf); set(source,'ReturnedColorSpace','RGB'); triggerconfig(source, 'Manual'); Timer=timer('TimerFcn', {@FrameRDisplay,source},'Period',1/NumberFrameDisplayPerSecond,'ExecutionMode','fixedRate','BusyMode','drop'); start(source); start(Timer); uiwait(hFigure); stop(Timer); delete(Timer); stop(source); delete(source); clear functions; end
% This function is called by the timer to display one frame of the figure
function FrameRDisplay(obj, event,source) persistent IM; persistent handlesRaw; persistent handlesPlot;
trigger(source); IM=getdata(source);
if isempty(handlesRaw)
for i = 2:3:(length(source)-1)
fr1 = source(i-1).cdata; % read in frame-i-1
fr_bw1 = rgb2gray(fr1); % convert frame-i-1 to grayscale
fr2 = source(i).cdata; % read in frame-i
fr_bw2 = rgb2gray(fr2); % convert frame-i to grayscale
fr3 = source(i+1).cdata; % read in frame-i+1
fr_bw3 = rgb2gray(fr3); % convert frame-i+1 to grayscale
fr_diff1 = abs(double(fr_bw1) - double(fr_bw2)); % First frame Difference cast operands as double to avoid negative overflow
fr_diff2 = abs(double(fr_bw2) - double(fr_bw3)); % Second frame difference cast operands as double to avoid negative overflow
for j=1:width % if fr_diff > thresh pixel in foreground
for k=1:height
if ((fr_diff1(k,j) > thresh) && (fr_diff2(k,j) > thresh))
f(k,j) = 255;
f(k,j) = 0;
Walter Roberson
Walter Roberson on 5 Jan 2013
"source" is the video object. length(source) is 1.
It is like having a clock and asking how many clocks you have (one!) when what you should have done is looked at the clock instead of counting how many there are. getdata(source) is the way to read the video instead of counting how many cameras you have.
Jerome Lecoq
Jerome Lecoq on 5 Jan 2013
Edited: Jerome Lecoq on 5 Jan 2013
Hello Walter,
I am happy to modify my code at if you believe it is broken in any way. I never expected so many people to read it when I initially did it and I don't want anyone to rely on broken things.
The original code is somehow different from the version that Sanjeev made.

Sign in to comment.

Answers (0)


Find more on MATLAB Support Package for IP Cameras in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!