Page 1 of 1

problems reading a video stream from file with "motion"

PostPosted: Sat Mar 14, 2009 10:41 am
by Thor
Hi evry one

I am doing some research with the "motion" demo program provided on the net. It works fine when I hook up a video camera or use some of the IP cameras provided in the lists, but I can not read from a video file. I get the error:

A first chance exception of type 'System.ApplicationException' occurred in Tiger.Video.VFW.dll
'motion.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
exception : Failed initializing decompressor
The thread 0xce4 has exited with code 0 (0x0).

It seams lie the decompressor needed can not be found or something like that. The file I am trying to read is an avi file of size around 100 Mb.

Does anyone have a suggest of what I can do to get this working. It would be very good for my research on this to get over this threshold.

Best regards


Re: problems reading a video stream from file with "motion"

PostPosted: Sat Mar 14, 2009 3:49 pm
by andrew.kirillov

There are few things, which you may try. But before you continue, I would suggest updating your solution to AForge.NET framework 1.7.0. The Tiger.Video.VFW.dll is something really old, which was done before AForge.NET (however used as a base). If you don't want spending too much time changing an old solution taken from CodeProject, you may try AForge.NET framework's samples - there is motion detection sample.

1. Try using AForge.Video.DirectShow instead of AForge.Video.VFW. It has FileVideoSource class, which allow accessing video files through DirectShow interface. All new codec support DirectShow interface instead of VFW, so you should have less problems with that.

2. However, if you would like to keep using AForge.Video.VFW or would like to keep your old solution without changing any line of code, you may try configuring your codecs to support VFW interface. I would suggest you to install FFDSHOW codecs set (if you still did not). After installation you will find VFW configuration application there. Go to Decoder page and select Codecs.
VFW configuration
ffdshow.jpg (77.11 KiB) Viewed 14858 times

As you may see, most formats have are disabled through VFW interface. Enables those formats (change disabled to libavcodec), which are required for your AVI files. You may check codec name used for your AVI file with other utilities (I used CodecInstaller for this). This solution should work. I recently used that to be able to play AVI files encoded in MJPEG format (had the similar problem - worked through DirectShow, but did not work through VFW).

Hope this helps. Good luck with your research.

Re: problems reading a video stream from file with "motion"

PostPosted: Sun Mar 15, 2009 11:42 pm
by Thor
Thank you for taking your time to reply to this. I have taken a look at the approach to find the correct codec to work with and it seams like the files I am working with are generic FOURCC DIB which I do not see in the list in the ffdshow tool. I do not find this DIB codec on the internet - so it is maybe not so common. How ever if the Windows media player can play those videos - a codec must be installed that can decode this. It is just a question if VFW can decode it?

Except from this I have made good progress with diffrentiation filters and my next step is to implement that in a video-stream, if I get that running some time.

Hope anyone has some comment on this problem on reading the videofiles.


Re: problems reading a video stream from file with "motion"

PostPosted: Mon Mar 16, 2009 6:48 am
by andrew.kirillov
Hello, Thor

This sounds quite strange that you experience difficulties with DIB files. These are uncompressed files and they just store bitmap as it is. I am not really sure that codec is required for this. I was using DIB "codec" many times jus for quick experiments and it always worked without any issues. Try FileVideoSource ...

Re: problems reading a video stream from file with "motion"

PostPosted: Tue Mar 17, 2009 7:31 am
by Thor
Hi again
I have not found a a way to get the 2005 version of Motion detector to read my videos, but I tested the newer 2007 version and that one works fine. But since I used the 2005 version to implement my trial of the blobTracker I would like to use that for my testing. I see that in the newer version the motiondetector is now a part of the library. but since I am modifying the "ProcessFrame()" method in the MotionDetector4 - could I in some way inherit this and implement my version of ProcessFrame with my BlobTracker classes etc. That would be a lean way to do it.

best regards

+---- copy of src that I modified -- *
// Motion Detector
// Copyright © Andrew Kirillov, 2005

// processFrame has been modified my Thor, in 2009 just an adhoc to test concept of tracking - not good for presentation!
namespace motion
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Collections;

using AForge.Imaging;
using AForge.Imaging.Filters;

/// <summary>
/// MotionDetector4
/// </summary>
public class MotionDetector4 : IMotionDetector
private IFilter grayscaleFilter = new GrayscaleBT709();
private IFilter pixellateFilter = new Pixellate();
private Difference differenceFilter = new Difference();
private Threshold thresholdFilter = new Threshold(15);
private MoveTowards moveTowardsFilter = new MoveTowards();

private FiltersSequence processingFilter1 = new FiltersSequence();
private BlobCounter blobCounter = new BlobCounter();

private Bitmap backgroundFrame;
private BitmapData bitmapData;
private int counter = 0;

private Bitmap[] numbersBitmaps = new Bitmap[9];

