public void SubtractImages() { Image <Bgr, Byte> Frame = null; //current Frame from camera Image <Gray, float> Gray_Frame; //gray_frame form camera Image <Gray, float> Background = null; //Average Background being formed Image <Gray, float> Previous_Frame; //Previiousframe aquired Image <Gray, float> Difference; //Difference between the two fra double alpha = 0.003; //stores alpha for thread access int Threshold = 60; //stores threshold for thread access using (var _capture = new Capture()) { if (Frame == null) //we need at least one fram to work out running average so acquire one before doing anything { //display the frame aquired Frame = _capture.RetrieveBgrFrame(); //we could use RetrieveGrayFrame if we didn't care about displaying colour image //DisplayImage(Frame.ToBitmap(), captureBox); //display the image using thread safe call //copy the frame to previousframe //Previous_Frame = Frame.Convert<Gray, Byte>().Convert<Gray, float>(); //we can only convert one aspect at a time so wel call convert twice Background = Frame.Convert <Gray, Byte>().Convert <Gray, float>(); //we can only convert one aspect at a time so wel call convert twice } //acquire the frame Frame = _capture.RetrieveBgrFrame(); //we could use RetrieveGrayFrame if we didn't care about displaying colour image //DisplayImage(Frame.ToBitmap(), captureBox); //display the image using thread safe call //create a gray copy for processing Gray_Frame = Frame.Convert <Gray, Byte>().Convert <Gray, float>(); //we can only convert one aspect at a time so wel call convert twice //cvAbsDiff(pFrameMat, pBkMat, pFrMat); Difference = Background.AbsDiff(Gray_Frame); //find the absolute difference Difference.ToBitmap().Save("C:\\test_Difference.jpg"); //CvInvoke.cvRunningAvg(Difference, Background, 0.003, Background); /*Play with the alpha weighting 0.001 */ Background.RunningAvg(Difference, alpha); //performe the cvRunningAvg frame acumullation //DisplayImage(Background.ToBitmap(), resultbox); //display the image using thread safe call Background.ToBitmap().Save("C:\\test_Background.jpg"); } }