private void BaseCalibration(object sender, NewImageEventArgs e) { //var thread = new Thread(() => CalibThread(e)); //thread.SetApartmentState(ApartmentState.STA); //thread.Start(); //thread.Join(); //_vs.Dispatcher.Invoke(() => CalibThread(e)); //_vs.Dispatcher.InvokeAsync(() => //Debug.WriteLine("Calibrating"),DispatcherPriority.Send); //Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, // new Action(() => { })); //_vs.Dispatcher.InvokeAsync(() => CalibThread(e)); //if (calibTask == null || calibTask.Status != TaskStatus.Running) //{ // Task.Factory.StartNew(() => { calibTask = CalibThread(e); }); // Debug.WriteLine("thread started"); // //calibTask = CalibThread(e); //} //Debug.WriteLine(calibTask.Status); //Task.Factory.StartNew(() => CalibThread(e)); Debug.WriteLine("new image, sem = " + _sem.CurrentCount + " " + _t.Status); if (_sem.CurrentCount >= 1)//_t.Status != TaskStatus.Running)// { _sem.Wait(); Task.Factory.StartNew(() => CalibThread(e)); } }
private void NewImage(object sender, NewImageEventArgs e) { //if (PenPositionChanged != null) PenPositionChanged(this, PenTracker.GetPenPosition(e.NewImage)); _gridcheck--; if (_gridcheck == 0) // calibration check needed { _gridcheck = 1000; switch (_calibrator.CheckCalibration()) { case 1: Calibrator.Calibrate(); break; case 2: Calibrator.CalibrateColors(); break; } } }
void bmdm_NewImage(object sender, NewImageEventArgs e) { //needed as can be triggered by event from BMD //Updates the UI in another thread - throws errors without this.Dispatcher.Invoke((Action)(() => { foreach (string imagePath in e.GetPaths) { filesList.Add(imagePath); } setNextImage(); if (btnMediaExpressEnabled.IsChecked) { this.Activate(); } })); }
/// <summary> /// 图像采集完成事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Camera_NewImageEvent(object sender, NewImageEventArgs e) { try { imageInfo = e.ImageInfo; lock (threadLock) { isVisionProcessed = true; } } catch (Exception) { throw; } finally { //注销事件回调 var camera = sender as ICamera; camera.NewImageEvent -= Camera_NewImageEvent; } }
/// <summary> /// /// </summary> /// <param name="param"></param> /// <remarks> /// This code should always be called in main thread /// </remarks> public void RunWait(object @param) { using (var capture = new Capture(m_Source)) { //capture.DuplexQueryFrame(); capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_COUNT, 10); // capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, Config.FRAME_WIDTH); //5168, 1280, 2304, capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, Config.FRAME_HEIGHT); //2907, 720, 1536 capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FOURCC, CvInvoke.CV_FOURCC('U', '2', '6', '3')); //622,3730 capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_MONOCROME, 1); CheckErrorThrow(); var task = Task.Factory.StartNew((o) => { var sw = Stopwatch.StartNew(); for (; ;) { using (Image <Bgr, byte> img = capture.QueryFrame()) { CheckErrorThrow(); if (img != null && NewImage != null && sw.Elapsed > LOOP) { var ne = new NewImageEventArgs() { Image = img.Copy(), Param = o }; NewImage(this, ne); break; } } Thread.Sleep(50); } }, @param, TaskCreationOptions.AttachedToParent); Task.WaitAll(task); } }
/// <summary> /// 图像采集完成事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CameraConfigViewModel_NewImageEvent(object sender, NewImageEventArgs e) { try { using (Bitmap bitmap = CreateBmpImage(e.ImageInfo.PixelFormat, e.ImageInfo.Width, e.ImageInfo.Height, e.ImageInfo.ImagePtr)) { if (bitmap != null) { BitmapImage bitmapToBitmapImage = BitmapToBitmapImage(bitmap); CameraImage = bitmapToBitmapImage; } } } finally { e.ImageInfo.DisposeImageIntPtr?.Invoke(e.ImageInfo.ImagePtr); //由于涉及到image控件的绘制,所以必须要手动GC,否则会存在内存泄漏的风险 GC.Collect(); GC.WaitForPendingFinalizers(); } }
private void Camera_NewImageEvent(object sender, NewImageEventArgs e) { OnNewImageEvent(e.ImageInfo); }
private async Task CalibThread(NewImageEventArgs e) { #if DEBUG e.NewImage.Save(@"C:\temp\srcimg\img" + _calibrationStep + ".jpg"); #endif //lock (_lockObj) { Debug.WriteLine("Calibrating"); if (_errors > 100) { //calibration not possible return; } if (_calibrationStep == CalibrationFrames || !Grid.DataNeeded) { //_cc.NewImage -= BaseCalibration; // TODO _vs.Close(); CalibrationCompleted(this, new EventArgs()); } //else if (_calibrationStep > CalibrationFrames/2) //{ //} else if (_calibrationStep > 2) { var tcb = ThreeChannelBitmap.FromBitmap(e.NewImage); _vs.Clear(); FillRandomRects(); for (int j = 0; j < 3; j++) { var cChan = new OneChannelBitmap(); var diff = new BinaryBitmap(); Debug.WriteLine(j); switch (j) { case 0: cChan = _blackImage.RChannelBitmap - tcb.RChannelBitmap; diff = cChan.GetBinary(RedDiff); #if DEBUG var s = new ThreeChannelBitmap(cChan, new OneChannelBitmap(diff.Width, diff.Height), new OneChannelBitmap(diff.Width, diff.Height)); s.GetVisual().Save(@"C:\temp\rimg\img" + _calibrationStep + ".jpg"); #endif break; case 1: cChan = _blackImage.GChannelBitmap - tcb.GChannelBitmap; diff = cChan.GetBinary(GreenDiff); #if DEBUG s = new ThreeChannelBitmap(new OneChannelBitmap(diff.Width, diff.Height), cChan, new OneChannelBitmap(diff.Width, diff.Height)); s.GetVisual().Save(@"C:\temp\gimg\img" + _calibrationStep + ".jpg"); #endif break; case 2: cChan = _blackImage.BChannelBitmap - tcb.BChannelBitmap; diff = cChan.GetBinary(BlueDiff); #if DEBUG s = new ThreeChannelBitmap(new OneChannelBitmap(diff.Width, diff.Height), new OneChannelBitmap(diff.Width, diff.Height), cChan); s.GetVisual().Save(@"C:\temp\bimg\img" + _calibrationStep + ".jpg"); #endif break; } var topLeftCorner = GetTopLeftCorner(diff); var topRightCorner = GetTopRightCorner(diff); var bottomLeftCorner = GetBottomLeftCorner(diff); var bottomRightCorner = GetBottomRightCorner(diff); if (topLeftCorner.X < topRightCorner.X && topLeftCorner.X > bottomLeftCorner.X && topRightCorner.Y < bottomRightCorner.Y && topRightCorner.Y < bottomLeftCorner.Y && topLeftCorner.Y < bottomRightCorner.Y && bottomLeftCorner.X < bottomRightCorner.X && topLeftCorner.X < bottomRightCorner.X && bottomLeftCorner.X < topRightCorner.X && IsValid(topLeftCorner) && IsValid(topRightCorner) && IsValid(bottomLeftCorner) && IsValid(bottomRightCorner) && true) { //Grid.AddPoint(_rects[j].Left, topLeftCorner); //Grid.AddPoint(_rects[j].TopRight, topRightCorner); //Grid.AddPoint(_rects[j].BottomLeft, bottomLeftCorner); //Grid.AddPoint(_rects[j].BottomRight, bottomRightCorner); } else { _errors++; } } _calibrationStep++; } switch (_calibrationStep) { case 2: var diff = (_blackImage - ThreeChannelBitmap.FromBitmap(e.NewImage)).GetGrayscale(); var b = diff.GetBinary(GreyDiff); var s = new ThreeChannelBitmap(diff, diff, diff); s.GetVisual().Save(@"C:\temp\diffs\img" + _errors + ".jpg"); Grid.TopLeft = GetTopLeftCorner(b); Grid.TopRight = GetTopRightCorner(b); Grid.BottomLeft = GetBottomLeftCorner(b); Grid.BottomRight = GetBottomRightCorner(b); if (Grid.TopLeft.X < Grid.TopRight.X && Grid.TopLeft.X < Grid.BottomRight.X && Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X && Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y && Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y && IsValid(Grid.TopLeft) && IsValid(Grid.TopRight) && IsValid(Grid.BottomLeft) && IsValid(Grid.BottomRight)) { _calibrationStep++; _vs.Clear(); FillRandomRects(); _sem = new SemaphoreSlim(3, 4); } else { _calibrationStep = 0; _errors++; } break; case 1: _blackImage = ThreeChannelBitmap.FromBitmap(e.NewImage); _vs.AddRect(0, 0, _vs.Width, _vs.Height, DColor.FromArgb(255, 255, 255, 255)); _calibrationStep++; break; case 0: Grid = new Grid(e.NewImage.Width, e.NewImage.Height); var thread = new Thread(() => { _vs.Show(); //_vs.Draw(); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); _vs.AddRect(0, 0, _vs.Width, _vs.Height, DColor.FromArgb(255, 0, 0, 0)); _calibrationStep++; break; } } await Task.Delay(500); Debug.WriteLine("releasing"); _sem.Release(); }
protected virtual void OnNewImage(NewImageEventArgs args) { NewImage(this, args); }
private void AcquisitionModule_NewImage(object sender, NewImageEventArgs e) { this.Image = e.Image; this.Process(); }