private bool calculateMotionLevel = false;
private int width; // image width
private int height; // image height
private int pixelsChanged;
private int iNumberOfObjects;

private BlobTrack blobTracker = new BlobTrack();

public int iCount = 0;

// Motion level calculation - calculate or not motion level
public bool MotionLevelCalculation
get { return calculateMotionLevel; }
set { calculateMotionLevel = value; }

// Number of objects detected ÞP
public int NumberOfObjects
get { return iNumberOfObjects; }
set { iNumberOfObjects = value; }
// Motion level - amount of changes in percents
public double MotionLevel
get { return (double)pixelsChanged / (width * height); }

// Constructor
public MotionDetector4()

// load numbers bitmaps
Assembly assembly = this.GetType().Assembly;

for (int i = 1; i <= 9; i++)
numbersBitmaps[i - 1] = new Bitmap(assembly.GetManifestResourceStream(
string.Format("motion.Resources.{0}.gif", i)));

// BlobTrack blobTracker = new BlobTrack();

// Reset detector to initial state
public void Reset()
if (backgroundFrame != null)
backgroundFrame = null;
counter = 0;

// Process new frame
public void ProcessFrame(ref Bitmap image)
if (backgroundFrame == null)
// create initial backgroung image
backgroundFrame = processingFilter1.Apply(image);

// get image dimension
width = image.Width;
height = image.Height;

// just return for the first time

Bitmap tmpImage;

// apply the the first filters sequence
tmpImage = processingFilter1.Apply(image);

if (++counter == 2)
counter = 0;

// move background towards current frame
moveTowardsFilter.OverlayImage = tmpImage;

// set backgroud frame as an overlay for difference filter
differenceFilter.OverlayImage = backgroundFrame;

// lock temporary image to apply several filters
bitmapData = tmpImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

// apply difference filter
// apply threshold filter

// get object rectangles
Rectangle[] newFrameBlobRect = blobCounter.GetObjectRectangles(); // newFrameBlobRect

// unlock temporary image

pixelsChanged = 0;

// -------------------- here after the tracking and drawing is made -----------------------------

int N = 4; // Max number of simultaneous tracks handled in code

if (newFrameBlobRect.Length != 0)
blobTracker.populateTrack(newFrameBlobRect, 1);

// draw the track

Graphics g = Graphics.FromImage(image);

Pen pen = new Pen(Color.Red);
Pen penTrack = new Pen(Color.BlueViolet);
Pen penScope = new Pen(Color.Cyan);
Font myFont = new System.Drawing.Font("Helvetica", 10, FontStyle.Italic);
Brush myBrush = new SolidBrush(System.Drawing.Color.Red);

g.DrawLine(pen, 500, 0, 500, 576);
g.DrawLine(pen, 560, 0, 560, 576);
g.DrawLine(pen, 719, 0, 719, 576);

int n = 0;
// draw each rectangle
foreach (Rectangle rc in newFrameBlobRect)
if ((rc.Right < 700)) // only draw rc that are before blue line
g.DrawRectangle(pen, rc);

if ((n < 10) && (rc.Width > 15) && (rc.Height > 15))
g.DrawImage(numbersBitmaps[n], rc.Left, rc.Top, 7, 9);

// a little bit inaccurate, but fast
if (calculateMotionLevel)
pixelsChanged += rc.Width * rc.Height;

// draw each track
int iNoObjects = N - blobTracker.freeObjNo.Count; // number of tracks

for (int j = 0; j < iNoObjects; j++)
int last = blobTracker.activeObjTrack[j].Count;
if (last > 1)
for (int k = 1; k < last; k++)
g.DrawLine(penTrack, blobTracker.activeObjTrack[j][k - 0].pos, blobTracker.activeObjTrack[j][k - 1].pos);
g.DrawString("vy " + blobTracker.activeObjTrack[j][k - 0].vy, myFont, myBrush, blobTracker.activeObjTrack[j][k - 0].pos);

g.DrawRectangle(penScope, blobTracker.activeObjTrack[j][last - 2].pos.X, blobTracker.activeObjTrack[j][last - 2].pos.Y - blobTracker.activeObjTrack[j][last - 2].r, blobTracker.activeObjTrack[j][last - 2].r, 2 * blobTracker.activeObjTrack[j][last - 2].r);

g.DrawString("Count: " + blobTracker.globalBlobCount, myFont, myBrush, 10, 500);
iNumberOfObjects = blobTracker.globalBlobCount;

g.Dispose( );



Re: problems reading a video stream from file with "motion"

PostPosted: Tue Mar 17, 2009 8:23 am
by andrew.kirillov
Hello, Thor

I see several options to resolve your issue:

1. You may just include that older motion detector with your modifications into the new Motion Detector sample application coming with 1.7.0 version.

2. You may update your old application to use new AForge.Video.* assemblies instead of old.

3. You may also get sources of 1.7.0 version and find implementation of motion detection algorithm there. You may update that one with similar what you've done in your older application.