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?
Show older comments
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)
subplot(2,1,1);
imagesc(IM);
title('REAL TIME IMAGE');
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;
else
f(k,j) = 0;
end
end
end
subplot(2,1,2);
handlesPlot=imshow(uint8(f));
title('MOVING OBJECT');
end
end
end
12 Comments
Image Analyst
on 4 Jan 2013
I don't see where you set "thresh" to any value. What kind of output do you get? What does (the badly named) binary image "f" look like? If you display fr1, fr2, and fr3, so they look as expected?
Sean de Wolski
on 4 Jan 2013
So it looks like you copied the code from here:
I highly recommend citing others' work when you choose to copy it.
Sanjeev
on 5 Jan 2013
Walter Roberson
on 5 Jan 2013
It doesn't even open a plot window for you?
Sanjeev
on 5 Jan 2013
Walter Roberson
on 5 Jan 2013
Consider how much easier it is for volunteers to understand your code when it is nicely formatted. http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Sanjeev
on 5 Jan 2013
Edited: Walter Roberson
on 5 Jan 2013
Walter Roberson
on 5 Jan 2013
What purpose does handlesRaw serve in the second routine? It is a persistent variable and some code is only done if it is empty, which would be the case if it had never been initialized. That kind of isempty() test is normally only used to determine whether some initialization needs to be done the first time through a routine, with the initialization being skipped on other runs through the routine, but in this case nothing is done if the object is already initialized, and the code that is run when it is not initializes never initializes it. This is confusing for readers: if something needs to be done every time the routine is called, then just don't protect it with that kind of test.
Anyhow, from the point in the code "Plot first value", the variable "source" gets used in many questionable ways. The variable has been passed in from the realVideo routine, and represents a videoreader object. Arrays of videoreader objects are allowed according to the documentation for disp() applied to videoreader objects, so subscripting of a videoreader object would select a single videoreader object, and length() applied to a possible array of videoreader objects would return 1 if there is only a single videoreader object in the array, such as is the case here. So the
for i = 2:3:(length(source)-1)
is going to be effectively
for i = 2:3:(1-1)
which is never going to execute.
Inside that "for" block, the videoreader object "source" has some information accessed as source(i).cdata . That would be fine if videoreader objects had a cdata method, but as far as I can tell they do not. The information that the section of code is attempting to extract is information that would have been extracted into the 4-dimensional array
IM=getdata(source);
where the 4th dimension is the frame number.
It appears that the whole "for i" loop needs to be rewritten to pull information from "IM" instead from "source"
Now there is a problem, that the code in that "for" loop expects to work through bunches of 3 frames at a time, but the videoreader object has been set to acquire 1 frame per trigger. 3 getdata() would be needed to retrieve 3 frames with that videoreader object configuration, but there is only one getdata() call in the FrameRDisplay call -- and the time interval for the timer has been set to run the fraemRDisplay routine once for every frame expected by the frame rate, so there should not be multiple getdata() calls in the FrameRDisplay routine. So you are going to need to rewrite that routine to save data in-between calls, and you are going to have to consider whether you want to apply the movement detection in non-overlapping groups of 3 frames or in overlapping groups of 3 frames.
My summary is that the code you have downloaded is too broken for use as-is and should be mostly thrown away and re-written, at least the display routine.
Sanjeev
on 5 Jan 2013
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
on 5 Jan 2013
Edited: Jerome Lecoq
on 5 Jan 2013
Hello Walter,
I am happy to modify my code at http://www.matlabtips.com/realtime-processing/ 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.
Answers (0)
Categories
Find more on Video Formats and Interfaces in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!