DSPRelated.com
Forums

Motion detection help

Started by Active8 October 30, 2005
Hi:

I'm looking for a simple way to detect motion in an empty room. I'll have
a webcam set up and everything should be still. I'll take occasional
snapshots and compare them to previos snaps. If nothing changes, I'll
discard the snaps. When someone enters, I want to switch to record until
the motion stops.

I though maybe comparing histograms would work, but I dunno. I'd rather
not do a pixel by pixel comparison, because changes in light level and
all... even a fly would foul things up.

What are your thoughts? Thow me some ideas and I'll figger out how to code
it.

TIA,
Mike
Active8 wrote:
> Hi: > > I'm looking for a simple way to detect motion in an empty room. I'll have > a webcam set up and everything should be still. I'll take occasional > snapshots and compare them to previos snaps. If nothing changes, I'll > discard the snaps. When someone enters, I want to switch to record until > the motion stops. > > I though maybe comparing histograms would work, but I dunno. I'd rather > not do a pixel by pixel comparison, because changes in light level and > all... even a fly would foul things up. > > What are your thoughts? Thow me some ideas and I'll figger out how to code > it. > > TIA, > Mike
First pass attempt: Sum the rows and sum the columns. Difference between frames. Sum the differences.
On Sun, 30 Oct 2005 16:36:05 +0000, Bryan Hackney wrote:

> Active8 wrote: >> Hi: >> >> I'm looking for a simple way to detect motion in an empty room. I'll >> have a webcam set up and everything should be still. I'll take >> occasional snapshots and compare them to previos snaps. If nothing >> changes, I'll discard the snaps. When someone enters, I want to switch >> to record until the motion stops. >> >> I though maybe comparing histograms would work, but I dunno. I'd rather >> not do a pixel by pixel comparison, because changes in light level and >> all... even a fly would foul things up. >> >> What are your thoughts? Thow me some ideas and I'll figger out how to >> code it. >> >> TIA, >> Mike > > > First pass attempt: > > Sum the rows and sum the columns. Difference between frames. Sum the > differences.
The diff between frames would hopefully be large if a person entered the picture, but what is the purpose of summing the differences? Thanks, Mike
Hi Mike,
There's a linux app called 'motion'. Does just what you want. Check it out 
http://www.lavrsen.dk/twiki/bin/view/Motion/WebHome
Here's a link showing it in  action!!
http://news.bbc.co.uk/1/hi/magazine/4276851.stm
It's open source, so it should give you some ideas if you need a bespoke 
solution.
HTH, Syms.
"Active8" <reply2group@ndbbm.net> wrote in message 
news:pan.2005.10.30.16.58.44.353000@ndbbm.net...
> On Sun, 30 Oct 2005 16:36:05 +0000, Bryan Hackney wrote: > >> Active8 wrote: >>> Hi: >>> >>> I'm looking for a simple way to detect motion in an empty room. I'll >>> have a webcam set up and everything should be still. I'll take >>> occasional snapshots and compare them to previos snaps. If nothing >>> changes, I'll discard the snaps. When someone enters, I want to switch >>> to record until the motion stops. >>> >>> I though maybe comparing histograms would work, but I dunno. I'd rather >>> not do a pixel by pixel comparison, because changes in light level and >>> all... even a fly would foul things up. >>> >>> What are your thoughts? Thow me some ideas and I'll figger out how to >>> code it. >>> >>> TIA, >>> Mike >> >> >> First pass attempt: >> >> Sum the rows and sum the columns. Difference between frames. Sum the >> differences. > > The diff between frames would hopefully be large if a person > entered the picture, but what is the purpose of summing the differences? > > Thanks, > Mike > >
"Active8" <reply2group@ndbbm.net> wrote in message 
news:pan.2005.10.30.16.58.44.353000@ndbbm.net...
> On Sun, 30 Oct 2005 16:36:05 +0000, Bryan Hackney wrote: > >> Active8 wrote: >>> Hi: >>> >>> I'm looking for a simple way to detect motion in an empty room. I'll >>> have a webcam set up and everything should be still. I'll take >>> occasional snapshots and compare them to previos snaps. If nothing >>> changes, I'll discard the snaps. When someone enters, I want to switch >>> to record until the motion stops. >>> >>> I though maybe comparing histograms would work, but I dunno. I'd rather >>> not do a pixel by pixel comparison, because changes in light level and >>> all... even a fly would foul things up. >>> >>> What are your thoughts? Thow me some ideas and I'll figger out how to >>> code it. >>> >>> TIA, >>> Mike >> >> >> First pass attempt: >> >> Sum the rows and sum the columns. Difference between frames. Sum the >> differences. > > The diff between frames would hopefully be large if a person > entered the picture, but what is the purpose of summing the differences?
The sum of the differences would be an integral - so it's an attempt to deal with an average difference I'd imagine. I'd be tempted to do this: Compute a long running average for the "reference" frame - to get rid of slowly changing illumination changes. Presumably you're looking for large differences that happen relatively quickly. Compute the *sum of* differences between the current frame and the reference frame - yielding a scalar quantity. Perhaps average a finite set of these scalars to remove noise that will likely be there. (This would be a FIR filter). Compare the output of the filter to a threshold that you establish. [This is very important!] If the filter output exceeds the threshold, you have a detection. Your comment about a fly fouling things up suggests you need to think about the detection threshold. Setting the threshold is a matter of reducing false alerts *and* reducing false rests. Given that the "noise" is uniform, an optimum threshold would be near the *mid-point* between "the no motion measurement" and the measurement you take when there is the smallest disturbance that must be detected. So, you can possibly reject flies, birds, cats, dogs,.....humans, cars depending on the scale of the scene and the threshold you set. The reason for setting the mid-point is so that "noise" will equally bias the measurement toward a false alert and a false rest. Putting the threshold closer to the "no motion" value will increase the probability of a false alert - your scenario with the fly. Putting the threshold closer to the "motion" value will increase the probability of a false rest. Fred
Fred Marshall wrote:
> "Active8" <reply2group@ndbbm.net> wrote in message > news:pan.2005.10.30.16.58.44.353000@ndbbm.net... > >>On Sun, 30 Oct 2005 16:36:05 +0000, Bryan Hackney wrote: >> >> >>>Active8 wrote: >>> >>>>Hi: >>>> >>>>I'm looking for a simple way to detect motion in an empty room. I'll >>>>have a webcam set up and everything should be still. I'll take >>>>occasional snapshots and compare them to previos snaps. If nothing >>>>changes, I'll discard the snaps. When someone enters, I want to switch >>>>to record until the motion stops. >>>> >>>>I though maybe comparing histograms would work, but I dunno. I'd rather >>>>not do a pixel by pixel comparison, because changes in light level and >>>>all... even a fly would foul things up. >>>> >>>>What are your thoughts? Thow me some ideas and I'll figger out how to >>>>code it. >>>> >>>>TIA, >>>>Mike >>> >>> >>>First pass attempt: >>> >>>Sum the rows and sum the columns. Difference between frames. Sum the >>>differences. >> >>The diff between frames would hopefully be large if a person >>entered the picture, but what is the purpose of summing the differences? > > > The sum of the differences would be an integral - so it's an attempt to deal > with an average difference I'd imagine. > > I'd be tempted to do this: > > Compute a long running average for the "reference" frame - to get rid of > slowly changing illumination changes. > Presumably you're looking for large differences that happen relatively > quickly. > Compute the *sum of* differences between the current frame and the reference > frame - yielding a scalar quantity. > Perhaps average a finite set of these scalars to remove noise that will > likely be there. > (This would be a FIR filter). > Compare the output of the filter to a threshold that you establish. > [This is very important!] > If the filter output exceeds the threshold, you have a detection. > > Your comment about a fly fouling things up suggests you need to think about > the detection threshold. Setting the threshold is a matter of reducing > false alerts *and* reducing false rests. Given that the "noise" is uniform, > an optimum threshold would be near the *mid-point* between "the no motion > measurement" and the measurement you take when there is the smallest > disturbance that must be detected. So, you can possibly reject flies, > birds, cats, dogs,.....humans, cars depending on the scale of the scene and > the threshold you set. > The reason for setting the mid-point is so that "noise" will equally bias > the measurement toward a false alert and a false rest. > Putting the threshold closer to the "no motion" value will increase the > probability of a false alert - your scenario with the fly. > Putting the threshold closer to the "motion" value will increase the > probability of a false rest.
The sum of differences creates a single number to compare to a threshold. Once the capture process begins, one might require quiescence for several frames before stopping the sequence. Or not, depending on the use. Again, depending on the use either a false start or false rest may be more pernicious. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Active8 wrote:
> On Sun, 30 Oct 2005 16:36:05 +0000, Bryan Hackney wrote: > > >>Active8 wrote: >> >>>Hi: >>> >>>I'm looking for a simple way to detect motion in an empty room. I'll >>>have a webcam set up and everything should be still. I'll take >>>occasional snapshots and compare them to previos snaps. If nothing >>>changes, I'll discard the snaps. When someone enters, I want to switch >>>to record until the motion stops. >>> >>>I though maybe comparing histograms would work, but I dunno. I'd rather >>>not do a pixel by pixel comparison, because changes in light level and >>>all... even a fly would foul things up. >>> >>>What are your thoughts? Thow me some ideas and I'll figger out how to >>>code it. >>> >>>TIA, >>>Mike >> >> >>First pass attempt: >> >>Sum the rows and sum the columns. Difference between frames. Sum the >>differences. > > > The diff between frames would hopefully be large if a person > entered the picture, but what is the purpose of summing the differences? > > Thanks, > Mike > >
When you sum the rows and sum the columns, you get a luminosity graph in 2 dimensions. Luminosity is all you need, I believe. Throw away the chroma. You might want to take differences in these 2 dimensions from, say, frame 1 to frame 0, 2 to 0, 3 to 0, etc, to get indications of both fast and slow motion. Be sure to take the absolute values - don't use negatives unless you want to graph the data. Sum the absolute row differences and the absolute column differences to get 2 numbers. Add these together if you wish to get s single number. Experiment, and you will quickly get a feel for what numbers constitute motion. This threshold may need to vary with ambient light.
Symon wrote:
> Hi Mike, > There's a linux app called 'motion'. Does just what you want. Check it out > http://www.lavrsen.dk/twiki/bin/view/Motion/WebHome > Here's a link showing it in action!! > http://news.bbc.co.uk/1/hi/magazine/4276851.stm > It's open source, so it should give you some ideas if you need a bespoke > solution. > HTH, Syms.
[...] I'm aware of the motion detection software, but I've never looked at it. If it's documented, and perhaps better than the simple method I described, please review it here. Thanks.
On Sun, 30 Oct 2005 11:11:23 -0800, Symon wrote:

