I would like to make a few circles with default colors and then once they got touched, a new random color will be assigned to those circles. However, the circles will keep changing colors if the kinect players did not take away his hands from the ball. And I would like to make them like this: once the ball get touched, it changes its color, and it will not change to another colors until the next touch by player. Also, it won't random a color when the player's hand is stayed on the ball. And actually I am not sure that the way I random the colors is correct or not. Why the ball will keep changing colors when it is touch? Is there any problem about the looping? using System; using System.Collections.Generic; using System.Linq; using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using KinectWPFEmgu;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.Drawing;
namespace Ball
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private KinectSensor sensor;
private short[] depthData = null;
WriteableBitmap depthBitmap;
WriteableBitmap colorBitmap;
byte[] depthPixels;
byte[] colorPixels;
private Random rnd1 = new Random();
private Boolean touched = false;
int count = 0;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Console.WriteLine("window loaded");
if (KinectSensor.KinectSensors.Count == 0)
{
MessageBox.Show("No Kinects detected");
Application.Current.Shutdown();
}
else
{
sensor = KinectSensor.KinectSensors[0];
if (sensor == null)
{
MessageBox.Show("Kinect is not ready to use");
Application.Current.Shutdown();
}
}
// enable color and depthstream
sensor.DepthStream.Enable();
sensor.ColorStream.Enable();
depthData = new short[sensor.DepthStream.FramePixelDataLength];
depthPixels = new byte[sensor.DepthStream.FramePixelDataLength * sizeof(int)];
colorPixels = new byte[sensor.ColorStream.FramePixelDataLength];
depthBitmap = new WriteableBitmap(sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);
depthImg.Source = depthBitmap;
colorBitmap = new WriteableBitmap(sensor.ColorStream.FrameWidth, sensor.ColorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);
colorImg.Source = colorBitmap;
sensor.DepthFrameReady += new EventHandler<DepthImageFrameReadyEventArgs>(sensor_DepthFrameReady);
sensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(sensor_ColorFrameReady);
sensor.Start();
}
private void sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame == null) return;
colorFrame.CopyPixelDataTo(colorPixels);
colorBitmap.WritePixels(
new Int32Rect(0, 0, colorFrame.Width, colorFrame.Height),
colorPixels, colorFrame.Width * 4, 0);
}
}
private void sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
{
using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
{
if (depthFrame == null) return;
depthFrame.CopyPixelDataTo(depthData);
BitmapSource slicedBmp = depthFrame.SliceDepthImage();
Image<Bgr, Byte> openCVImg = new Image<Bgr, byte>(slicedBmp.ToBitmap());
// convert from Bgr to gray
Image<Gray, Byte> grayImg = openCVImg.Convert<Gray, Byte>();
// find all contours with no holes
Contour<System.Drawing.Point> contours = grayImg.FindContours(
Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL
);
System.Drawing.Color[] colors = new System.Drawing.Color[4];
colors[0] = System.Drawing.Color.Red;
colors[1] = System.Drawing.Color.Green;
colors[2] = System.Drawing.Color.Blue;
colors[3] = System.Drawing.Color.Yellow;
// Ball 1
//System.Drawing.Color c = colors[rnd1.Next(0,4)];
System.Drawing.Color c = System.Drawing.Color.Red;
CircleF cir = new CircleF(new System.Drawing.PointF(50f, 50f), 30);
openCVImg.Draw(cir, new Bgr(c), 5);
//Ball 2
System.Drawing.Color c2 = System.Drawing.Color.Green;
CircleF cir2 = new CircleF(new System.Drawing.PointF(200f, 200f), 30);
openCVImg.Draw(cir2, new Bgr(c2), 5);
while (contours != null)
{
// Ball 1
if (contours.InContour(cir.Center) > 0 && touched == false)
{
c = colors[rnd1.Next(0, 4)];
touched = true;
}
if (contours.BoundingRectangle.Offset((int)cir.Center.X, (int)cir.Center.Y))
{
touched = false;
}
// Ball 2
if (contours.BoundingRectangle.Contains((int)cir2.Center.X, (int)cir2.Center.Y))
{
c2 = colors[rnd1.Next(0, 4)];
}
//if (contours.Area > 10 * 10 && contours.Area < 100 * 100)
//{
openCVImg.Draw(contours, new Bgr(System.Drawing.Color.Red), 2);
//openCVImg.Draw(contours.GetMinAreaRect, new Bgr(System.Drawing.Color.Purple), 2);
openCVImg.Draw(contours.GetConvexHull(Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE),
new Bgr(System.Drawing.Color.Yellow), 2);
// }
contours = contours.HNext;
}
openCVImg.Draw(cir, new Bgr(c), 5);
openCVImg.Draw(cir2, new Bgr(c2), 5);
// display the processed image to WPF Image control
depthImg.Source = ImageHelpers.ToBitmapSource(openCVImg);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire