示例#1
0
        /// <summary>
        /// Called when the button is clicked to open the video/image.
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            // show file dialog, and continue only if OK was chosen.
            OpenFileDialog dialog = new OpenFileDialog();

            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            // open and play the file (errors are not handled nicely here).
            videoPlayer1.Open(dialog.FileName);

            // open the video writer (close previously opened writer).
            if (writer != null)
            {
                writer.Release();
            }
            writer = videoPlayer1.CreateVideoWriter("c:\\myoutput.avi");

            // start the video.
            videoPlayer1.Play();
        }
示例#2
0
        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();
                }
            }
        }