/// <summary> /// Called by the video player to handle the next video frame. /// </summary> private void videoPlayer1_NextFrame(OpenCVDotNet.UI.VideoPlayer sender, OpenCVDotNet.UI.NextFrameEventArgs args) { // args.Frame contains the frame to be handled. CVImage frame = args.Frame; // go over all the pixels (rows, cols) for (int y = 0; y < frame.Height; ++y) { for (int x = 0; x < frame.Width; ++x) { CVRgbPixel pixel = frame[y, x]; // invert RGB colors. frame[y, x] = new CVRgbPixel( (byte)(255 - pixel.R), (byte)(255 - pixel.G), (byte)(255 - pixel.B)); } } // assign resulting frame to picture box as a bitmap. pictureBox1.Image = frame.ToBitmap(); writer.WriteFrame(frame); }
private void Work() { CVImage frame; try { while (true) { ReopenCapture(); while ((frame = cap.QueryFrame()) != null) { for (int row = 0; row < frame.Height; ++row) { for (int col = 0; col < frame.Width; ++col) { CVRgbPixel pixel = frame[row, col]; // get black and white value for this pixel. byte bwValue = pixel.BwValue; // accumulate the new value to the moving average. double val = (double)bwValue / 255.0; avg.Accumulate(row, col, val); // calculate the diff between the frame and the average up until now. double delta = val - avg[row, col]; // if delta is smaller than the threshold, eliminate the background. if (delta < Threshold) { // set the color of the background frame to black. frame[row, col] = backgroundColor; } else { // if monochrome is checked, set pixel to foreground color. if (monochrome.Checked) { frame[row, col] = foregroundColor; } } } } // display the updated frame on the window. cvPanel.Image = frame.ToBitmap(); if (vw != null) { vw.WriteFrame(frame); } // delay for 20ms. Thread.Sleep(20); frame.Release(); } } } catch (CVException e1) { MessageBox.Show(e1.Message); } catch (ThreadAbortException) { Console.WriteLine("Aborting thread..."); } finally { if (cap != null) { cap.Release(); } if (vw != null) { vw.Release(); } } }