> Hi Mike, > There's a linux app called 'motion'. Does just what you want. Check it out > http://www.lavrsen.dk/twiki/bin/view/Motion/WebHome Here's a link showing > it in action!! http://news.bbc.co.uk/1/hi/magazine/4276851.stm It's open > source, so it should give you some ideas if you need a bespoke solution. > HTH, Syms.
Yeah. My next act would be to figure out how to work with the webcam in Linux. I'm doing the GUI in wxWidgets so it'll compile for both OSs. You're clairvoyant. Thanks. Mike
> "Active8" <reply2group@ndbbm.net> wrote in message > news:pan.2005.10.30.16.58.44.353000@ndbbm.net... >> On Sun, 30 Oct 2005 16:36:05 +0000, Bryan Hackney wrote: >> >>> Active8 wrote: >>>> Hi: >>>> >>>> I'm looking for a simple way to detect motion in an empty room. I'll >>>> have a webcam set up and everything should be still. I'll take >>>> occasional snapshots and compare them to previos snaps. If nothing >>>> changes, I'll discard the snaps. When someone enters, I want to >>>> switch to record until the motion stops. >>>> >>>> I though maybe comparing histograms would work, but I dunno. I'd >>>> rather not do a pixel by pixel comparison, because changes in light >>>> level and all... even a fly would foul things up. >>>> >>>> What are your thoughts? Thow me some ideas and I'll figger out how to >>>> code it. >>>> >>>> TIA, >>>> Mike >>> >>> >>> First pass attempt: >>> >>> Sum the rows and sum the columns. Difference between frames. Sum the >>> differences. >> >> The diff between frames would hopefully be large if a person entered >> the picture, but what is the purpose of summing the differences? >> >> Thanks, >> Mike >> >> >>
The sum of differences is one accepted measure of similarity between
two areas of pixels.

One method of motion detection is called 'sum of absolute differences'.
This is used to locate movement, rather than just to detect that
movement has happened. That is, when you want to see where the movement
was and in what direction. In your application this could for instance
let you detect movement in a certain area (near a door or wallet)
preferentially.

Anyway, you divide the image into (square, 8x8) blocks that are called
macroblocks. Then, for each new frame you take each macoblock form the
previous frame and scan it around its original position by some amount.
For each position you compare the pixels in the reference macroblock
with those underneath it in the new frame, to see how close is the
match. What do you mean by 'a close match'? Well, one measure is the
sum of absolute differences (SAD):

For each pair of pixels (from reference and new frame's macroblock),
find the difference, take the absolute value of it, and sum this over
all pairs. The lowest SAD is the closest match - and if this is
elsewhere than the original position, then something moved and this is
where it moved to. (This is called a 'motion vector'). You can use
these to find movement and identify the type, direction, and location
of movement.

Chris
====================
Chris Bore
BORES Signal Processing
www.bores.com