void _grabber_OnFrame(Parsley.Core.BuildingBlocks.FrameGrabber fg, Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte> img) { // Note: This method is called from the frame-grabber's thread loop. // The framegrabber holds a Breath on the camera to ensure that the camera object remains // alive during this callback. The camera object is a SharedResource, meaning that any // invocation to Dispose will block until all Breaths on the camera are released. Suppose // The thread that owns the picture box has called dispose and blocks. The callback here is called // and invoke is used. Invoke executes the delegate on thread that owns this control, which is the one // that is already blocked. This leads to a deadlock. That is why we use BeginInvoke, which executes // the delegate on the GUI thread associated with this control. if (_can_invoke) { Rectangle client = this.ClientRectangle; Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte> img_copy = img.Resize(client.Width, client.Height, _interpolation); this.BeginInvoke(new MethodInvoker(delegate { // Update image Emgu.CV.IImage prev = _picture_box.Image; _picture_box.Image = img_copy; if (prev != null) { prev.Dispose(); } })); } }
/////////////////////////////////////////////////////////////////////////////// // Inherited methods // /////////////////////////////////////////////////////////////////////////////// #region OVERRIDES #endregion //OVERRIDES /////////////////////////////////////////////////////////////////////////////// // Eventhandler // /////////////////////////////////////////////////////////////////////////////// #region EVENTHANDLER /// <summary> /// The event handler for the <see cref="Tracker.FrameCaptureComplete"/> /// event which updates the OpenCV image control with the new frame /// according to the current display settings. /// </summary> private void Tracker_FrameCaptureComplete(object sender, EventArgs args) { try { if (this.isReadyToRender) { this.isReadyToRender = false; if (this.isNativeResolution) { if (GTSettings.Current.Visualization.IsVideoModeProcessed) { this.image = this.tracker.GetProcessedImage(); } else { this.image = this.tracker.GetOriginalImage(); } } else { if (GTSettings.Current.Visualization.IsVideoModeProcessed) { this.image = this.tracker.GetProcessedImage(this.Width, this.Height); } else { this.image = this.tracker.GetOriginalImage(this.Width, this.Height); } } if (this.pictureBox.InvokeRequired) { this.pictureBox.BeginInvoke(new MethodInvoker(delegate { this.pictureBox.Image = this.image; })); } else { // Emgu.CV.IImage this.pictureBox.Image = this.image; } this.isReadyToRender = true; } } catch (Exception ex) { ErrorLogger.ProcessException(ex, false); } }
private BitmapImage GetJPEGEncodedImage(Emgu.CV.IImage rawImage, EncoderParameters encoderParameters) { MemoryStream imageMemoryStream = new MemoryStream(); ((System.Drawing.Bitmap)rawImage.Bitmap).Save(imageMemoryStream, GetEncoder(ImageFormat.Jpeg), GetJPEGEncoderParameters()); BitmapImage image = new BitmapImage(); image.BeginInit(); imageMemoryStream.Seek(0, SeekOrigin.Begin); image.StreamSource = imageMemoryStream; image.EndInit(); return(image); }
public static BitmapSource ToBitmapSource(Emgu.CV.IImage image) { using (System.Drawing.Bitmap source = image.Bitmap) { IntPtr ptr = source.GetHbitmap(); BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( ptr, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); DeleteObject(ptr); return(bs); } }
/////////////////////////////////////////////////////////////////////////////// // Eventhandler // /////////////////////////////////////////////////////////////////////////////// #region EVENTHANDLER /// <summary> /// The event handler for the FrameCaptureComplete event /// of the PlayStation camera device CLEyeCameraDevice. /// Sends the new frame to the processing tree of the tracker class. /// </summary> /// <param name="newFrame">An OpenCV image with the new frame.</param> private void PlayStationEyeCamera_FrameCaptureComplete(Emgu.CV.IImage newFrame) { this.ImgCounter++; switch (this.playStationEyeCamera.ColorMode) { case CLEyeCameraColorMode.CLEYE_GRAYSCALE: this.ImageProcessing.NewImage( (Emgu.CV.Image <Emgu.CV.Structure.Gray, byte>)newFrame, Emgu.CV.CvEnum.FLIP.NONE); break; case CLEyeCameraColorMode.CLEYE_COLOR: this.ImageProcessing.NewImage( (Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte>)newFrame, Emgu.CV.CvEnum.FLIP.NONE); break; } if (!this.ImageProcessing.ProcessImage()) { if (this.Calibrating) { this.SaveCalibInfo(); } if (this.Calibration.calibrated) { this.CalculateGazeCoordinates(); } } // Raise OnFrameCaptureComplete event (UI listens for updating video stream) try { this.OnFrameCaptureComplete(); } catch (ThreadInterruptedException ex) { ErrorLogger.RaiseGazeTrackerMessage("An error occured in Tracker.cs (ThreadInterruptedException). Message: " + ex.Message); } catch (Exception we) { ErrorLogger.ProcessException(we, false); } }