public void updateInfo(Bitmap frame) { if (frame != null) { frame = Handdetection.imageBinarization(frame, setup.Threshold, setup.Mirror); frame = Handdetection.Findhands(frame, setup.Rectangles); Graphics g = Graphics.FromImage(frame); Brush b = new SolidBrush(Color.FromArgb(64, 0, 191, 255)); foreach (Rectangle r in setup.Rectangles) { g.FillRectangle(b, r); } pictureBoxHandDetection.Image = (Image)frame; } Update(); }
/// <summary> /// Event that occurs when the application is doing nothing. This will update the frame on the picturebox, setting it to the most recent camera frame /// </summary> /// <param name="sender">Object that fired the event</param> /// <param name="e">Extra information on the event</param> private void Application_Idle(Object sender, EventArgs e) { Bitmap bmp = camera.LastFrame; if (bmp != null) { bmp = Handdetection.imageBinarization(bmp, setup.Threshold, setup.Mirror); bmp = GetNonIndexed(bmp); Graphics g = Graphics.FromImage(bmp); Brush b = new SolidBrush(Color.FromArgb(64, 0, 191, 255)); foreach (Rectangle r in rectanglesUnscaled) { g.FillRectangle(b, r); } pictureBox1.Image = (Image)bmp; } }
private void updatePictureBox(Object sender, NewFrameEventArgs e) { Bitmap bmp = (Bitmap)e.Frame.Clone(); if (bmp != null) { bmp = Handdetection.imageBinarization(bmp, setup.Threshold, setup.Mirror); pictureBoxHanddetectionZones.Image = Handdetection.Findhands(bmp, setup.Rectangles); bmp = GetNonIndexed(bmp); Graphics g = Graphics.FromImage(bmp); System.Drawing.Brush b = new SolidBrush(System.Drawing.Color.FromArgb(150, 255, 0, 0)); foreach (System.Drawing.Rectangle r in setup.Rectangles) { g.FillRectangle(b, r); } pictureBoxHandzones.Image = bmp; } }
private void Window_Idle(object sender, EventArgs e) { //update game data gameData.updateInfo(camera.LastFrame); //check for goals for (int i = 0; i < balls.Count; i++) { Ball ball = balls[i]; if (Helper.isGoal(ball, goals)) { //find the player who scored, increase his score int index = players.IndexOf(ball.LastHit); if (index != -1) { //make sure someone hit the ball players[index].Score++; if (Helper.gameIsOver(players[index], setup.MaxScore)) { //if the game is over, display a label Label label = new Label { Content = setup.PlayerNames[index] + " wins the game!", FontSize = 40, FontFamily = new System.Windows.Media.FontFamily("Tahoma") }; Canvas.SetLeft(label, (World.ActualWidth / 4) - label.ActualWidth); Canvas.SetTop(label, (World.ActualHeight / 2) - label.ActualHeight); World.Children.Add(label); } } // Set ball in center of world and reinitialize the speeds. if there were multiple balls, remove the ball that scored instead if (balls.Count == 1) { ResetBall(ball); gameData.updateInfo(camera.LastFrame); PauseResume(); } else { World.Children.Remove(ball.Shape); balls.Remove(ball); } } } //hand detection using (Bitmap bmp = camera.LastFrame) { if (bmp != null) { List <System.Drawing.Point> hands = Handdetection.GetHandLocations(Handdetection.imageBinarization(bmp, setup.Threshold, setup.Mirror), setup.Rectangles); System.Drawing.Size s = bmp.Size; Dispatcher.BeginInvoke(new Action(() => { for (int i = 0; i < players.Count; i++) { Player player = players[i]; //determine which coordinate should be altered if (player.Direction == 'Y') { if (hands[i].Y != 0) { double relativeValue = (double)hands[i].Y / s.Height; //if the player is affected by a reverse powerup, invert position if (player.ReverseTime > 0) { player.Beam.Y = World.ActualHeight - (relativeValue * World.ActualHeight); } else { player.Beam.Y = relativeValue * World.ActualHeight; } } } else { if (hands[i].X != 0) { double relativeValue = (double)hands[i].X / s.Width; //if the player is affected by a reverse powerup, invert position if (player.ReverseTime > 0) { player.Beam.X = World.ActualWidth - (relativeValue * World.ActualWidth); } else { player.Beam.X = relativeValue * World.ActualWidth; } } } } })); } } //ball collision Physics p = new Physics(World, setup); Ball b; for (int i = 0; i < balls.Count; i++) { b = balls[i]; System.Windows.Point[] intersection = new System.Windows.Point[3]; try { Dispatcher.Invoke(new Action(() => { intersection = p.Intersection(b); })); if (!(intersection[0].X < 0)) { b = p.BallCollisionBeam(b, intersection); } b = p.HandleSideCollisions(b); } catch (Exception) { } //ball movement try { Dispatcher.Invoke(new Action(() => { b.X += b.SpeedX; })); } catch (Exception) { } try { Dispatcher.Invoke(new Action(() => { b.Y += b.SpeedY; })); } catch (Exception) { } //Handle powerups try { Dispatcher.Invoke(new Action(() => { p.HandlePowerups(b, players, powerups, balls); })); } catch (Exception) { } } }