/// <summary> /// Start or stop the particle filter. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnProcessFrame_Click(object sender, EventArgs e) { try { _numParticles = int.Parse(tBoxNumParticles.Text); _Threshold = double.Parse(tboxThreshold.Text); _HoldPercentage = float.Parse(tBoxHoldPercent.Text); _Alpha = float.Parse(tBoxAlpha.Text); _pixelsToMilliseconds = int.Parse(tBoxPixels.Text); } catch (Exception ex) { MessageBox.Show("Invalid value in particle text boxes!"); } if (!_ParticleFilterRunning) { SetStatus("Starting", lblStatus); _ParticleFilterRunning = true; Thread FilterThread = new Thread(new ThreadStart(ProcessFrame)); _RocketTracking = new Thread(new ThreadStart(TrackAndFireThread)); FilterThread.Start(); btnProcessFrame.Text = "Stop Filter"; } else { SetStatus("Off", lblStatus); _ParticleFilterRunning = false; _Filterstopping.WaitOne(); btnProcessFrame.Text = "Start Filter"; pf = null; } }
/// <summary> /// Run the particle filter on its own independent thread. /// </summary> private void ProcessFrame() { while (_ParticleFilterRunning) { if (_currentFrame != null && _trackingImage != null) { _newFrame.WaitOne(); _newFrame.Reset(); _frameLock.WaitOne(); var Frame = (Bitmap)_currentFrame.Clone(); _frameLock.Release(); if (pf == null) { Bitmap Template = (Bitmap)_trackingImage.Clone(); pf = new ParticleFilter(_numParticles, Template, Frame, _Threshold, _HoldPercentage, _Alpha); } pf.processFrame(Frame); Frame.Dispose(); Pause.WaitOne(100); //Prevents Jerky delayed webcam updates. } } _Filterstopping.Set(); }