void CameraVehicle_MotorSpeedChanged(object sender, MotorSpeedChangedEventArgs e) { backgroundImage = null; Dispatcher.Invoke(() => { currentTimeTextBlock.Text = e.Tick.ToString(); leftWheelSpeedTextBlock.Text = e.LeftSpeed.ToString("f2"); rightWheelSpeedTextBlock.Text = e.RightSpeed.ToString("f2"); }); if (saveToFileCheckBox.IsChecked.GetValueOrDefault(false) && saveToFileCheckBox.IsEnabled) { actionList.Add(e); } if (e.LeftSpeed == 0 && e.RightSpeed == 0 && lastMotorEvent != null) { if (lastMotorEvent.LeftSpeed == lastMotorEvent.RightSpeed) { double distance = (e.Tick - lastMotorEvent.Tick) / 1000 * lastMotorEvent.LeftSpeed; //vehicle.Forward(distance); MapWindow w = GetMapWindow(); w.startingVehicleState = vehicle.State; w.Draw(); } } lastMotorEvent = e; }
private void AnalyzeFrame(object sender, DoWorkEventArgs e) { Image <Bgr, byte> img = ((ImageAvailableEventArgs)e.Argument).Image; if (backgroundImage == null) { backgroundImage = new Image <Gray, byte>(img.Size); } //long t = Environment.TickCount; Image <Gray, byte> b = GetBackground(img); Dispatcher.BeginInvoke(new Action(() => CvInvoke.cvShowImage("Background", img.Copy(b)))); //Debug.WriteLine("GetBackground: " + (Environment.TickCount - t)); //t = Environment.TickCount; //CvInvoke.cvShowImage("Masked", img); Contour <PointInt> contours = b.FindContours(); Contour <PointInt> biggestContour = contours; while (contours != null) { if (contours.Area > biggestContour.Area) { biggestContour = contours; } contours = contours.HNext; } //Debug.WriteLine("contours: " + (Environment.TickCount - t)); //t = Environment.TickCount; bool voteFull = false; Image <Gray, byte> tempImage = new Image <Gray, byte>(backgroundImage.Size); tempImage.Draw(biggestContour, new Gray(10), -1); backgroundImage = backgroundImage.Add(tempImage); PointInt pUseless = PointInt.Empty; voteFull = !backgroundImage.CheckRange(0, 250, ref pUseless); if (voteFull) { collectedBackgroundCount++; Dispatcher.Invoke(() => { backgroundImage = backgroundImage - new Gray(130); //w.carRectangle.Width = this.vehicle.State.Width; //w.carRectangle.Height = this.vehicle.State.Length; //w.carRectangle.SetValue(Canvas.LeftProperty, this.vehicle.State.Center.X - this.vehicle.State.Width / 2); //w.carRectangle.SetValue(Canvas.TopProperty, this.vehicle.State.Center.Y + this.vehicle.State.Length / 2); backgroundImage._Dilate(1); //backgroundImage.ROI = new Rectangle(0, backgroundImage.Height - 1, backgroundImage.Width, 1); //backgroundImage._Or(new Image<Gray, byte>(backgroundImage.Width, 1, new Gray(255))); //backgroundImage.ROI = Rectangle.Empty; CvInvoke.cvShowImage("Background-Draw to map", backgroundImage.ThresholdBinary(new Gray(0), new Gray(255))); AddToMap(backgroundImage.ThresholdBinary(new Gray(0), new Gray(255)), img); MapWindow w = GetMapWindow(); w.Draw(); CameraVehicle vehicle = this.vehicle as CameraVehicle; //if (vehicle != null) //{ // if (collectedBackgroundCount < 3) // vehicle.SetSpeed(4.05, 5000); //} getBackgroundCheckBox.IsChecked = false; CvInvoke.cvShowImage("VotedBackground", backgroundImage); }); } else { Dispatcher.BeginInvoke(new Action(() => CvInvoke.cvShowImage("VotedBackground", backgroundImage))); } }