private void AllFramesReady(object sender,AllFramesReadyEventArgs e) { using(ColorImageFrame colorImage=e.OpenColorImageFrame()) using(SkeletonFrame skeletonFrame=e.OpenSkeletonFrame()) if(colorImage!=null&&skeletonFrame!=null){ colorImage.CopyPixelDataTo(bitmappixels); skeletonFrame.CopySkeletonDataTo(skeletons); bitmap.WritePixels(updateRect,bitmappixels,bitmap.PixelWidth*sizeof(int),0); using(DrawingContext drawingContext=drawingGroup.Open()){ drawingContext.DrawImage(bitmap,drawingRect); //drawingContext.DrawGeometry(button1.IsHitting?Brushes.White:null,new Pen(Brushes.Blue,2.0),button1.Geometry); //drawingContext.DrawGeometry(button2.IsHitting?Brushes.White:null,new Pen(Brushes.Blue,2.0),button2.Geometry); foreach(Skeleton skel in skeletons){ if(skel.TrackingState==SkeletonTrackingState.Tracked){ foreach(Joint joint in skel.Joints){ if(joint.TrackingState==JointTrackingState.Tracked){ var depthPoint=sensor.MapSkeletonPointToDepth(joint.Position,DepthImageFormat.Resolution640x480Fps30); drawingContext.DrawEllipse(Brushes.Green,null,new Point(depthPoint.X,depthPoint.Y),15,15); } } drawingContext.DrawRectangle(Brushes.Red,null,new Rect(0.0,0.0,distance1.Distance,50.0)); drawingContext.DrawLine(new Pen(Brushes.Blue,10),volume1.MiddlePoint,volume1.RightHandLocation); var mat=Matrix.Identity; mat.RotateAt(volume1.Angle,volume1.MiddlePoint.X,volume1.MiddlePoint.Y); drawingContext.DrawLine(new Pen(Brushes.Blue,10),volume1.MiddlePoint,mat.Transform(volume1.RightHandLocation)); drawingContext.DrawText(new FormattedText(volume1.Angle.ToString(),CultureInfo.CurrentCulture,FlowDirection.LeftToRight,new Typeface("MS Gothic"),150,Brushes.Blue),new Point()); break; } } } } return; }
void kinect_AllFramesReady( object sender, AllFramesReadyEventArgs e ) { using ( var colorFrame = e.OpenColorImageFrame() ) { if ( colorFrame != null ) { var pixel = new byte[colorFrame.PixelDataLength]; colorFrame.CopyPixelDataTo( pixel ); ImageRgb.Source = BitmapSource.Create( colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixel, colorFrame.Width * 4 ); } } using ( var depthFrame = e.OpenDepthImageFrame() ) { if ( depthFrame != null ) { // Depth情報を入れる // GetRawPixelData()はインタラクションライブラリ内で実装された拡張メソッド stream.ProcessDepth( depthFrame.GetRawPixelData(), depthFrame.Timestamp ); } } using ( var skeletonFrame = e.OpenSkeletonFrame() ) { if ( skeletonFrame != null ) { var skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo( skeletons ); // スケルトン情報を入れる stream.ProcessSkeleton( skeletons, kinect.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp ); } } }
void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) { //throw new NotImplementedException(); using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { // check for frame drop. if (skeletonFrame == null) { return; } // copy the frame data in to the collection skeletonFrame.CopySkeletonDataTo(totalSkeleton); // get the first Tracked skeleton Skeleton firstSkeleton = (from trackskeleton in totalSkeleton where trackskeleton.TrackingState == SkeletonTrackingState.Tracked select trackskeleton).FirstOrDefault(); // if the first skeleton returns null if (firstSkeleton == null) { return; } this.myCanvas.Children.Clear(); this.DrawSkeleton(firstSkeleton); recognitionEngine.Skeleton = firstSkeleton; recognitionEngine.StartRecognize(); } }
private Skeleton GetTrackedSkeleton(AllFramesReadyEventArgs e) { using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame()) { SkeletonDetected = false; if (skeletonFrameData == null) { return null; } // Kinect SDK always returns 6 skeleton const int skeletonCount = 6; Skeleton[] allSkeletons = new Skeleton[skeletonCount]; skeletonFrameData.CopySkeletonDataTo(allSkeletons); //Get the first tracked skeleton out of the 6 Skeleton trackedSkeleton = null; foreach (Skeleton skeleton in allSkeletons) { // if no skeleton is tracked, null will be returned. if (skeleton.TrackingState == SkeletonTrackingState.Tracked) { trackedSkeleton = skeleton; SkeletonDetected = true; break; } } return trackedSkeleton; } }
public void KinectAllFramesReady(object sender,AllFramesReadyEventArgs e) { using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null && screenManager != null) { // take skeleton data and update avatar state skeletonFrame.CopySkeletonDataTo(skeletonData); float headX = skeletonData[0].Joints[JointType.Head].Position.X; //floats between -1 and 1 float headY = skeletonData[0].Joints[JointType.Head].Position.Y; midViewPort.X = screenManager.GraphicsDevice.Viewport.Width / 2; midViewPort.Y = screenManager.GraphicsDevice.Viewport.Height / 2; //set the posistion of the head's rectangle to be in the center of the screen and move by the joint amount //TODO: figure out if skeleton data stream has lower left origin, because XNA has upper left origin and we adjust for that. head.SetRectPos((int)((headX * 100) + midViewPort.X), (int)((headY * 100) + midViewPort.Y)); //head.SetRectPos((int)((headX * 100)), (int)((headY * 100) )); // Console.WriteLine( "head: " + head.Rectangle.X + ", " + head.Rectangle.Y ); Console.WriteLine("joint: " + headX + ", " + headY); } else { // skeletonFrame is null because the request did not arrive in time } } }
void kinect_AllFramesReady( object sender, AllFramesReadyEventArgs e ) { image1.Source = e.OpenColorImageFrame().ToBitmapSource(); // スケルトンフレームを取得する SkeletonFrame skeletonFrame = e.OpenSkeletonFrame(); if ( skeletonFrame != null ) { // スケルトンデータを取得する Skeleton[] skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo( skeletonData ); // プレーヤーごとのスケルトンを描画する foreach ( var skeleton in skeletonData ) { var head = skeleton.Joints[JointType.Head]; if ( head.TrackingState == JointTrackingState.Tracked ) { ColorImagePoint point = kinect.MapSkeletonPointToColor( head.Position, kinect.ColorStream.Format ); var x = image2.Width / 2; var y = image2.Height / 2; image2.Margin = new Thickness( point.X - x, point.Y - y, 0, 0 ); image2.Visibility = System.Windows.Visibility.Visible; } } } }
void Kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { using (var Frame = e.OpenColorImageFrame()) { if (Frame != null) { Frame.CopyPixelDataTo(Pixels); Bitmap.WritePixels( new Int32Rect(0, 0, Bitmap.PixelWidth, Bitmap.PixelHeight), Pixels, Bitmap.PixelWidth * sizeof(int), 0); } } using (var Frame = e.OpenSkeletonFrame()) { if (Frame != null) { var skeletons = new Skeleton[Frame.SkeletonArrayLength]; Frame.CopySkeletonDataTo(skeletons); var skel = (from s in skeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault(); if (skel != null) { var left_hand = skel.Joints[JointType.HandLeft]; var rite_hand = skel.Joints[JointType.HandRight]; var shoulderRight = skel.Joints[JointType.ShoulderRight]; var fleft = skel.Joints[JointType.FootLeft]; var frite = skel.Joints[JointType.FootRight]; Joint head = skel.Joints[JointType.Head]; Point mousePos = new Point((rite_hand.Position.X * 1300 + 683), (rite_hand.Position.Y * -1300 + 768)); //двойное нажатие левой кнопкой мыши if (distance(head.Position, left_hand.Position) < 0.06f) { NatiteMethods.sendMouseDoubleClick(mousePos); // правая кнопка мыши } else if(distance(left_hand.Position, rite_hand.Position) < 0.03f) { NatiteMethods.mouseLeftButtonDown(mousePos); // перетаскивание } else if(distance(shoulderRight.Position, left_hand.Position) < 0.03f) { NatiteMethods.sendMouseRightclick(mousePos); } if (fleft.Position.Y <= fleft.Position.Y + 0.5f) { // Get the virtual key code for the character we want to press //int key = 87; //uint vkKey = NatiteMethods.VkKeyScan((char) key); //NatiteMethods.keybd_event(vkKey, 0, 0, 0); //NatiteMethods.keybd_event(vkKey, 0, 2, 0); } NatiteMethods.SetCursorPos((int) mousePos.X, (int) mousePos.Y); } } } }
private void AllFramesReady(object sender,AllFramesReadyEventArgs e) { if(secondBuffer!=null){ using(ColorImageFrame colorImage=e.OpenColorImageFrame()) using(SkeletonFrame skeletonFrame=e.OpenSkeletonFrame()) if(colorImage!=null&&skeletonFrame!=null){ skeletonFrame.CopySkeletonDataTo(skeletons); //colorImage.CopyPixelDataTo(bitmappixels); //bitmap.WritePixels(updateRect,bitmappixels,bitmap.PixelWidth*sizeof(int),0); using(DrawingContext drawingContext=drawingGroup.Open()){ //drawingContext.DrawImage(bitmap,drawingRect); drawingContext.DrawRectangle(Brushes.Black,null,drawingRect); var redPen=new Pen(Brushes.Red,5.0); drawingContext.DrawGeometry(button.IsHitting?Brushes.White:null,redPen,button.Geometry); drawingContext.DrawGeometry(muteButton.IsHitting?Brushes.White:null,redPen,muteButton.Geometry); //drawingContext.DrawGeometry(boostButton.IsHitting?Brushes.White:null,redPen,boostButton.Geometry); drawingContext.DrawGeometry(gateButton.IsHitting?Brushes.White:null,redPen,gateButton.Geometry); drawingContext.DrawText(new FormattedText(secondBuffer.Status.Playing?"■":"▶",CultureInfo.CurrentCulture,FlowDirection.LeftToRight,new Typeface("メイリオ"),44,Brushes.Red),new Point(0,0)); foreach(Skeleton skel in skeletons){ if(skel.TrackingState==SkeletonTrackingState.Tracked){ foreach(Joint joint in skel.Joints){ if(joint.TrackingState==JointTrackingState.Tracked){ var depthPoint=sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(joint.Position,DepthImageFormat.Resolution640x480Fps30); drawingContext.DrawEllipse(Brushes.Green,null,new Point(depthPoint.X,depthPoint.Y),10,10); } } if(mode==1){ drawingContext.DrawLine(new Pen(Brushes.DarkBlue,20),distance.Joint1Location,distance.Joint2Location); var volume=-10000+(distance.Distance<=250?distance.Distance:250)*35; if(timer.IsEnabled) prevvolume=volume; else secondBuffer.Volume=volume; }else if(mode==2){ var pen=new Pen(new SolidColorBrush(Color.FromArgb(0x7F,0,0,0x8B)),12.5); drawingContext.DrawLine(pen,volume.MiddlePoint,volume.RightHandLocation); var mat=Matrix.Identity; mat.RotateAt(volume.Angle,volume.MiddlePoint.X,volume.MiddlePoint.Y); drawingContext.DrawLine(pen,volume.MiddlePoint,mat.Transform(volume.RightHandLocation)); settings[0].Gain=15/180*(-volume.Angle); settings[1].Gain=10/180*(-volume.Angle); settings[2].Gain=15/180*(volume.Angle); settings[3].Gain=15/180*(volume.Angle); settings[4].Gain=15/180*(volume.Angle); for(int i=0;i<settings.Length;i++){ var effectInst=(ParamEqEffect)secondBuffer.GetEffects(i); effectInst.AllParameters=settings[i]; } } drawingContext.DrawText(new FormattedText("ControlMode:"+(mode==0?"None":mode==1?"Volume":"Filter"),CultureInfo.CurrentCulture,FlowDirection.LeftToRight,new Typeface("メイリオ"),40,Brushes.White),new Point(0,400)); if(muteButton.IsHitting){ secondBuffer.Volume=-10000; } break; } } } } } return; }
/// <summary> /// Manage frames of kinect sensor according to the services activated /// </summary> /// <param name="e"></param> private void ManageAllFrame(AllFramesReadyEventArgs e) { if (!IsRunning) { return; } // SkeletonTracking Frame Manager using (SkeletonFrame SFrame = e.OpenSkeletonFrame()) { try { ManageSkeletonFrame(SFrame); } catch (Exception ex) { // Just log the error Console.Error.WriteLine("Error with skeleton frame : " + ex.Message + " _ " + ex.StackTrace); } } // Color Frame Manager if (PropertiesPluginKinect.Instance.EnableColorFrameService) { using (ColorImageFrame CFrame = e.OpenColorImageFrame()) { try { ManageColorFrame(CFrame); } catch (Exception ex) { // Just log the error Console.Error.WriteLine("Error with color frame : " + ex.Message + " _ " + ex.StackTrace); } } } // Depth Frame Manager if (PropertiesPluginKinect.Instance.EnableDepthFrameService || PropertiesPluginKinect.Instance.KinectPointingModeEnabled || PropertiesPluginKinect.Instance.EnableGestureGrip) { using (DepthImageFrame DFrame = e.OpenDepthImageFrame()) { try { ManageDepthFrame(DFrame); } catch (Exception ex) { // Just log the error Console.Error.WriteLine("Error with depth frame : " + ex.Message + " _ " + ex.StackTrace); } } } }
private void updateVis(object sender, AllFramesReadyEventArgs e) { if (recorder.areRecording) { ellipse1.Fill = new SolidColorBrush(Colors.Red); } else if (recorder.handsMet) { ellipse1.Fill = new SolidColorBrush(Colors.Blue); } else { ellipse1.Fill = new SolidColorBrush(Colors.Green); } if (e.OpenSkeletonFrame() != null) { Skeleton[] skelData = new Skeleton[6]; e.OpenSkeletonFrame().CopySkeletonDataTo(skelData); label1.Content = (skelData[0].Joints[JointType.HandLeft].Position.X - skelData[0].Joints[JointType.HandRight].Position.X).ToString(); } }
Skeleton GetFirstSkeleton(AllFramesReadyEventArgs e) { using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame()) { if (skeletonFrameData == null) return null; skeletonFrameData.CopySkeletonDataTo(allSkeletons); Skeleton first = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault(); return first; } }
static void FrameHandler(object sender, AllFramesReadyEventArgs afrea) { using(var sf = afrea.OpenSkeletonFrame()) { var data = new Skeleton[kinect.SkeletonStream.FrameSkeletonArrayLength]; sf.CopySkeletonDataTo(data); foreach(Joint joint in data[0].Joints) { if(joint.JointType == JointType.WristLeft) Console.WriteLine("Left Wrist: ({0},{1},{2})", joint.Position.X, joint.Position.Y, joint.Position.Z); } } }
//returns the latest picture!!!!! void FramesReady(object sender, AllFramesReadyEventArgs e) { ColorImageFrame VFrame = e.OpenColorImageFrame(); if (VFrame == null) return; byte[] pixelS = new byte[VFrame.PixelDataLength]; bmp = ImageToBitmap(VFrame); SkeletonFrame SFrame = e.OpenSkeletonFrame(); if (SFrame == null) return; Graphics g = Graphics.FromImage(bmp); Skeleton[] Skeletons = new Skeleton[SFrame.SkeletonArrayLength]; SFrame.CopySkeletonDataTo(Skeletons); foreach (Skeleton S in Skeletons) { if (S.TrackingState == SkeletonTrackingState.Tracked) { //body DrawBone(JointType.Head, JointType.ShoulderCenter, S, g); DrawBone(JointType.ShoulderCenter, JointType.Spine, S, g); DrawBone(JointType.Spine, JointType.HipCenter, S, g); //left leg DrawBone(JointType.HipCenter, JointType.HipLeft, S, g); DrawBone(JointType.HipLeft, JointType.KneeLeft, S, g); DrawBone(JointType.KneeLeft, JointType.AnkleLeft, S, g); DrawBone(JointType.AnkleLeft, JointType.FootLeft, S, g); //Right Leg DrawBone(JointType.HipCenter, JointType.HipRight, S, g); DrawBone(JointType.HipRight, JointType.KneeRight, S, g); DrawBone(JointType.KneeRight, JointType.AnkleRight, S, g); DrawBone(JointType.AnkleRight, JointType.FootRight, S, g); //Left Arm DrawBone(JointType.ShoulderCenter, JointType.ShoulderLeft, S, g); DrawBone(JointType.ShoulderLeft, JointType.ElbowLeft, S, g); DrawBone(JointType.ElbowLeft, JointType.WristLeft, S, g); DrawBone(JointType.WristLeft, JointType.HandLeft, S, g); //Right Arm DrawBone(JointType.ShoulderCenter, JointType.ShoulderRight, S, g); DrawBone(JointType.ShoulderRight, JointType.ElbowRight, S, g); DrawBone(JointType.ElbowRight, JointType.WristRight, S, g); DrawBone(JointType.WristRight, JointType.HandRight, S, g); } } }
private void kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { byte[] imagem = ObterImagemSensorRGB(e.OpenColorImageFrame()); if( chkEscalaCinza.IsChecked.HasValue && chkEscalaCinza.IsChecked.Value) ReconhecerDistancia(e.OpenDepthImageFrame(),imagem, 2000); if (imagem != null) canvasKinect.Background = new ImageBrush(BitmapSource.Create(kinect.ColorStream.FrameWidth, kinect.ColorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null, imagem, kinect.ColorStream.FrameWidth * kinect.ColorStream.FrameBytesPerPixel)); canvasKinect.Children.Clear(); DesenharEsqueletoUsuario(e.OpenSkeletonFrame()); }
public static Skeleton Get(AllFramesReadyEventArgs e, Skeleton[] allSkeletons) { using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame()) { if (skeletonFrameData != null) { skeletonFrameData.CopySkeletonDataTo(allSkeletons); //get the first tracked skeleton Skeleton first = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault(); return first; } } return null; }
public void KinectAllFramesReady(object sender, AllFramesReadyEventArgs e) { using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { skeletonFrame.CopySkeletonDataTo(skeletonData); if (skeletonData[0] != null && skeletonData[0].TrackingState == SkeletonTrackingState.Tracked) { animationPlayer.Update(skeletonData[0], Matrix.Identity); //animationPlayer.Draw(cameraArc, cameraDistance, cameraRotation); } } } }
private void Kinect_AllFramesReady(object sender, AllFramesReadyEventArgs allFrameEvent) { byte[] imagem = ObterImagemSensorRGB(allFrameEvent.OpenColorImageFrame()); FuncoesProfundidade(allFrameEvent.OpenDepthImageFrame(), imagem, 2000); if (imagem != null) { canvasKinect.Background = new ImageBrush(BitmapSource.Create(Kinect.ColorStream.FrameWidth, Kinect.ColorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null, imagem, Kinect.ColorStream.FrameBytesPerPixel * Kinect.ColorStream.FrameWidth)); } canvasKinect.Children.Clear(); FuncoesEsqueletoUsuario(allFrameEvent.OpenSkeletonFrame()); }
/// <summary> /// Handles the Kinect AllFramesReady event /// </summary> private void Sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) { ColorImageFrame colorFrame = null; DepthImageFrame depthFrame = null; Skeleton[] skeletonData; try { colorFrame = e.OpenColorImageFrame(); depthFrame = e.OpenDepthImageFrame(); using (var skeletonFrame = e.OpenSkeletonFrame()) { if (colorFrame == null || depthFrame == null || skeletonFrame == null) return; skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletonData); } // Find a skeleton to track. // First see if our old one is good. // When a skeleton is in PositionOnly tracking state, don't pick a new one // as it may become fully tracked again. Skeleton skeletonOfInterest = skeletonData.FirstOrDefault(s => s.TrackingId == this.trackedSkeletonId && s.TrackingState != SkeletonTrackingState.NotTracked); if (skeletonOfInterest == null) { // Old one wasn't around. Find any skeleton that is being tracked and use it. skeletonOfInterest = skeletonData.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked); if (skeletonOfInterest != null) this.trackedSkeletonId = skeletonOfInterest.TrackingId; } if (this.FrameDataUpdated != null) this.FrameDataUpdated(this, new FrameData(colorFrame, depthFrame, skeletonOfInterest)); } finally { if (colorFrame != null) colorFrame.Dispose(); if (depthFrame != null) depthFrame.Dispose(); } }
void KinectSensorAllFramesReady(object sender, AllFramesReadyEventArgs e) { using (var frame = e.OpenSkeletonFrame()) { if (frame == null) return; if (_skeletons == null) _skeletons = new Skeleton[frame.SkeletonArrayLength]; frame.CopySkeletonDataTo(_skeletons); } var trackedSkeleton = _skeletons.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked); if (trackedSkeleton == null) return; DrawJoints(trackedSkeleton); }
//Obtiene el primer skeleto a mano Skeleton GetFirstSkeleton(AllFramesReadyEventArgs e) { using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame()) { //Instanciar nuevo skeleton skeletons = new Skeleton[skeletonFrameData.SkeletonArrayLength]; //Prevencion contra framedrop (perdida de informacion) if (skeletonFrameData == null) { return null; } //Copia datos a skeletos skeletonFrameData.CopySkeletonDataTo(skeletons); //Elige el primer skeleto de todos Skeleton first = (from s in skeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault(); return first; } }
// FrameReady イベントのハンドラ // 背景を描画し、骨格情報から頭の角度を取得しマウスを動かす private void AllFramesReady( object sender, AllFramesReadyEventArgs e ) { // 描画の準備 var drawCtx = drawVisual.RenderOpen(); // 背景の描画 drawBase( drawCtx ); using ( SkeletonFrame skeletonFrame = e.OpenSkeletonFrame() ) { if ( skeletonFrame != null ) { // 骨格情報をバッファにコピー skeletonFrame.CopySkeletonDataTo( skeletonBuffer ); // 取得できた骨格毎にループ foreach ( Skeleton skeleton in skeletonBuffer ) processSkeleton( skeleton, drawCtx ); } } // 画面に表示するビットマップに描画 drawCtx.Close(); bmpBuffer.Render( drawVisual ); }
private Skeleton GetFirstSkeleton(AllFramesReadyEventArgs e) { var allSkeletons = new Skeleton[6]; using (var skeletonFrameData = e.OpenSkeletonFrame()) { if (skeletonFrameData == null) { return null; } skeletonFrameData.CopySkeletonDataTo(allSkeletons); //get the first tracked skeleton var first = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault(); return first; } }
/// <summary> /// Returns the first skeleton (the kinect recognizes up to two) /// </summary> /// <param name="e"></param> /// <returns></returns> private Skeleton getFirstSkeleton(AllFramesReadyEventArgs e) { using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame()) { if (skeletonFrameData == null) { return null; } Skeleton[] mainSkeletonData = new Skeleton[mainKinectSensor.SkeletonStream.FrameSkeletonArrayLength]; skeletonFrameData.CopySkeletonDataTo(mainSkeletonData); //get the first tracked skeleton Skeleton first = (from s in mainSkeletonData where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault(); return first; } }
// すべてのデータの更新通知を受け取る void kinect_AllFramesReady( object sender, AllFramesReadyEventArgs e ) { // Disposableなのでusingでくくる using ( ColorImageFrame colorFrame = e.OpenColorImageFrame() ) { if ( colorFrame != null ) { imageRgbCamera.Source = colorFrame.ToBitmapSource(); } } // Disposableなのでusingでくくる using ( DepthImageFrame depthFrame = e.OpenDepthImageFrame() ) { if ( depthFrame != null ) { imageDepthCamera.Source = depthFrame.ToBitmapSource(); } } // Disposableなのでusingでくくる using ( SkeletonFrame skeletonFrame = e.OpenSkeletonFrame() ) { if ( skeletonFrame != null ) { // 骨格位置の表示 ShowSkeleton( skeletonFrame ); } } }
void FramesReady(object sender, AllFramesReadyEventArgs e) { using (ColorImageFrame VFrame = e.OpenColorImageFrame()) { if (VFrame == null) { return; } byte[] pixelS = new byte[VFrame.PixelDataLength]; Bitmap bmap = ImageToBitmap(VFrame); if (!((KinectSensor)sender).SkeletonStream.IsEnabled) { return; } using (SkeletonFrame SFrame = e.OpenSkeletonFrame()) { if (SFrame == null) { return; } Graphics g = Graphics.FromImage(bmap); Skeleton[] Skeletons = new Skeleton[SFrame.SkeletonArrayLength]; SFrame.CopySkeletonDataTo(Skeletons); Color cor = System.Drawing.Color.Aquamarine; foreach (Skeleton S in Skeletons) { if (S.TrackingState == SkeletonTrackingState.Tracked) { //Desenha Corpo DrawBone(JointType.Head, JointType.ShoulderCenter, S, g); DrawBone(JointType.ShoulderCenter, JointType.Spine, S, g); DrawBone(JointType.Spine, JointType.HipCenter, S, g); //Desenha Braço Esquerdo DrawBone(JointType.ShoulderCenter, JointType.ShoulderLeft, S, g); DrawBone(JointType.ShoulderLeft, JointType.ElbowLeft, S, g); DrawBone(JointType.ElbowLeft, JointType.WristLeft, S, g); DrawBone(JointType.WristLeft, JointType.HandLeft, S, g); //Desenha Braço Direito DrawBone(JointType.ShoulderCenter, JointType.ShoulderRight, S, g); DrawBone(JointType.ShoulderRight, JointType.ElbowRight, S, g); DrawBone(JointType.ElbowRight, JointType.WristRight, S, g); DrawBone(JointType.WristRight, JointType.HandRight, S, g); //Desenha Perna Esquerda DrawBone(JointType.HipCenter, JointType.HipRight, S, g); DrawBone(JointType.HipRight, JointType.KneeRight, S, g); DrawBone(JointType.KneeRight, JointType.AnkleRight, S, g); DrawBone(JointType.AnkleRight, JointType.FootRight, S, g); //Desenha Perna Direita DrawBone(JointType.HipCenter, JointType.HipLeft, S, g); DrawBone(JointType.HipLeft, JointType.KneeLeft, S, g); DrawBone(JointType.KneeLeft, JointType.AnkleLeft, S, g); DrawBone(JointType.AnkleLeft, JointType.FootLeft, S, g); //Desenha VÉRTICES foreach (Joint j in S.Joints) { DrawJoint(j.JointType, S, g, cor); } skeleton = S; if (t == null) { t = new Thread(new ThreadStart(isFall)); t.Start(); } } } } campo.Image = bmap; } }
private void ShowSkeleton(AllFramesReadyEventArgs e) { // キャンバスをクリアする canvas1.Children.Clear(); // スケルトンフレームを取得する SkeletonFrame skeletonFrame = e.OpenSkeletonFrame(); if (skeletonFrame != null) { // スケルトンデータを取得する Skeleton[] skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletonData); // プレーヤーごとのスケルトンを描画する switch (skeletonMax(skeletonData)) { case 0: drawString("画面の中に入ってください!"); if (flicker <= 0) { flicker = flicker + (float)0.05; } else if (flicker >= 1) { flicker = flicker - (float)0.05; } break; case 1: skeleton = getSkeleton(skeletonData).First(); foreach (Joint joint in skeleton.Joints) { if (joint.JointType == JointType.HandLeft) { if (Math.Max(0, Math.Min(127, (int)(127 * (joint.Position.Z - 1)))) >= 63) { //drawCoordinate(joint, Colors.Pink); midi1.sendNoteOff(kinectTestWPF1.App.noteChannel); kinectTestWPF1.kinect2Midi.sendingPitch = Pitch.A0; } else { drawLine(get10(joint.Position.Y)); //drawCoordinate(joint, Colors.Aqua); midi1.sendNoteOn(joint.Position.Y, 0, kinectTestWPF1.App.noteChannel); } } if (joint.JointType == JointType.HandRight) { midi1.sendAll(joint.Position.X, joint.Position.Y, joint.Position.Z); drawCircle(joint, Colors.Red, 5); if (Math.Max(0, Math.Min(127, (int)(127 * (joint.Position.Z - 1)))) <= 63) { //drawCoordinate(joint, Colors.Aqua); drawImage(joint, "images/blue.png"); } else { //drawCoordinate(joint, Colors.Pink); drawImage(joint, "images/pink.png"); } } else if (joint.JointType == JointType.Head) { drawCircle(joint, Colors.Aqua, 5); } else { drawCircle(joint, Colors.Blue, 5); } } break; case 2: skeleton = getSkeleton(skeletonData).First(); foreach (Joint joint in skeleton.Joints) { if (joint.JointType == JointType.HandRight) { midi1.sendAll(joint.Position.X, joint.Position.Y, joint.Position.Z); drawCircle(joint, Colors.Red, 5); if (Math.Max(0, Math.Min(127, (int)(127 * (joint.Position.Z - 1)))) <= 63) { //drawCoordinate(joint, Colors.Aqua); } else { //drawCoordinate(joint, Colors.Pink); } } } skeleton = getSkeleton(skeletonData).Skip(1).First(); foreach (Joint joint in skeleton.Joints) { if (joint.JointType == JointType.HandLeft) { if (Math.Max(0, Math.Min(127, (int)(127 * (joint.Position.Z - 1)))) >= 63) { //drawCoordinate(joint, Colors.Pink); midi1.sendNoteOff(kinectTestWPF1.App.noteChannel); kinectTestWPF1.kinect2Midi.sendingPitch = Pitch.A0; } else { drawLine(get10(joint.Position.Y)); //drawCoordinate(joint, Colors.Aqua); midi1.sendNoteOn(joint.Position.Y, 0, kinectTestWPF1.App.noteChannel); } } } break; default: break; } } }
public void FramesReady(object sender, AllFramesReadyEventArgs e) { ColorImageFrame Vframe = e.OpenColorImageFrame(); if (Vframe == null) { return; } SkeletonFrame sframe = e.OpenSkeletonFrame(); if (sframe == null) { return; } Skeleton[] skeletons = new Skeleton[sframe.SkeletonArrayLength]; sframe.CopySkeletonDataTo(skeletons); BitmapSource bmp = ImagetoBitMap(Vframe); // CroppedBitmap croppedBitmap = new CroppedBitmap(bmp, new Int32Rect(20, 20, 100, 100)); using (DrawingContext dc = this.drawingGroup.Open()) { // dc.DrawImage(bmp, new Rect(0.0, 0.0, RenderWidth, RenderHeight)); //dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight)); if (skeletons != null) { foreach (Skeleton item in skeletons) { Random r = new Random(); switch (r.Next(1, 4)) { case 1: trackedBonPen = new Pen(Brushes.White, 5); break; case 2: trackedBonPen = new Pen(Brushes.Yellow, 5); break; } RenderClippedEdges(item, dc); if (item.TrackingState == SkeletonTrackingState.Tracked) { this.DrawBonesAndJoints(item, dc); } else if (item.TrackingState == SkeletonTrackingState.PositionOnly) { dc.DrawEllipse( this.centerPointBrush, null, this.SkeletonPointToScreen(item.Position), BodyCenterThickness, BodyCenterThickness); } } } } img_sensor.Source = bmp; }
private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } // Check for image format changes. The FaceTracker doesn't // deal with that so we need to reset. if (this.depthImageFormat != depthImageFrame.Format) { this.ResetFaceTracking(); this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.ResetFaceTracking(); this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; } // Create any buffers to store copies of the data we work with if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } // Get the skeleton information if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); // Update the list of trackers and the trackers with the current frame information foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked || skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { // We want keep a record of any skeleton, tracked or untracked. if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId)) { this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker()); } // Give each tracker the upated frame. SkeletonFaceTracker skeletonFaceTracker; if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.Kinect, colorImageFormat, colorImage, depthImageFormat, depthImage, skeleton); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } } this.RemoveOldTrackers(skeletonFrame.FrameNumber); this.InvalidateVisual(); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
void sensor_allFramesReady(object sender, AllFramesReadyEventArgs e) { if (windowClosing) { return; } int value; if (int.TryParse(this.textBox_init.Text, out value)) { initFrames = value; } if (fpsEnd == 1) { //FPS Auswahl. Bei niedrigen Frameraten werden empfangene Frames übersprungen (nicht angezeigt) Int16 fps = Convert.ToInt16(this.dropDown_fps.Text); switch (fps) { case 30: fpsEnd = 1; break; case 15: fpsEnd = 2; break; case 10: fpsEnd = 3; break; case 5: fpsEnd = 6; break; case 1: fpsEnd = 30; break; } using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { // Kinect Color Frame to Bitmap tempColorFrame = ColorImageFrameToBitmap(colorFrame); //this.pictureBox_colorPic.Image = new Bitmap(tempColorFrame, this.pictureBox_colorPic.Width, this.pictureBox_colorPic.Height); } } /* * using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) * { * if (depthFrame != null) * { * // Kinect Depth Frame to Bitmap * Bitmap tempDepthFrame = DepthImageFrameToBitmap(depthFrame); * this.pictureBox_depthPic.Image = new Bitmap(tempDepthFrame, this.pictureBox_depthPic.Width, this.pictureBox_depthPic.Height); * } * } */ using (SkeletonFrame skelFrame = e.OpenSkeletonFrame()) { if (skelFrame != null) { Image tempSkeletonFrame = new Bitmap(this.pictureBox_skeleton.Width, this.pictureBox_skeleton.Height); // zeichne Skelette auf schwarzem Hintergrund in der Picturebox this.pictureBox_skeleton.BackColor = Color.Black; if (checkBox_colorCam.Checked) { tempSkeletonFrame = tempColorFrame; } Skeleton[] skeletons = new Skeleton[skelFrame.SkeletonArrayLength]; skelFrame.CopySkeletonDataTo(skeletons); if (skeletons.Length != 0) { foreach (Skeleton skel in skeletons) { if (skel.TrackingState == SkeletonTrackingState.Tracked) { //Zeichne Skelett DrawSkeletons(tempSkeletonFrame, skel); if (BVHFile != null) { if (BVHFile.isRecording == true && BVHFile.isInitializing == true) { BVHFile.Entry(skel); if (BVHFile.intializingCounter > initFrames) { BVHFile.startWritingEntry(); } } if (BVHFile.isRecording == true && BVHFile.isInitializing == false) { BVHFile.Motion(skel); this.textBox_sensorStatus.Text = "Record"; this.textBox_sensorStatus.BackColor = Color.Green; } } } } } this.pictureBox_skeleton.Image = tempSkeletonFrame; } } } else { fpsEnd -= 1; } }
void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) { if (windowClosing) { return; } int value; if (int.TryParse(this.textBoxInitial.Text, out value)) { initFrames = value; } if (fpsEnd == 1) { // FPS - Количество кадров в секунду (При <30 лишние кадры отбрасываются) Int16 fps = Convert.ToInt16(this.dropDownFps.Text); switch (fps) { case 30: fpsEnd = 1; break; case 15: fpsEnd = 2; break; case 10: fpsEnd = 3; break; case 5: fpsEnd = 6; break; case 1: fpsEnd = 30; break; } // Сохранение текущего полученного кадра изображения во временное хранилище using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { tempColorFrame = ColorImageFrameToBitmap(colorFrame); } } // Получение кадра потока глубины using (SkeletonFrame skelFrame = e.OpenSkeletonFrame()) { if (skelFrame != null) { Image tempSkeletonFrame = new Bitmap(this.pictureBoxSkeleton.Width, this.pictureBoxSkeleton.Height); // Отображение кадра скелетного потока и кадра видеопотока this.pictureBoxSkeleton.BackColor = Color.Black; if (checkBoxСolor.Checked) { tempSkeletonFrame = tempColorFrame; } Skeleton[] skeletons = new Skeleton[skelFrame.SkeletonArrayLength]; skelFrame.CopySkeletonDataTo(skeletons); if (skeletons.Length != 0) { foreach (Skeleton skel in skeletons) { if (skel.TrackingState == SkeletonTrackingState.Tracked) { // Нарисовать элемент скелета, если он находится в поле зрения камеры DrawSkeletons(tempSkeletonFrame, skel); if (BVHFile != null) { if (BVHFile.isRecording == true && BVHFile.isInitializing == true) { BVHFile.Entry(skel); if (BVHFile.intializingCounter > initFrames) { BVHFile.startWritingEntry(); } } if (BVHFile.isRecording == true && BVHFile.isInitializing == false) { BVHFile.Motion(skel); this.textBoxSensorStatus.Text = "Recording."; this.textBoxSensorStatus.BackColor = Color.Green; } } } } } this.pictureBoxSkeleton.Image = tempSkeletonFrame; } } } else { fpsEnd -= 1; } }
void sensor_allFramesReady(object sender, AllFramesReadyEventArgs e) { if (windowClosing) { return; } int value; if (int.TryParse(this.textBox_init.Text, out value)) { initFrames = value; } if (fpsEnd == 1) { //FPS Suggestion. Bei niedrigen Frameraten werden empfangene Frames übersprungen (nicht angezeigt) Int16 fps = Convert.ToInt16(this.dropDown_fps.Text); switch (fps) { case 30: fpsEnd = 1; break; case 15: fpsEnd = 2; break; case 10: fpsEnd = 3; break; case 5: fpsEnd = 6; break; case 1: fpsEnd = 30; break; } using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { // Kinect Color Frame to Bitmap tempColorFrame = ColorImageFrameToBitmap(colorFrame); this.pictureBox_colorPic.BackgroundImage = tempColorFrame; // this.pictureBox_colorPic.Image = new Bitmap(tempColorFrame, this.pictureBox_colorPic.Width, this.pictureBox_colorPic.Height); // Too slow ifnot scaled down by 10x //this.pictureBox_colorPic.BackgroundImage = BitmapManipulator.ConvertImageToHeatmapBitMap(BitmapManipulator.ScaleBitmap(tempColorFrame, 0.1, 0.1, System.Drawing.Drawing2D.InterpolationMode.Default), Heatmap, false); } } using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) { if (depthFrame != null) { // Kinect Depth Frame to Bitmap Bitmap tempDepthFrame = DepthImageFrameToBitmap(depthFrame); this.pictureBox_depthPic.BackgroundImage = tempDepthFrame; // this.pictureBox_depthPic.Image = new Bitmap(tempDepthFrame, this.pictureBox_depthPic.Width, this.pictureBox_depthPic.Height); objectsFound = 0; depthBmp = ImageHelpers.SliceDepthImage(depthFrame, (int)trackBarDepthMinDistance.Value, (int)trackBarDepthMaxDistance.Value); Image<Bgr, Byte> openCVImg = new Image<Bgr, byte>(depthBmp); Image<Gray, byte> gray_image = openCVImg.Convert<Gray, byte>(); using (MemStorage stor = new MemStorage()) { //Find contours with no holes try CV_RETR_EXTERNAL to find holes Contour<System.Drawing.Point> contours = gray_image.FindContours( Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL, stor); for (int i = 0; contours != null; contours = contours.HNext) { i++; if ((contours.Area > Math.Pow(trackBarObjectMinSize.Value, 2)) && (contours.Area < Math.Pow(trackBarObjectMaxSize.Value, 2))) { MCvBox2D box = contours.GetMinAreaRect(); openCVImg.Draw(box, new Bgr(System.Drawing.Color.Red), 2); objectsFound++; } } } pictureBox_depthPicSmoothed.BackgroundImage = openCVImg.Bitmap; richTextBoxObjectFound.Text = objectsFound.ToString(); // The Smoothed Image will apply both a filter and a weighted moving average over the // depth data from the Kinect (depending on UI selections) //pictureBox_depthPicSmoothed.BackgroundImage = CreateSmoothImageFromDepthArray(tempDepthFrame); // pictureBox_depthPicSmoothed.Image = CreateSmoothImageFromDepthArray(tempDepthFrame); // Create a pseudo color of the depth map // Too slow for 30 frames per second if not scaled down by 10x //this.pictureBox_depthPicSmoothed.BackgroundImage = BitmapManipulator.ConvertImageToHeatmapBitMap(BitmapManipulator.ScaleBitmap(tempDepthFrame, 0.1, 0.1, System.Drawing.Drawing2D.InterpolationMode.Default), Heatmap, false); // Create a byte arrya of the depth data //ConvertDepthFrameData(sender, depthFrame); DepthImagePixel[] depth = depthFrame.GetRawPixelData(); pictureBox_colorPic.BackgroundImage = LightBuzz.Vitruvius.WinForms.DepthExtensions.ToBitmap(depthFrame, PixelFormat.Format32bppRgb, LightBuzz.Vitruvius.WinForms.DepthImageMode.Colors); } } using (SkeletonFrame skelFrame = e.OpenSkeletonFrame()) { if (skelFrame != null) { Image tempSkeletonFrame = new Bitmap(this.pictureBox_skeleton.Width, this.pictureBox_skeleton.Height); // make the background black if there is no image this.pictureBox_skeleton.BackColor = Color.Black; this.pictureBox_skeleton.BackgroundImage = null; if (checkBox_colorCam.Checked) { tempSkeletonFrame = new Bitmap(tempColorFrame); } if (this.checkBoxShowSkeleton.Checked) { Skeleton[] skeletons = new Skeleton[skelFrame.SkeletonArrayLength]; skelFrame.CopySkeletonDataTo(skeletons); if (skeletons.Length != 0) { foreach (Skeleton skel in skeletons) { if (skel.TrackingState == SkeletonTrackingState.Tracked) { //Zeichne Skelett DrawSkeletons(tempSkeletonFrame, skel); if (skel != null) { double height = LightBuzz.Vitruvius.SkeletonExtensions.Height(skel); } if (NGEFile != null) { if (NGEFile.isRecording == true && NGEFile.isInitializing == true) { NGEFile.Entry(skel); if (NGEFile.intializingCounter > initFrames) { NGEFile.startWritingEntry(); } } if (NGEFile.isRecording == true && NGEFile.isInitializing == false) { NGEFile.Motion(skel); this.textBox_sensorStatus.Text = "Record"; this.textBox_sensorStatus.BackColor = Color.Green; } } } } } this.pictureBox_skeleton.BackgroundImage = tempSkeletonFrame; // this.pictureBox_skeleton.Image = tempSkeletonFrame; //this.pictureBox_skeleton.Image = new Bitmap(tempSkeletonFrame, this.pictureBox_skeleton.Width, this.pictureBox_skeleton.Height); } } } } else { fpsEnd -= 1; } UpdateFps(); }
/// <summary> /// RGBカメラ、スケルトンのフレーム更新イベント /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { try { KinectSensor kinect = sender as KinectSensor; using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { byte[] colorPixel = new byte[colorFrame.PixelDataLength]; colorFrame.CopyPixelDataTo(colorPixel); imageRgb.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, colorPixel, colorFrame.Width * colorFrame.BytesPerPixel); } } using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { // トラッキングされているスケルトンのジョイントを描画する Skeleton skeleton = skeletonFrame.GetFirstTrackedSkeleton(); if ((skeleton != null) && (skeleton.TrackingState == SkeletonTrackingState.Tracked)) { Joint hand = skeleton.Joints[JointType.HandRight]; if (hand.TrackingState == JointTrackingState.Tracked) { ImageSource source = imageRgb.Source; DrawingVisual drawingVisual = new DrawingVisual(); using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { //バイト列をビットマップに展開 //描画可能なビットマップを作る drawingContext.DrawImage(imageRgb.Source, new Rect(0, 0, source.Width, source.Height)); // 手の位置に円を描画 DrawSkeletonPoint(drawingContext, hand); } // 描画可能なビットマップを作る // http://stackoverflow.com/questions/831860/generate-bitmapsource-from-uielement RenderTargetBitmap bitmap = new RenderTargetBitmap((int)source.Width, (int)source.Height, 96, 96, PixelFormats.Default); bitmap.Render(drawingVisual); imageRgb.Source = bitmap; // Frame中の手の位置をディスプレイの位置に対応付ける ColorImagePoint point = kinect.MapSkeletonPointToColor(hand.Position, // スケルトン座標 → RGB画像座標 kinect.ColorStream.Format); System.Windows.Forms.Screen screen = System.Windows.Forms.Screen.AllScreens[0]; // メインディスプレイの情報を取得 point.X = (point.X * screen.Bounds.Width) / kinect.ColorStream.FrameWidth; point.Y = (point.Y * screen.Bounds.Height) / kinect.ColorStream.FrameHeight; // マウスカーソルの移動 SendInput.MouseMove(point.X, point.Y, screen); // クリック動作 if (IsClicked(skeletonFrame, point)) { SendInput.LeftClick(); } } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Function for processing each skeleton /// </summary> /// <param name="e">All frames ready event arguments</param> private void ProcessSkeletons(AllFramesReadyEventArgs e) { // Clear current relationship results Relationships.Clear(); using (SkeletonFrame skeletonData = e.OpenSkeletonFrame()) { if (skeletonData == null) { return; } Skeletons = new Skeleton[skeletonData.SkeletonArrayLength]; skeletonData.CopySkeletonDataTo(Skeletons); // Cycle Through Each Skeleton foreach (Skeleton skeleton in Skeletons) { if (skeleton.TrackingState != SkeletonTrackingState.Tracked) { continue; } if (!Relationships.ContainsKey(skeleton.TrackingId)) { Relationships.Add(skeleton.TrackingId, new Dictionary <JointType, Dictionary <string, Dictionary <Relationship, Single> > >()); } // Cycle Through Each Actor foreach (KeyValuePair <JointType, Dictionary <string, List <Relationship> > > actor in JointRelationshipsUsed) { SkeletonPoint actorPos = NormalizeJoint(skeleton.Joints[actor.Key]); if (!Relationships[skeleton.TrackingId].ContainsKey(actor.Key)) { Relationships[skeleton.TrackingId].Add(actor.Key, new Dictionary <string, Dictionary <Relationship, Single> >()); } // Cycle Through Each Relative foreach (KeyValuePair <string, List <Relationship> > relative in actor.Value) { if (!Relationships[skeleton.TrackingId][actor.Key].ContainsKey(relative.Key)) { Relationships[skeleton.TrackingId][actor.Key].Add(relative.Key, new Dictionary <Relationship, Single>()); } // Cycle Through Each Relationship foreach (Relationship relation in relative.Value) { JointType? relativeJoint = relative.Key.ParseJoint(); SkeletonPoint relativePos = new SkeletonPoint(); if (relativeJoint != null) { relativePos = NormalizeJoint(skeleton.Joints[relativeJoint.Value]); } else { relativePos = StaticReferences[relative.Key]; } switch (relation) { case Relationship.Above: Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, Convert.ToSingle(actorPos.Y >= relativePos.Y)); break; case Relationship.Below: Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, Convert.ToSingle(actorPos.Y < relativePos.Y)); break; case Relationship.Behind: Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, Convert.ToSingle(actorPos.Z >= relativePos.Z)); break; case Relationship.InfrontOf: Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, Convert.ToSingle(actorPos.Z < relativePos.Z)); break; case Relationship.RightOf: Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, Convert.ToSingle(actorPos.X >= relativePos.X)); break; case Relationship.LeftOf: Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, Convert.ToSingle(actorPos.X < relativePos.X)); break; case Relationship.Distance: SkeletonPoint deltaPos = new SkeletonPoint() { X = actorPos.X - relativePos.X, Y = actorPos.Y - relativePos.Y, Z = actorPos.Z - relativePos.Z }; Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, Convert.ToSingle(Math.Sqrt((deltaPos.X * deltaPos.X) + (deltaPos.Y * deltaPos.Y) + (deltaPos.Z * deltaPos.Z)))); break; case Relationship.XChange: if (ReferenceRelationships.ContainsKey(skeleton.TrackingId)) { Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, ReferenceRelationships[skeleton.TrackingId][actor.Key].X - actorPos.X); } break; case Relationship.YChange: float curY = NormalizeJoint(skeleton.Joints[actor.Key]).Y; if (ReferenceRelationships.ContainsKey(skeleton.TrackingId)) { Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, ReferenceRelationships[skeleton.TrackingId][actor.Key].Y - actorPos.Y); } break; case Relationship.ZChange: float curZ = NormalizeJoint(skeleton.Joints[actor.Key]).Z; if (ReferenceRelationships.ContainsKey(skeleton.TrackingId)) { Relationships[skeleton.TrackingId][actor.Key][relative.Key].Add(relation, ReferenceRelationships[skeleton.TrackingId][actor.Key].Z - actorPos.Z); } break; default: break; } } } } // Now that the current relationships have been determined process the result for gesture recognition ProcessGestures(skeleton, Relationships[skeleton.TrackingId]); } } }
//function for event-handling. Every data-update from the sensor is handled in this function. //If there is any data it copies it to a bitmap making it possible to overwrite the Image-control in XAML. public void framesready(object sender, AllFramesReadyEventArgs e, System.Windows.Controls.Image image, KinectSensor sensor) { using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame == null) { return; } // Important rendering objects. Bitmap bmap = Drawing.ImageToBitmap(colorFrame); Graphics g = Graphics.FromImage(bmap); SkeletonFrame SFrame = e.OpenSkeletonFrame(); //If the frame would be null you can't do anything. if (SFrame == null) { return; } //Create skeleton-array for skeleton-data-storing Skeleton[] Skeletons = new Skeleton[SFrame.SkeletonArrayLength]; //-------set important values for the slider-control. And set values that are good for---- // back -angle while running. slider.Maximum = 80; slider.Minimum = -20; int sliderValue = (int)slider.Value; if (sliderValue >= -5 && sliderValue <= 15) { System.Windows.Media.Color color = new System.Windows.Media.Color(); color = System.Windows.Media.Color.FromRgb(0, 255, 0); slider.Background = new System.Windows.Media.SolidColorBrush(color); } else { System.Windows.Media.Color color = new System.Windows.Media.Color(); color = System.Windows.Media.Color.FromRgb(255, 0, 0); slider.Background = new System.Windows.Media.SolidColorBrush(color); } //-------------------------------------------------------------------------------------- SFrame.CopySkeletonDataTo(Skeletons); foreach (Skeleton S in Skeletons) { if (S.TrackingState == SkeletonTrackingState.Tracked) { if (sensor == _sensor) { Drawing.DrawTrackedSkeletonJoint(S.Joints, S, g, sensor); if ((0f <= S.Joints[JointType.FootLeft].Position.Z || S.Joints[JointType.FootLeft].Position.Z < 0f)) { time.Add(MilliSeconds.ToString()); Drawing.WritePositionToFile(S.Joints[JointType.FootLeft], FootPositions, time); } // Drawing.WriteAngleToFile(S.Joints[JointType.KneeLeft], S.Joints[JointType.AnkleLeft], S.Joints[JointType.HipLeft], S.Joints[JointType.AnkleRight], kneeup, heelkick); textBox.Text = Drawing.calculateAngleFoot(S.Joints).ToString(); angle_foot = Drawing.calculateAngleFoot(S.Joints); } else { Drawing.DrawSkeletonSidewaySensor(S.Joints, S, g, sensor); try { slider.Value = Drawing.calculateAngleBack(S.Joints); angle_back = slider.Value; } catch (ArgumentException) { MessageBox.Show(""); } Drawing.WriteAngleToFile(S.Joints[JointType.KneeLeft], S.Joints[JointType.AnkleLeft], S.Joints[JointType.HipLeft], S.Joints[JointType.AnkleRight], kneeup, heelkick); } } } //Change the source of the Image-instance in the XAML-file to the current frame. image.Source = Drawing.Convert(bmap); } }
private void AllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } // Check for changes in any of the data this function is receiving // and reset things appropriately. if (this.depthImageFormat != depthImageFrame.Format) { this.DestroyFaceTracker(); this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.DestroyFaceTracker(); this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; this.colorImageWritableBitmap = null; this.ColorImage.Source = null; this.theMaterial.Brush = null; } if (this.skeletonData != null && this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = null; } // Create any buffers to store copies of the data we work with if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } if (this.colorImageWritableBitmap == null) { this.colorImageWritableBitmap = new WriteableBitmap( colorImageFrame.Width, colorImageFrame.Height, 96, 96, PixelFormats.Bgr32, null); this.ColorImage.Source = this.colorImageWritableBitmap; this.theMaterial.Brush = new ImageBrush(this.colorImageWritableBitmap) { ViewportUnits = BrushMappingMode.Absolute }; } if (this.skeletonData == null) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } // Copy data received in this event to our buffers. colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); this.colorImageWritableBitmap.WritePixels( new Int32Rect(0, 0, colorImageFrame.Width, colorImageFrame.Height), this.colorImage, colorImageFrame.Width * Bgr32BytesPerPixel, 0); // Find a skeleton to track. // First see if our old one is good. // When a skeleton is in PositionOnly tracking state, don't pick a new one // as it may become fully tracked again. Skeleton skeletonOfInterest = this.skeletonData.FirstOrDefault( skeleton => skeleton.TrackingId == this.trackingId && skeleton.TrackingState != SkeletonTrackingState.NotTracked); if (skeletonOfInterest == null) { // Old one wasn't around. Find any skeleton that is being tracked and use it. skeletonOfInterest = this.skeletonData.FirstOrDefault( skeleton => skeleton.TrackingState == SkeletonTrackingState.Tracked); if (skeletonOfInterest != null) { // This may be a different person so reset the tracker which // could have tuned itself to the previous person. if (this.faceTracker != null) { this.faceTracker.ResetTracking(); } this.trackingId = skeletonOfInterest.TrackingId; } } bool displayFaceMesh = false; if (skeletonOfInterest != null && skeletonOfInterest.TrackingState == SkeletonTrackingState.Tracked) { if (this.faceTracker == null) { try { this.faceTracker = new FaceTracker(this.Kinect); } catch (InvalidOperationException) { // During some shutdown scenarios the FaceTracker // is unable to be instantiated. Catch that exception // and don't track a face. Debug.WriteLine("AllFramesReady - creating a new FaceTracker threw an InvalidOperationException"); this.faceTracker = null; } } if (this.faceTracker != null) { FaceTrackFrame faceTrackFrame = this.faceTracker.Track( this.colorImageFormat, this.colorImage, this.depthImageFormat, this.depthImage, skeletonOfInterest); if (faceTrackFrame.TrackSuccessful) { this.UpdateMesh(faceTrackFrame); // Only display the face mesh if there was a successful track. displayFaceMesh = true; } } } else { this.trackingId = -1; } this.viewport3d.Visibility = displayFaceMesh ? Visibility.Visible : Visibility.Hidden; } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
/// <summary> /// RGBカメラ、距離カメラ、骨格のフレーム更新イベント /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { try { // Kinectのインスタンスを取得する KinectSensor kinect = sender as KinectSensor; if (kinect == null) { return; } // RGBカメラのフレームデータを取得する using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { // RGBカメラのピクセルデータを取得する byte[] colorPixel = new byte[colorFrame.PixelDataLength]; colorFrame.CopyPixelDataTo(colorPixel); // ピクセルデータをビットマップに変換する Bitmap bitmap = new Bitmap(kinect.ColorStream.FrameWidth, kinect.ColorStream.FrameHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb); Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); BitmapData data = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb); Marshal.Copy(colorPixel, 0, data.Scan0, colorPixel.Length); bitmap.UnlockBits(data); pictureBoxRgb.Image = bitmap; } } // 書き込み用のビットマップデータを作成(32bit bitmap) // 16bpp グレースケールは表示できない // 距離カメラのフレームデータを取得する using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) { if (depthFrame != null) { // 距離データを画像化して表示 Bitmap bitmap = new Bitmap(kinect.DepthStream.FrameWidth, kinect.DepthStream.FrameHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb); Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); BitmapData data = bitmap.LockBits(rect, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); byte[] gray = ConvertDepthColor(kinect, depthFrame); Marshal.Copy(gray, 0, data.Scan0, gray.Length); bitmap.UnlockBits(data); pictureBoxDepth.Image = bitmap; } } // スケルトンのフレームを取得する using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { Graphics g = Graphics.FromImage(pictureBoxRgb.Image); // スケルトンのデータを取得する Skeleton[] skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletons); // トラッキングされているスケルトンのジョイントを描画する foreach (var skeleton in skeletons) { // スケルトンがトラッキング状態(デフォルトモード)の場合は、ジョイントを描画する if (skeleton.TrackingState == SkeletonTrackingState.Tracked) { // ジョイントを描画する foreach (Joint joint in skeleton.Joints) { // ジョイントがトラッキングされていなければ次へ if (joint.TrackingState != JointTrackingState.Tracked) { continue; } // スケルトンの座標を、RGBカメラの座標に変換して円を書く DrawEllipse(kinect, g, joint.Position); } } // スケルトンが位置追跡(ニアモードの)の場合は、スケルトン位置(Center hip)を描画する else if (skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { // スケルトンの座標を、RGBカメラの座標に変換して円を書く DrawEllipse(kinect, g, skeleton.Position); } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
void SensorAllFramesReady(object sender, AllFramesReadyEventArgs e) { if (windowClosing) { return; } if (replay != null && !replay.IsFinished) { return; } if (fpsEnd == 1) { fpsEnd = SelectFPS(); using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { System.Drawing.Image tempStreamFrame = new Bitmap((int)this.image_stream.MinWidth, (int)this.image_stream.MinHeight); // Kinect Color Frame to Bitmap tempColorFrame = ColorImageFrameToBitmap(colorFrame); tempStreamFrame = tempColorFrame; // Help to translate system Drawing.Image to Windows.Media.ImageSource BitmapImage bi = new BitmapImage(); bi.BeginInit(); // Save to a memory stream... tempStreamFrame.Save(ms_Stream, ImageFormat.Bmp); // Rewind the stream... ms_Stream.Seek(0, SeekOrigin.Begin); // Tell the WPF image to use this stream... bi.StreamSource = ms_Stream; bi.EndInit(); this.image_stream.Source = bi; // Record if (recorder != null && recorder.Options != 0) { recorder.Record(colorFrame); } } } using (SkeletonFrame skelFrame = e.OpenSkeletonFrame()) { if (skelFrame != null) { Skeleton[] skeletons = new Skeleton[skelFrame.SkeletonArrayLength]; skelFrame.CopySkeletonDataTo(skeletons); if (skeletons.Length != 0) { foreach (Skeleton skel in skeletons) { if (skel.TrackingState == SkeletonTrackingState.Tracked) { if (BVHFile != null) { if (BVHFile.IsRecording == true && BVHFile.IsInitializing == true) { BVHFile.Entry(skel); if (BVHFile.intializingCounter > initFrames) { BVHFile.StartWritingEntry(); } } if (BVHFile.IsRecording == true && BVHFile.IsInitializing == false) { BVHFile.Motion(skel); Controls.ConsoleControl.LogEntries.Add(new LogEntry(DateTime.Now, "Recording...")); } } } } } } } } else { fpsEnd -= 1; } }
// Reads the skeleton public void UpdateSkeleton(object sender, AllFramesReadyEventArgs imageFrames) { using (SkeletonFrame skeletonFrame = imageFrames.OpenSkeletonFrame()) { if (skeletonFrame != null) { if ((SkeletonData == null) || (SkeletonData.Length != skeletonFrame.SkeletonArrayLength)) { SkeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } skeletonFrame.CopySkeletonDataTo(SkeletonData); } } if (SkeletonData != null) { //Console.WriteLine(skeletonData.Length); foreach (Skeleton skel in SkeletonData) { if (skel.TrackingState == SkeletonTrackingState.Tracked) { Skeleton = skel; } } } }
/// <summary> /// Event handler for Kinect sensor's SkeletonFrameReady event /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void SensorAllFramesReady(object sender, AllFramesReadyEventArgs e) { if (shuttingDown) { return; } KinectSensor sensor = (KinectSensor)sender; ColorImageFrame colorFrame = e.OpenColorImageFrame(); if (colorFrame == null) return; if (colorPixelData[sensor] == null) colorPixelData[sensor] = new byte[colorFrame.PixelDataLength]; colorFrame.CopyPixelDataTo(colorPixelData[sensor]); colorFrame = null; DepthImageFrame depthFrame = e.OpenDepthImageFrame(); if (depthFrame == null) return; if (depthPixelData[sensor] == null) depthPixelData[sensor] = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(depthPixelData[sensor]); depthFrame = null; SkeletonFrame skeletonFrame = e.OpenSkeletonFrame(); if (skeletonFrame == null) return; skeletonFrame.CopySkeletonDataTo(skeletons[sensor]); skeletonFrame = null; SensorFrameHelper(sensor, true); }
private void KinectSensorOnAllFramesReady(object sender, AllFramesReadyEventArgs e) { if (frameProccessed[1] == false) { frameProccessed[1] = true; } else { frameProccessed[1] = false; return; } ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = e.OpenColorImageFrame(); depthImageFrame = e.OpenDepthImageFrame(); skeletonFrame = e.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } if (this.depthImageFormat != depthImageFrame.Format) { this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; } if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } using (depthImageFrame) { if (depthImageFrame != null && skeletonData != null) { foreach (Skeleton sd in skeletonData) { if (sd.TrackingState == SkeletonTrackingState.Tracked || sd.TrackingState == SkeletonTrackingState.PositionOnly) { Joint joint = sd.Joints[JointType.Head]; DepthImagePoint depthPoint; CoordinateMapper coordinateMapper = new CoordinateMapper(frontSensor); depthPoint = coordinateMapper.MapSkeletonPointToDepthPoint(joint.Position, DepthImageFormat.Resolution320x240Fps30); point = new System.Windows.Point((int)(frontSensor.ColorStream.FrameWidth * depthPoint.X / depthImageFrame.Width), (int)(frontSensor.ColorStream.FrameHeight * depthPoint.Y / depthImageFrame.Height)); /* textBlock1.Text = string.Format("X:{0:0.00} Y:{1:0.00} Z:{2:0.00}", * point.X, * point.Y, * joint.Position.Z); */ Canvas.SetLeft(headEllipse, point.X - headEllipse.Width / 2); Canvas.SetTop(headEllipse, point.Y - headEllipse.Height / 2); if (this.faceTracker == null) { try { this.faceTracker = new FaceTracker(frontSensor); } catch (InvalidOperationException) { // During some shutdown scenarios the FaceTrack // is unable to be instantiated. Catch that exception // and don't track a face. this.faceTracker = null; } } if (this.faceTracker != null) { FaceTrackFrame frame = this.faceTracker.Track( colorImageFormat, colorImage, depthImageFormat, depthImage, sd); if (frame.TrackSuccessful) { faceTriangles = frame.GetTriangles(); this.facePoints = frame.GetProjected3DShape(); var faceModelPts = new List <Point>(); var faceModel = new List <FaceModelTriangle>(); for (int i = 0; i < this.facePoints.Count; i++) { faceModelPts.Add(new Point(this.facePoints[i].X + 0.5f, this.facePoints[i].Y + 0.5f)); } foreach (var t in faceTriangles) { var triangle = new FaceModelTriangle(); triangle.P1 = faceModelPts[t.First]; //triangle.P2 = faceModelPts[t.Second]; //triangle.P3 = faceModelPts[t.Third]; faceModel.Add(triangle); } Canvas.SetLeft(noseEllipse, faceModel[108].P1.X - noseEllipse.Width / 2); Canvas.SetTop(noseEllipse, faceModel[108].P1.Y - noseEllipse.Height / 2); nosePoint = new Point(faceModel[108].P1.X, faceModel[108].P1.Y); } } } } } } getAttentionAngle(nosePoint); } }
private void run(object sender, AllFramesReadyEventArgs e) { //if (!takeFrame) using (ColorImageFrame frame_colour = e.OpenColorImageFrame()) using (DepthImageFrame frame_depth = e.OpenDepthImageFrame()) using (SkeletonFrame frame_skel = e.OpenSkeletonFrame()) { frames++; if (frames % gap != 0) { //try { e.OpenColorImageFrame().Dispose(); } //catch (Exception noFrameException) { }; takeFrame = true; return; } else { takeFrame = false; } if (null != frame_colour) { byte[] rawColorImage = new byte[frame_colour.PixelDataLength]; frame_colour.CopyPixelDataTo(rawColorImage); ProcessFrame(rawColorImage); if (null != frame_depth) { DepthImagePixel[] depthImage = new DepthImagePixel[frame_depth.PixelDataLength]; frame_depth.CopyDepthImagePixelDataTo(depthImage); int newX = Math.Max(x - 150, 0); depth = depthImage[x + (y * 640)].Depth; lbl_depth.Content = depth; /* * //double x_pos = Math.Round((2.2 * 2 * Math.Tan(57) * (this.x - 320))/ 640, 4); * double x_pos = 0.00425 * (this.x - 320); * * double y_pos = Math.Round((2.2 * 2 * Math.Tan(21.5) * (this.y - 240) * -1) / 480, 4); * double y_pos2 = (y_pos * -1) -0.45; * // +1.05 * //Console.WriteLine("Depth: " + depth + ", " + * //label3.Content = "X/Y = " + x_pos + ", " + y_pos + " (" + y_pos2 + ")"; * * double actual_y_skel = (y_pos2) * 1000; // -1.6 * this.y_actual = actual_y_skel; * this.x_actual = x_pos; * //double actual_x_skel = (x_pos - 2.2) * 1000; */ //double x_pos = Math.Round((2.2 * 2 * Math.Tan(57) * tempX) / 640, 4); double x_pos = 0.00425 * (this.x - 320); double y_pos = Math.Round((2.2 * 2 * Math.Tan(21.5) * (this.y - 240) * -1) / 480, 4); //double y_pos2 = (y_pos * -1) + 1.05; double y_pos2 = (y_pos * -1) - 0.155; this.x_actual = x_pos; this.y_actual = y_pos2; } } processSkeleton(frame_skel); //ProcessFrame(MaskedRGB); } }
private void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) { handleSkeletonFrame(e.OpenSkeletonFrame()); handleDepthImageFrame(e.OpenDepthImageFrame()); handleColorImageFrame(e.OpenColorImageFrame()); }
private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } // Check for image format changes. The FaceTracker doesn't // deal with that so we need to reset. if (this.depthImageFormat != depthImageFrame.Format) { this.ResetFaceTracking(); this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.ResetFaceTracking(); this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; } // Create any buffers to store copies of the data we work with if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } // Get the skeleton information if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); // Update the list of trackers and the trackers with the current frame information var i = 0; foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked || skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { // We want keep a record of any skeleton, tracked or untracked. if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId)) { this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker()); } var position = skeleton.Position; if (position.X != 0 && position.Y != 0 && position.Z != 0) { Console.WriteLine($"Face {i}: X {position.X}, Y {position.Y}, Z {position.Z}"); string text = position.X + "\r\n" + position.Y + "\r\n" + position.Z; System.IO.File.WriteAllText(@"C:\Users\Mykayla\Desktop\transforms\coords_" + file_index + ".txt", text); file_index++; } // Give each tracker the upated frame. SkeletonFaceTracker skeletonFaceTracker; if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.Kinect, colorImageFormat, colorImage, depthImageFormat, depthImage, skeleton); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } i += 1; } this.RemoveOldTrackers(skeletonFrame.FrameNumber); this.InvalidateVisual(); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
private void SensorAllFramesReady(object sender, AllFramesReadyEventArgs e) { Skeleton[] skeletons = new Skeleton[0]; bool depthReceived = false; bool colorReceived = false; using (DepthImageFrame framesDistancia = e.OpenDepthImageFrame()) { if (framesDistancia == null) { return; } framesDistancia.CopyDepthImagePixelDataTo(this.depthPixels); depthReceived = true; if (datosDistancia == null) { datosDistancia = new short[framesDistancia.PixelDataLength]; } if (colorImagenDistancia == null) { colorImagenDistancia = new byte[framesDistancia.PixelDataLength * 4]; } framesDistancia.CopyPixelDataTo(datosDistancia); } using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { colorFrame.CopyPixelDataTo(this.colorPixels); colorReceived = true; System.Drawing.Bitmap bmp = EmguCVHelper.ImageToBitmap(colorFrame); Image <Hsv, Byte> currentFrameHSV = new Image <Hsv, byte>(bmp); Image <Gray, Byte> grayFrame = currentFrameHSV.Convert <Gray, Byte>(); Image <Gray, Byte> imageHSVDest = currentFrameHSV.InRange(lowerLimit, upperLimit); imageHSVDest.Erode(100); VectorOfVectorOfPoint vectorOfPoint = EmguCVHelper.FindContours(imageHSVDest); for (int i = 0; i < vectorOfPoint.Size; i++) { var contour = vectorOfPoint[i]; var area = CvInvoke.ContourArea(contour); if (area > 100) { System.Drawing.Rectangle rec = CvInvoke.BoundingRectangle(contour); Point p1 = new Point(rec.X, rec.Y); Point p2 = new Point(rec.X + rec.Width, rec.Y + rec.Height); ObjetoX = (p1.X + p2.X) / 2; ObjetoY = (p1.Y + p2.Y) / 2; if (true == depthReceived) { this.sensor.CoordinateMapper.MapDepthFrameToColorFrame( DepthFormat, this.depthPixels, ColorFormat, this.colorCoordinates); int depthIndex = (int)ObjetoX + ((int)ObjetoY * this.depthWidth); DepthImagePixel depthPixel = this.depthPixels[depthIndex]; ObjetoZ = datosDistancia[depthIndex] >> 3; int X = (int)ObjetoX / this.colorToDepthDivisor; int Y = (int)ObjetoY / this.colorToDepthDivisor; } if (ObjetoZ > 0) { skelObjeto = DistanceHelper.ObtenerSkelPoint((int)ObjetoX, (int)ObjetoY, ObjetoZ, this.sensor); flagObjeto = true; } } } colorFrame.CopyPixelDataTo(this.colorPixels); // Write the pixel data into our bitmap this.colorBitmap.WritePixels( new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight), this.colorPixels, this.colorBitmap.PixelWidth * sizeof(int), 0); } } using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletons); } } using (DrawingContext dc = this.drawingGroup.Open()) { // Draw a transparent background to set the render size //dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight)); dc.DrawImage(this.colorBitmap, new Rect(0.0, 0.0, RenderWidth, RenderHeight)); if (skeletons.Length != 0) { foreach (Skeleton skel in skeletons) { if (skel.TrackingState == SkeletonTrackingState.Tracked) { this.DrawBonesAndJoints(skel, dc); } //Toma de mediciones de mano, hombro y codo derecho: ManoDerecha = skel.Joints[JointType.HandRight]; //Joint munecaDer = skel.Joints[JointType.WristRight]; CodoDerecho = skel.Joints[JointType.ElbowRight]; HombroDerecho = skel.Joints[JointType.ShoulderRight]; //Dibujo un punto negro sobre el objeto detectado Point objeto = new Point(this.ObjetoX, this.ObjetoY); dc.DrawEllipse(Brushes.Black, new Pen(Brushes.Black, 5), objeto, 5, 5); if ((HombroDerecho.TrackingState == JointTrackingState.Tracked) && (ManoDerecha.TrackingState == JointTrackingState.Tracked) && (CodoDerecho.TrackingState == JointTrackingState.Tracked)) { if (flagObjeto && !flagSkeleton) { CalcularAngulosFinales(); } //Console.WriteLine($"Mano X Y Z {handRight.Position.X} {handRight.Position.Y} {handRight.Position.Z}"); //Console.WriteLine($"Objeto X Y Z {skelObjeto.X} {skelObjeto.Y} {skelObjeto.Z}"); if (DistanceHelper.ObtenerDistancia(ManoDerecha, skelObjeto) < 0.1) { //significa que se llegó al objeto, por lo que se cierra la ventana y se envían //los datos. resultado = true; this.Close(); } } } } // prevent drawing outside of our render area this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight)); } }
private void nui_AllFramesReady(object sender, AllFramesReadyEventArgs e) { //DepthStream과 SkeletonStream에 동시에 추가한 이벤트 핸들러 SkeletonFrame sf = e.OpenSkeletonFrame(); //현재 인식한 골격정보(스캘리톤)을 sf라는 변수에 저장 if (sf == null) { return; } //골격이 인식되지 못하면, 즉 사람이 인식되지 않으면 보여줄 상자, 이를 이은 선을 보여줄 필요가 없으므로 종료 Skeleton[] skeletonData = new Skeleton[sf.SkeletonArrayLength]; //스캘리톤(골격정보)를 저장하라 배열을 인식된 수만큼 크기 지정 sf.CopySkeletonDataTo(skeletonData); //현재 SkeletonFrame안에서 스캘리톤 데이터를 복사 using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame()) { //현재 DepthImageFrame 안에서 데이터 복사 if (depthImageFrame != null) { //데이터가 있다면 foreach (Skeleton sd in skeletonData) { //복사해온 스캘리톤 데이터를 하나씩 차근차근 반복한다 if (sd.TrackingState == SkeletonTrackingState.Tracked) { //인식된 사용자 인덱스 중 추적이 되는 Tracked에 해당하는 골격정보만 사용 //만들 조인트만큼(키넥트가 인식하는 것만큼> 20) 조인트 배열 선언 for (int j = 0; j < nMax; j++) { //조인트 생성(초기화 및 설정) joints[j] = sd.Joints[(JointType)j]; } //여기까지 찾아진 골격상태에서 각 골격의 정보를 얻어오기 위함 for (int j = 0; j < nMax; j++) { DepthImagePoint depthPoint; depthPoint = depthImageFrame.MapFromSkeletonPoint(joints[j].Position); points[j] = new Point((int)((user_img.Width * depthPoint.X / depthImageFrame.Width) + 340), (int)(user_img.Height * depthPoint.Y / depthImageFrame.Height)); } //여기까지 각 조인트정보로부터 좌표를 얻기 위함 for (int j = 0; j < nMax; j++) { m_rect[j].Visibility = Visibility.Visible; //아까 만들었던 사각형을 보이도록 함 Canvas.SetTop(m_rect[j], points[j].Y - (m_rect[j].Height / 2)); Canvas.SetLeft(m_rect[j], points[j].X - (m_rect[j].Width / 2)); //사각형 배치 } //여기까지 가져온 각 조인트의 정보를 화면에 표시하는 코드 //엉덩이 중앙부터 머리까지 연결되는 선을 긋는 코드 PointCollection pc0 = new PointCollection(4); //이을 관절 개수만큼 크기를 지정하고 위치를 저장할 변수 pc0.Add(points[(int)JointType.HipCenter]); //엉덩이 중간 pc0.Add(points[(int)JointType.Spine]); //등뼈 pc0.Add(points[(int)JointType.ShoulderCenter]); //어깨 중간 pc0.Add(points[(int)JointType.Head]); //머리 m_poly[0].Points = pc0; //관절들 위치를 저장한 변수를 선으로 그리는 함수로 선언한 배열에 넣는다 m_poly[0].Visibility = Visibility.Visible; //넣었으니 활성화시켜 위치를 토대로 그린 선을 보여준다 //왼쪽 손부터 어깨까지 연결되는 선을 긋는 코드 PointCollection pc1 = new PointCollection(5); pc1.Add(points[(int)JointType.ShoulderCenter]); //어깨 중간 pc1.Add(points[(int)JointType.ShoulderLeft]); //왼쪽 어깨 pc1.Add(points[(int)JointType.ElbowLeft]); //왼쪽 팔꿈치 pc1.Add(points[(int)JointType.WristLeft]); //왼쪽 손목 pc1.Add(points[(int)JointType.HandLeft]); //왼손 m_poly[1].Points = pc1; m_poly[1].Visibility = Visibility.Visible; //오른쪽 손부터 어깨까지 연결되는 선을 긋는 코드 PointCollection pc2 = new PointCollection(5); pc2.Add(points[(int)JointType.ShoulderCenter]); //어깨 중간 pc2.Add(points[(int)JointType.ShoulderRight]); //오른쪽 어깨 pc2.Add(points[(int)JointType.ElbowRight]); //오른쪽 팔꿈치 pc2.Add(points[(int)JointType.WristRight]); //오른쪽 손목 pc2.Add(points[(int)JointType.HandRight]); //오른손 m_poly[2].Points = pc2; m_poly[2].Visibility = Visibility.Visible; //왼발부터 엉덩이까지 연결되는 선을 긋는 코드 PointCollection pc3 = new PointCollection(5); pc3.Add(points[(int)JointType.HipCenter]); //엉덩이 중간 pc3.Add(points[(int)JointType.HipLeft]); //엉덩이 왼쪽 pc3.Add(points[(int)JointType.KneeLeft]); //왼쪽 무릎 pc3.Add(points[(int)JointType.AnkleLeft]); //왼쪽 발목 pc3.Add(points[(int)JointType.FootLeft]); //왼쪽 발 m_poly[3].Points = pc3; m_poly[3].Visibility = Visibility.Visible; //오른발부터 엉덩이까지 연결되는 선을 긋는 코드 PointCollection pc4 = new PointCollection(5); pc4.Add(points[(int)JointType.HipCenter]); //엉덩이 중간 pc4.Add(points[(int)JointType.HipRight]); //엉덩이 오른쪽 pc4.Add(points[(int)JointType.KneeRight]); //오른쪽 무릎 pc4.Add(points[(int)JointType.AnkleRight]); //오른쪽 발목 pc4.Add(points[(int)JointType.FootRight]); //오른쪽 발 m_poly[4].Points = pc4; m_poly[4].Visibility = Visibility.Visible; Thread check = new Thread(new ThreadStart(step)); check.Start(); } } } } }
private void KinectAllFramesReady(object sender, AllFramesReadyEventArgs e) { // Have we already been "shut down" by the user of this viewer, // or has the SkeletonStream been disabled since this event was posted? if ((this.Kinect == null) || !((KinectSensor)sender).SkeletonStream.IsEnabled) { return; } bool haveSkeletonData = false; using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { if (this.skeletonCanvases == null) { this.CreateListOfSkeletonCanvases(); } if ((this.skeletonData == null) || (this.skeletonData.Length != skeletonFrame.SkeletonArrayLength)) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } skeletonFrame.CopySkeletonDataTo(this.skeletonData); haveSkeletonData = true; } } if (haveSkeletonData) { using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame()) { if (depthImageFrame != null) { int trackedSkeletons = 0; foreach (Skeleton skeleton in this.skeletonData) { Dictionary <JointType, JointMapping> jointMapping = this.jointMappings[trackedSkeletons]; jointMapping.Clear(); KinectSkeleton skeletonCanvas = this.skeletonCanvases[trackedSkeletons++]; skeletonCanvas.ShowBones = this.ShowBones; skeletonCanvas.ShowJoints = this.ShowJoints; skeletonCanvas.ShowCenter = this.ShowCenter; // Transform the data into the correct space // For each joint, we determine the exact X/Y coordinates for the target view foreach (Joint joint in skeleton.Joints) { Console.WriteLine("Here they are: " + joint.Position.X + "" + joint.Position.Y + "" + "" + joint.Position.Z); Point mappedPoint = this.GetPosition2DLocation(depthImageFrame, joint.Position); jointMapping[joint.JointType] = new JointMapping { Joint = joint, MappedPoint = mappedPoint }; } // Look up the center point Point centerPoint = this.GetPosition2DLocation(depthImageFrame, skeleton.Position); // Scale the skeleton thickness // 1.0 is the desired size at 640 width double scale = this.RenderSize.Width / 640; skeletonCanvas.RefreshSkeleton(skeleton, jointMapping, centerPoint, scale); } if (ImageType == ImageType.Depth) { this.ChooseTrackedSkeletons(this.skeletonData); } } } } }
void KinectFaceNode_AllFrameReady(object sender, AllFramesReadyEventArgs e) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; colorImageFrame = e.OpenColorImageFrame(); depthImageFrame = e.OpenDepthImageFrame(); skeletonFrame = e.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } return; } if (first) { first = false; this.olddepth = depthImageFrame.Format; } else { if (this.olddepth != depthImageFrame.Format) { //Need a reset if (this.depthImage != null) { this.depthImage = null; } foreach (SkeletonFaceTracker sft in this.trackedSkeletons.Values) { sft.Dispose(); } this.trackedSkeletons.Clear(); this.olddepth = depthImageFrame.Format; } } if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked || skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { // We want keep a record of any skeleton, tracked or untracked. if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId)) { this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker()); } // Give each tracker the upated frame. SkeletonFaceTracker skeletonFaceTracker; if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.runtime.Runtime, colorImageFrame.Format, colorImage, depthImageFrame.Format, depthImage, skeleton); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } } this.RemoveOldTrackers(skeletonFrame.FrameNumber); colorImageFrame.Dispose(); depthImageFrame.Dispose(); skeletonFrame.Dispose(); this.FInvalidate = true; }
void nui_AllFramesReady(object sender, AllFramesReadyEventArgs e) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = e.OpenColorImageFrame(); depthImageFrame = e.OpenDepthImageFrame(); skeletonFrame = e.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } if (this.depthImageFormat != depthImageFrame.Format) { this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; } if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } using (depthImageFrame) { if (depthImageFrame != null) { foreach (Skeleton skeleton in skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked || skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { /////////// 머리 정보를 받아온다 /////////////// Joint joint = skeleton.Joints[JointType.Head]; DepthImagePoint depthPoint; depthPoint = depthImageFrame.MapFromSkeletonPoint(joint.Position); System.Windows.Point point = new System.Windows.Point((int)(image1.ActualWidth * depthPoint.X / depthImageFrame.Width), (int)(image1.ActualHeight * depthPoint.Y / depthImageFrame.Height)); textBlock1.Text = string.Format("X:{0:0.00} Y:{1:0.00} Z:{2:0.00}", point.X, point.Y, joint.Position.Z); // 이전 헤드의 위치를 저장한다. m_prevHeadX = m_headX; m_prevHeadY = m_headY; m_headX = point.X; m_headY = point.Y; if (Math.Abs(m_prevHeadX - point.X) < 10) { m_headX = m_prevHeadX; } if (Math.Abs(m_prevHeadY - point.Y) < 10) { m_headY = m_prevHeadY; } Canvas.SetLeft(ellipse1, point.X - ellipse1.Width / 2); Canvas.SetTop(ellipse1, point.Y - ellipse1.Height / 2); ////////////// face 정보를 받아온다////////////////////// if (this.faceTracker == null) { try { this.faceTracker = new FaceTracker(nui1); } catch (InvalidOperationException) { // During some shutdown scenarios the FaceTracker // is unable to be instantiated. Catch that exception // and don't track a face. this.faceTracker = null; } } if (this.faceTracker != null) { FaceTrackFrame frame = this.faceTracker.Track( colorImageFormat, colorImage, depthImageFormat, depthImage, skeleton); if (frame.TrackSuccessful) { facePoints = frame.GetProjected3DShape(); textBlock2.Text = string.Format("noseX:{0:0.00} noseY:{1:0.00} ", facePoints[107].X, facePoints[107].Y); m_noseX = facePoints[107].X; m_noseY = facePoints[107].Y; Canvas.SetLeft(ellipse2, facePoints[107].X - ellipse2.Width / 2); Canvas.SetTop(ellipse2, facePoints[107].Y - ellipse2.Width / 2); } } ///////////////고개의 각도를 계산 //////////////////// lineOfSight.X1 = m_headX; lineOfSight.Y1 = m_headY; lineOfSight.X2 = m_noseX; lineOfSight.Y2 = m_noseY; Canvas.SetLeft(m_sightRect, m_headX - m_sightRect.Width / 2); Canvas.SetTop(m_sightRect, m_headY); CheckWhichSight(depthImageFrame, m_noseX, m_noseY); } } } } }
private void OnAllFrameReady(object sender, AllFramesReadyEventArgs e) { if (this.isShutDown) { return; } #region Skeleton Data Process Region // 1. Process Skeleton Data // Grab Skeleton Frame using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null && this.skeletons != null) { // Copy Skeleton Datas skeletonFrame.CopySkeletonDataTo(skeletons); } } #endregion #region Depth Data Process Region // 2. Process Depth Data // Grab Depth Frame short depthValue; using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) { if (depthFrame != null) { // Copy Depth Pixel Data depthFrame.CopyDepthImagePixelDataTo(this.depthPixels); // Convert Depth to RGB for (int i = 0; i < this.depthPixels.Length; i++) { // Get Depth Value For This Pixel depthValue = depthPixels[i].Depth; unsafe { if (depthValue >= this.maxDepth || depthValue <= this.minDepth) { this.depthImage.ImageDataPtr[i] = (byte)0; } else { this.depthImage.ImageDataPtr[i] = this.depthLookupTable[depthValue]; } } } if (this.leftHand.TrackingState == JointTrackingState.Tracked) { DepthImagePoint ldHand = this.sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(this.leftHand.Position, DepthImageFormat.Resolution640x480Fps30); int topleft_x = ldHand.X - Configuration.HAND_REGION_WIDTH / 2; int topleft_y = ldHand.Y - Configuration.HAND_REGION_HEIGHT / 2; if (topleft_x < 0) { topleft_x = 0; } if (topleft_x + Configuration.HAND_REGION_WIDTH >= this.sensor.ColorStream.FrameWidth) { topleft_x = this.sensor.ColorStream.FrameWidth - Configuration.HAND_REGION_WIDTH; } if (topleft_y < 0) { topleft_y = 0; } if (topleft_y + Configuration.HAND_REGION_HEIGHT >= this.sensor.ColorStream.FrameHeight) { topleft_y = this.sensor.ColorStream.FrameHeight - Configuration.HAND_REGION_HEIGHT; } CvRect ldHandRect = new CvRect(topleft_x, topleft_y, Configuration.HAND_REGION_WIDTH, Configuration.HAND_REGION_HEIGHT); //Cv.Rectangle( this.depthImage, ldHandRect, new CvScalar( 0, 0, 255 ), 5 ); // Used for Visualization Cv.SetImageROI(this.depthImage, ldHandRect); Cv.Copy(this.depthImage, this.leftHandDepthImage); Cv.ResetImageROI(this.depthImage); // Filter Hand Depth Image Cv.Smooth(this.leftHandDepthImage, this.leftHandDepthImage, SmoothType.Median); } if (this.rightHand.TrackingState == JointTrackingState.Tracked) { DepthImagePoint rdHand = this.sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(this.rightHand.Position, DepthImageFormat.Resolution640x480Fps30); int topleft_x = rdHand.X - Configuration.HAND_REGION_WIDTH / 2; int topleft_y = rdHand.Y - Configuration.HAND_REGION_HEIGHT / 2; if (topleft_x < 0) { topleft_x = 0; } if (topleft_x + Configuration.HAND_REGION_WIDTH >= this.sensor.ColorStream.FrameWidth) { topleft_x = this.sensor.ColorStream.FrameWidth - Configuration.HAND_REGION_WIDTH; } if (topleft_y < 0) { topleft_y = 0; } if (topleft_y + Configuration.HAND_REGION_HEIGHT >= this.sensor.ColorStream.FrameHeight) { topleft_y = this.sensor.ColorStream.FrameHeight - Configuration.HAND_REGION_HEIGHT; } CvRect rdHandRect = new CvRect(topleft_x, topleft_y, Configuration.HAND_REGION_WIDTH, Configuration.HAND_REGION_HEIGHT); //Cv.Rectangle( this.depthImage, rdHandRect, new CvScalar( 0, 0, 255 ), 5 ); // Used for Visualization Cv.SetImageROI(this.depthImage, rdHandRect); Cv.Copy(this.depthImage, this.rightHandDepthImage); Cv.ResetImageROI(this.depthImage); // Filter Hand Depth Image Cv.Smooth(this.rightHandDepthImage, this.rightHandDepthImage, SmoothType.Median); } } } #endregion #region Color Data Process Region // 3. Process Color Data // Grab Color Frame using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { // Copy Pixel Data unsafe { colorFrame.CopyPixelDataTo(this.colorImage.ImageData, colorFrame.PixelDataLength); } // Draw Skeleton Position if (skeletons != null) { // Find Appropriate Skeleton Skeleton targetSkeleton = null; for (int i = 0; i < skeletons.Length; i++) { // Skip Invalid State if (skeletons[i] == null) { continue; } // Only Fully Tracked Skeleton if (skeletons[i].TrackingState == SkeletonTrackingState.Tracked) { // Set Target Skeleton - If exists Set to nearest. if (targetSkeleton == null) { targetSkeleton = skeletons[i]; } else if (targetSkeleton.Position.Z > skeletons[i].Position.Z) { targetSkeleton = skeletons[i]; } } } if (targetSkeleton != null) { // Left Hand Position this.leftHand = targetSkeleton.Joints[JointType.HandLeft]; // Check Tracked Status if (this.leftHand.TrackingState == JointTrackingState.Tracked) { this.leftHandFound = true; leftHandPoint = this.sensor.CoordinateMapper.MapSkeletonPointToColorPoint(leftHand.Position, ColorImageFormat.RgbResolution640x480Fps30); this.leftHandPosition.X = leftHandPoint.X; this.leftHandPosition.Y = leftHandPoint.Y; int topleft_x = leftHandPoint.X - Configuration.HAND_REGION_WIDTH / 2; int topleft_y = leftHandPoint.Y - Configuration.HAND_REGION_HEIGHT / 2; // Bound Check if (topleft_x < 0) { topleft_x = 0; } if (topleft_x + Configuration.HAND_REGION_WIDTH >= this.sensor.ColorStream.FrameWidth) { topleft_x = this.sensor.ColorStream.FrameWidth - Configuration.HAND_REGION_WIDTH; } if (topleft_y < 0) { topleft_y = 0; } if (topleft_y + Configuration.HAND_REGION_HEIGHT >= this.sensor.ColorStream.FrameHeight) { topleft_y = this.sensor.ColorStream.FrameHeight - Configuration.HAND_REGION_HEIGHT; } // Set Hand Position leftHandRect = new CvRect(topleft_x, topleft_y, Configuration.HAND_REGION_WIDTH, Configuration.HAND_REGION_HEIGHT); Cv.Rectangle(this.colorImage, this.leftHandRect, new CvScalar(0, 0, 255), 1); // Used for Visualization Cv.SetImageROI(this.colorImage, this.leftHandRect); // Copy Data Cv.Copy(this.colorImage, this.leftHandImage); // Reset ROI Cv.ResetImageROI(this.colorImage); // Smooth Color Hand Image Cv.Smooth(this.leftHandImage, this.leftHandImage, SmoothType.Median); // Only Hand Region CropNear(this.leftHandDepthImage); // Filter With Depth Image FilterFarObjects(this.leftHandImage, this.leftHandDepthImage); // Detect By Skin Color Model this.skinDetector.FilterSkinColorRegion(this.leftHandImage, this.leftHandSkinImage, 0.25f); // Smooth Color Hand Skin Image //Cv.Smooth( this.leftHandSkinImage, this.leftHandSkinImage, SmoothType.Median ); Cv.Smooth(this.leftHandSkinImage, this.leftHandSkinImage, SmoothType.Median, 5); Cv.Erode(this.leftHandSkinImage, this.leftHandSkinImage); Cv.Dilate(this.leftHandSkinImage, this.leftHandSkinImage); // Find Object Cv.Sub(this.leftHandDepthImage, this.leftHandSkinImage, this.leftObjectRemainedImage); Cv.Erode(this.leftObjectRemainedImage, this.leftObjectRemainedImage); Cv.Smooth(this.leftObjectRemainedImage, this.leftObjectRemainedImage, SmoothType.Median); // Filter Objects Only FilterFarObjects(this.leftHandImage, this.leftObjectRemainedImage); Cv.CvtColor(this.leftHandImage, this.leftObjectHSVImage, ColorConversion.BgraToBgr); Cv.CvtColor(this.leftObjectHSVImage, this.leftObjectHSVImage, ColorConversion.BgrToHsv); Cv.Split(this.leftObjectHSVImage, this.leftObjectHImage, this.leftObjectSImage, this.leftObjectVImage, null); } else { this.leftHandFound = false; } // Right Hand Position this.rightHand = targetSkeleton.Joints[JointType.HandRight]; if (this.rightHand.TrackingState == JointTrackingState.Tracked) { this.rightHandFound = true; rightHandPoint = this.sensor.CoordinateMapper.MapSkeletonPointToColorPoint(rightHand.Position, ColorImageFormat.RgbResolution640x480Fps30); this.rightHandPosition.X = rightHandPoint.X; this.rightHandPosition.Y = rightHandPoint.Y; int topleft_x = rightHandPoint.X - Configuration.HAND_REGION_WIDTH / 2; int topleft_y = rightHandPoint.Y - Configuration.HAND_REGION_HEIGHT / 2; // Bound Check if (topleft_x < 0) { topleft_x = 0; } if (topleft_x + Configuration.HAND_REGION_WIDTH >= this.sensor.ColorStream.FrameWidth) { topleft_x = this.sensor.ColorStream.FrameWidth - Configuration.HAND_REGION_WIDTH; } if (topleft_y < 0) { topleft_y = 0; } if (topleft_y + Configuration.HAND_REGION_HEIGHT >= this.sensor.ColorStream.FrameHeight) { topleft_y = this.sensor.ColorStream.FrameHeight - Configuration.HAND_REGION_HEIGHT; } // Set Hand Position rightHandRect = new CvRect(topleft_x, topleft_y, Configuration.HAND_REGION_WIDTH, Configuration.HAND_REGION_HEIGHT); Cv.Rectangle(this.colorImage, this.rightHandRect, new CvScalar(0, 0, 255), 1); // Used for Visualization Cv.SetImageROI(this.colorImage, this.rightHandRect); // Copy Data Cv.Copy(this.colorImage, this.rightHandImage); // Reset ROI Cv.ResetImageROI(this.colorImage); // Smooth Color Hand Image Cv.Smooth(this.rightHandImage, this.rightHandImage, SmoothType.Median); CropNear(this.rightHandDepthImage); // Filter With Depth Image FilterFarObjects(this.rightHandImage, this.rightHandDepthImage); // Detect By Skin Color Model this.skinDetector.FilterSkinColorRegion(this.rightHandImage, this.rightHandSkinImage, 0.25f); // Smooth Color Hand Skin Image //Cv.Smooth( this.rightHandSkinImage, this.rightHandSkinImage, SmoothType.Median ); Cv.Smooth(this.rightHandSkinImage, this.rightHandSkinImage, SmoothType.Median, 5); Cv.Erode(this.rightHandSkinImage, this.rightHandSkinImage); Cv.Dilate(this.rightHandSkinImage, this.rightHandSkinImage); // Find Object Cv.Sub(this.rightHandDepthImage, this.rightHandSkinImage, this.rightObjectRemainedImage); Cv.Erode(this.rightObjectRemainedImage, this.rightObjectRemainedImage); Cv.Smooth(this.rightObjectRemainedImage, this.rightObjectRemainedImage, SmoothType.Median); // Filter Objects Only FilterFarObjects(this.rightHandImage, this.rightObjectRemainedImage); Cv.CvtColor(this.rightHandImage, this.rightObjectHSVImage, ColorConversion.BgraToBgr); Cv.CvtColor(this.rightObjectHSVImage, this.rightObjectHSVImage, ColorConversion.BgrToHsv); Cv.Split(this.rightObjectHSVImage, this.rightObjectHImage, this.rightObjectSImage, this.rightObjectVImage, null); } else { this.rightHandFound = false; } } } #endregion #region Image Display Region // Show Depth Image Cv.ShowImage("Depth Image", this.depthImage); Cv.ShowImage("Left Hand Depth Image", this.leftHandDepthImage); Cv.ShowImage("Right Hand Depth Image", this.rightHandDepthImage); // Show Color Image Cv.ShowImage("Color Image", this.colorImage); Cv.ShowImage("Left Hand Image", this.leftHandImage); Cv.ShowImage("Right Hand Image", this.rightHandImage); Cv.ShowImage("Left Hand Skin Image", this.leftHandSkinImage); Cv.ShowImage("Right Hand Skin Image", this.rightHandSkinImage); // Show Object Only Image Cv.ShowImage("Left Hand Object Image", this.leftObjectRemainedImage); Cv.ShowImage("Right Hand Object Image", this.rightObjectRemainedImage); // Show Object HSV Image Cv.ShowImage("Left Hand H Image", this.leftObjectHImage); Cv.ShowImage("Left Hand S Image", this.leftObjectSImage); Cv.ShowImage("Left Hand V Image", this.leftObjectVImage); Cv.ShowImage("Right Hand H Image", this.rightObjectHImage); Cv.ShowImage("Right Hand S Image", this.rightObjectSImage); Cv.ShowImage("Right Hand V Image", this.rightObjectVImage); #endregion } } }
private void AllFrameReady(object sender, AllFramesReadyEventArgs e) { //Console.Clear(); using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(this.skeletons); Skeleton skel = skeletons[0]; if (skel.TrackingState == SkeletonTrackingState.Tracked) { SkeletonPoint rightHand = skeletons[0].Joints[JointType.HandRight].Position; SkeletonPoint head = skeletons[0].Joints[JointType.Head].Position; rightHandPosition = SkeletonPointToScreen(rightHand); headPosition = SkeletonPointToScreen(head); } } } using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { // Copy the pixel data from the image to a temporary array colorFrame.CopyPixelDataTo(this.colorPixels); // Write the pixel data into our bitmap this.ColorWriteBitmap.WritePixels( new System.Windows.Int32Rect(0, 0, this.ColorWriteBitmap.PixelWidth, this.ColorWriteBitmap.PixelHeight), this.colorPixels, this.ColorWriteBitmap.PixelWidth * sizeof(int), 0); } } using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) { if (depthFrame != null) { var sw = Stopwatch.StartNew(); // Copy the pixel data from the image to a temporary array depthFrame.CopyDepthImagePixelDataTo(this.depthPixels); // short[] depthData = new short[depthFrame.PixelDataLength]; // Get the min and max reliable depth for the current frame int minDepth = depthFrame.MinDepth; int maxDepth = depthFrame.MaxDepth; int width = depthFrame.Width; int height = depthFrame.Height; if (headPosition.X == 0) { headDepth = 100; } else { headDepth = depthPixels[headPosition.X + headPosition.Y * 640].Depth; } sw.Restart(); //*********** Convert cull and transform***************** colorizer.TransformCullAndConvertDepthFrame( depthPixels, minDepth, maxDepth, colorPixels, AngleRotateTan, (short)(headDepth - (short)CullingThresh), headPosition); Image <Bgra, byte> depthImg; //Console.WriteLine("iteration:" + sw.ElapsedMilliseconds); sw.Restart(); Image <Gray, Byte> rightFront = null; Image <Gray, Byte> leftFront = null; depthImg = ImageConverter.Array2Image(colorPixels, width, height, width * 4); PointF rightVector = PointF.Empty; PointF leftVector = PointF.Empty; bool isSkip = false; bool leftHandRaise = false; if (skeletons != null && skeletons[0].TrackingState == SkeletonTrackingState.Tracked) { PointF hr = SkeletonPointToScreen(skeletons[0].Joints[JointType.HandRight].Position); PointF hl = SkeletonPointToScreen(skeletons[0].Joints[JointType.HandLeft].Position); PointF er = SkeletonPointToScreen(skeletons[0].Joints[JointType.ElbowRight].Position); PointF el = SkeletonPointToScreen(skeletons[0].Joints[JointType.ElbowLeft].Position); PointF hip = SkeletonPointToScreen(skeletons[0].Joints[JointType.HipCenter].Position); // hand is lower than hip //Console.WriteLine(skeletons[0].Joints[JointType.HandRight].Position.Y); //Console.WriteLine(skeletons[0].Joints[JointType.HipCenter].Position.Y); //Console.WriteLine("-------------"); if (skeletons[0].Joints[JointType.HandRight].Position.Y < skeletons[0].Joints[JointType.HipCenter].Position.Y + 0.05) { isSkip = true; } if (skeletons[0].Joints[JointType.HandLeft].Position.Y > skeletons[0].Joints[JointType.HipCenter].Position.Y) { leftHandRaise = true; } rightVector.X = (hr.X - er.X); rightVector.Y = (hr.Y - er.Y); leftVector.X = (hl.X - el.X); leftVector.Y = (hl.Y - el.Y); } HandShapeModel handModel = null; if (!isSkip) { //handModel = m_OpenCVController.FindHandPart(ref depthImg, out rightFront, out leftFront, headDepth - (int)CullingThresh, rightVector, leftVector,leftHandRaise); } // no hands detected if (handModel == null) { handModel = new HandShapeModel(0, HandEnum.None); } //sw.Restart(); // database processing DBManager db = DBManager.GetSingleton(); if (db != null) { if (skeletons != null) { handModel.SetSkeletonData(skeletons[0]); } db.AddFrameData(handModel); } // not recording show prob else { //Image<Bgr, byte>[] result = HandShapeClassifier.GetSingleton() //.RecognizeGesture(handModel.hogRight, 3); ////Console.WriteLine(sw.ElapsedMilliseconds); //if (result != null) //{ // ImageConverter.UpdateWriteBMP(WrtBMP_Candidate1, result[0].Convert<Gray, byte>().ToBitmap()); // ImageConverter.UpdateWriteBMP(WrtBMP_Candidate2, result[1].Convert<Gray, byte>().ToBitmap()); // ImageConverter.UpdateWriteBMP(WrtBMP_Candidate3, result[2].Convert<Gray, byte>().ToBitmap()); //} } string currentSign = db == null ? "0" : db.CurrentSign.ToString(); string path = @"J:\Kinect data\Aaron 141-180\hands\" + currentSign + " " + handModel.frame.ToString(); // UI update if (rightFront != null) { Bitmap right = rightFront.ToBitmap(); //right.Save(path + " r.jpg"); ImageConverter.UpdateWriteBMP(WrtBMP_RightHandFront, right); } if (leftFront != null) { Bitmap left = leftFront.ToBitmap(); //left.Save(path + " l.jpg"); ImageConverter.UpdateWriteBMP(WrtBMP_LeftHandFront, left); } if (sw.ElapsedMilliseconds > 15) { Console.WriteLine("Find hand:" + sw.ElapsedMilliseconds); } sw.Restart(); //**************************draw gray histogram //Bitmap histo = m_OpenCVController.Histogram(depthImg); //ImageConverter.UpdateWriteBMP(GrayWriteBitmap, histo); // draw hand position from kinect // DrawHandPosition(depthBMP, rightHandPosition, System.Drawing.Brushes.Purple); //*******************upadte UI ImageConverter.UpdateWriteBMP(DepthWriteBitmap, depthImg.ToBitmap()); // Console.WriteLine("Update UI:" + sw.ElapsedMilliseconds); } } }
private void AllFramesReady(object sender, AllFramesReadyEventArgs e) { using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (colorFrame == null || depthFrame == null || skeletonFrame == null) { ErrorWhileRecivingData(skeletonFrame, depthFrame, colorFrame); return; } Skeleton[] skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletons); short[,] depths = new short[depthFrame.Width, depthFrame.Height]; short[] fromDepthFrame = new short[depthFrame.Width * depthFrame.Height]; depthFrame.CopyPixelDataTo(fromDepthFrame); DebuggingTable.LatestCreated["size"] = fromDepthFrame.Length.ToString(); for (int i = 0; i < fromDepthFrame.Length; i++) { depths[i / depthFrame.Height, i % depthFrame.Height] = fromDepthFrame[i]; } byte[] colorPixels = new byte[colorFrame.PixelDataLength]; colorFrame.CopyPixelDataTo(colorPixels); /* * //background removal * if (BackgroundRemoval) * { * Skeleton skeleton = skeletons.FirstOrDefault(); * int maxDepth = -1, minDepth = -1; * if (skeleton != default(Skeleton)) * { * short max = skeleton.Joints.Max( * joint => depths[(int)(depthFrame.Width * joint.Position.X), * (int)(depthFrame.Height * joint.Position.Y)]); * short min = skeleton.Joints.Max( * joint => depths[(int)(depthFrame.Width * joint.Position.X), * (int)(depthFrame.Height * joint.Position.Y)]); * int diff = max - min; * maxDepth = max + diff; * minDepth = min - diff; * } * for (int x = 0; x < colorFrame.Width; x++) * { * for (int y = 0; y < colorFrame.Height; y++) * { * bool isProbablyPerson; * if (skeleton == default(Skeleton)) * { * isProbablyPerson = false; * } * else * { * short depth = depths[x, y]; * isProbablyPerson = depth > minDepth && depth < maxDepth; * } * colorPixels[(y * colorFrame.Width + x) * 4] = (byte)(isProbablyPerson ? 255 : 0); * } * }*/ DataRecieved(skeletons, depths, colorPixels); } }
private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } if (this.depthImageFormat != depthImageFrame.Format) { this.ResetFaceTracking(); this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.ResetFaceTracking(); this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; } if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } // información del esqueleto if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked || skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId)) { this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker()); } SkeletonFaceTracker skeletonFaceTracker; if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.Kinect, colorImageFormat, colorImage, depthImageFormat, depthImage, skeleton); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } } this.RemoveOldTrackers(skeletonFrame.FrameNumber); this.InvalidateVisual(); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
void SensorAllFramesReady(object sender, AllFramesReadyEventArgs e) { Skeleton[] skeletons = new Skeleton[0]; using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletons); } } using (DrawingContext dc = this.drawingGroup.Open()) { // Draw a transparent background to set the render size dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight)); if (skeletons.Length != 0) { foreach (Skeleton skel in skeletons) { RenderClippedEdges(skel, dc); if (skel.TrackingState == SkeletonTrackingState.Tracked) { this.DrawBonesAndJoints(skel, dc); SetDepthPoints(skel, e); if (m_gestureController != null) { m_gestureController.UpdateAllGestures(skel, m_headDepthPoint, m_leftHandDepthPoint, m_rightHandDepthPoint); } break; } else if (skel.TrackingState == SkeletonTrackingState.PositionOnly) { dc.DrawEllipse( this.centerPointBrush, null, this.SkeletonPointToScreen(skel.Position), BodyCenterThickness, BodyCenterThickness); } } } // prevent drawing outside of our render area this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight)); } //Get raw image using (DepthImageFrame depth = e.OpenDepthImageFrame()) { if (depth != null) { //Create array for pixel data and copy it from the image frame short[] pixelData = new short[depth.PixelDataLength]; depth.CopyPixelDataTo(pixelData); byte[] convertedPixels = ConvertDepthFrame(pixelData, ((KinectSensor)sender).DepthStream, 640 * 480 * 4); m_colorImageData.ColorImage = convertedPixels; m_colorImageData.Height = depth.Height; m_colorImageData.Width = depth.Width; } } }
///<summary> ///RGBカメラ、距離カメラ、骨格のフレーム更新イベント ///</summary> ///<param name="sender"></param> ///<param name="e"></param> void kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { try { //Kinectのインスタンスを取得する KinectSensor kinect = sender as KinectSensor; if (kinect == null) { return; } //RGBカメラのフレームデータを取得する using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { //RGBカメラのピクセルデータを取得する byte[] colorPixel = new byte[colorFrame.PixelDataLength]; colorFrame.CopyPixelDataTo(colorPixel); //ピクセルデータをビットマップに変換する Bitmap bitmap = new Bitmap(kinect.ColorStream.FrameWidth, kinect.ColorStream.FrameHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb); Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); BitmapData data = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); Marshal.Copy(colorPixel, 0, data.Scan0, colorPixel.Length); bitmap.UnlockBits(data); pictureBoxRgb.Image = bitmap; } } //スケルトンのフレームを取得する using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { const int R = 5; Graphics g = Graphics.FromImage(pictureBoxRgb.Image); skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletons); //トラッキングされているスケルトンのジョイントを描画する foreach (var skeleton in skeletons) { //スケルトンがトラッキングされてなければ次へ if (skeleton.TrackingState != SkeletonTrackingState.Tracked) { continue; } //ジョイントを描画する foreach (Joint joint in skeleton.Joints) { //ジョイントがトラッキングされていなければ次へ if (joint.TrackingState != JointTrackingState.Tracked) { continue; } //スケルトンの座標を、RGBカメラの座標に変換して円を書く ColorImagePoint point = kinect.MapSkeletonPointToColor(joint.Position, kinect.ColorStream.Format); g.DrawEllipse(new Pen(Brushes.Red), new Rectangle(point.X - R, point.Y - R, R * 2, R * 2)); } } } //////////////////////////////////////////メソッド Judge(kinect, skeletonFrame); ////////////////////////////////////////////// } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
void _sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) { using (SkeletonFrame frame = e.OpenSkeletonFrame()) { WriteImageSkeleton(frame); } using (ColorImageFrame frame = e.OpenColorImageFrame()) // using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) { if (frame != null) { byte[] pixelData = new byte[frame.PixelDataLength]; frame.CopyPixelDataTo(pixelData); int stride = frame.Width * 4; BitmapSource source = BitmapSource.Create(frame.Width, frame.Height, 96.0, 96.0, PixelFormats.Bgr32, null, pixelData, stride); this.image1.Source = source; if (this._movieRecordingOn) { System.Drawing.Bitmap firstFrame = new System.Drawing.Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb); System.Drawing.Imaging.BitmapData bitmapdata = firstFrame.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, firstFrame.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); source.CopyPixels(Int32Rect.Empty, bitmapdata.Scan0, bitmapdata.Height * bitmapdata.Stride, bitmapdata.Stride); firstFrame.UnlockBits(bitmapdata); if (this._colorFrameCount == 0) { this._firstColorFrameTimestamp = frame.Timestamp; this._colorFrameCount++; } else { float colorFrameTime = ((float)(frame.Timestamp - this._firstColorFrameTimestamp)) / 1000f; //string filename = this.fileNameTextBox.Text + _colorFrameCount.ToString() + "_" + colorFrameTime.ToString("000.000") + ".png"; //firstFrame.Save(filename); _frameList.Add(new System.Drawing.Bitmap(firstFrame)); _frameTimes.Add(colorFrameTime); this._colorFrameCount++; } firstFrame.Dispose(); } } } }
private void KinectAllFramesReady(object sender, AllFramesReadyEventArgs e) { // Have we already been "shut down" by the user of this viewer, // or has the SkeletonStream been disabled since this event was posted? if ((this.Kinect == null) || !((KinectSensor)sender).SkeletonStream.IsEnabled) { return; } // File.AppendAllText(@"C:\Users\pk1601cs33\hell.txt", " 0 "); bool haveSkeletonData = false; using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { // File.AppendAllText(@"C:\Users\pk1601cs33\hell.txt", " 1 "); if (skeletonFrame != null) { if (this.skeletonCanvases == null) { // File.AppendAllText(@"C:\Users\pk1601cs33\hell.txt", " 2 "); this.CreateListOfSkeletonCanvases(); } if ((this.skeletonData == null) || (this.skeletonData.Length != skeletonFrame.SkeletonArrayLength)) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; // File.AppendAllText(@"C:\Users\pk1601cs33\hell.txt", " 3 "); } skeletonFrame.CopySkeletonDataTo(this.skeletonData); haveSkeletonData = true; } } if (haveSkeletonData) { using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame()) { if (depthImageFrame != null) { // File.AppendAllText(@"C:\Users\pk1601cs33\hell.txt", " 4 "); int trackedSkeletons = 0; foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked) { // File.AppendAllText(@"C:\Users\pk1601cs33\hell.txt","Tracked"); } else if (skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { // File.AppendAllText(@"C:\Users\pk1601cs33\hell.txt", trackedSkeletons.ToString() + " 10 "); } else if (skeleton.TrackingState == SkeletonTrackingState.NotTracked) { //File.AppendAllText(@"C:\Users\pk1601cs33\hell.txt", "Nottrack"); } Dictionary <JointType, JointMapping> jointMapping = this.jointMappings[trackedSkeletons]; jointMapping.Clear(); KinectSkeleton skeletonCanvas = this.skeletonCanvases[trackedSkeletons++]; skeletonCanvas.ShowBones = this.ShowBones; skeletonCanvas.ShowJoints = this.ShowJoints; skeletonCanvas.ShowCenter = this.ShowCenter; foreach (Joint joint in skeleton.Joints) { Point mappedPoint = this.GetPosition2DLocation(depthImageFrame, joint.Position); jointMapping[joint.JointType] = new JointMapping { Joint = joint, MappedPoint = mappedPoint }; Joint asd = joint; if (asd.TrackingState == JointTrackingState.Tracked) { Vector3D elbowl = new Vector3D(skeleton.Joints[JointType.ElbowLeft].Position.X, skeleton.Joints[JointType.ElbowLeft].Position.Y, skeleton.Joints[JointType.ElbowLeft].Position.Z); Vector3D WristLeft = new Vector3D(skeleton.Joints[JointType.WristLeft].Position.X, skeleton.Joints[JointType.WristLeft].Position.Y, skeleton.Joints[JointType.WristLeft].Position.Z); Vector3D ShoulderLeft = new Vector3D(skeleton.Joints[JointType.ShoulderLeft].Position.X, skeleton.Joints[JointType.ShoulderLeft].Position.Y, skeleton.Joints[JointType.ShoulderLeft].Position.Z); Vector3D Head = new Vector3D(skeleton.Joints[JointType.ShoulderCenter].Position.X, skeleton.Joints[JointType.ShoulderCenter].Position.Y, skeleton.Joints[JointType.ShoulderCenter].Position.Z); Vector3D Neck = new Vector3D(skeleton.Joints[JointType.Spine].Position.X, skeleton.Joints[JointType.Spine].Position.Y, skeleton.Joints[JointType.Spine].Position.Z); //for two hand Vector3D shoulderright = new Vector3D(skeleton.Joints[JointType.ShoulderRight].Position.X, skeleton.Joints[JointType.ShoulderRight].Position.Y, skeleton.Joints[JointType.ShoulderRight].Position.Z); Vector3D elbowright = new Vector3D(skeleton.Joints[JointType.ElbowRight].Position.X, skeleton.Joints[JointType.ElbowRight].Position.Y, skeleton.Joints[JointType.ElbowRight].Position.Z); //vector for high knees Vector3D ankleleft = new Vector3D(skeleton.Joints[JointType.AnkleLeft].Position.X, skeleton.Joints[JointType.AnkleLeft].Position.Y, skeleton.Joints[JointType.AnkleLeft].Position.Z); Vector3D kneeleft = new Vector3D(skeleton.Joints[JointType.KneeLeft].Position.X, skeleton.Joints[JointType.KneeLeft].Position.Y, skeleton.Joints[JointType.KneeLeft].Position.Z); Vector3D hipleft = new Vector3D(skeleton.Joints[JointType.HipLeft].Position.X, skeleton.Joints[JointType.HipLeft].Position.Y, skeleton.Joints[JointType.HipLeft].Position.Z); //Vector3D SpineShoulder = new Vector3D(skeleton.Joints[JointType.SpineShoulder].Position.X, skeleton.Joints[JointType.SpineShoulder].Position.Y, skeleton.Joints[JointType.SpineShoulder].Position.Z); LeftElbowAngle = AngleBetweenTwoVectors(elbowl - ShoulderLeft, elbowl - WristLeft); rotationAngle = AngleBetweenTwoVectors(elbowl - ShoulderLeft, Neck - Head); rottwohand = AngleBetweenTwoVectors(kneeleft - ankleleft, kneeleft - hipleft); footrot = AngleBetweenTwoVectors(ShoulderLeft - elbowl, shoulderright - elbowright); if (footrot >= 0 && footrot <= 100) { if (tempcount4 == 0) { countnumber4++; } tempcount4 = 1; } else { if (tempcount4 == 1) { // countnumber3++; } tempcount4 = 0; } if (rottwohand >= 0 && rottwohand <= 120) { if (tempcount3 == 0) { countnumber3++; } tempcount3 = 1; } else { if (tempcount3 == 1) { // countnumber3++; } tempcount3 = 0; } if (LeftElbowAngle >= 0 && LeftElbowAngle <= 90) { if (tempcount1 == 0) { countnumber1++; } tempcount1 = 1; } else { if (tempcount1 == 1) { //countnumber1++; } tempcount1 = 0; } if (rotationAngle >= 0 && rotationAngle <= 90) { if (tempcount2 == 0) { countnumber2++; } tempcount2 = 1; } else { if (tempcount2 == 1) { countnumber2++; } tempcount2 = 0; } // double angle=AngleBetweenTwoVectors(ElbowLeft, WristLeft); // Console.WriteLine("angle1 = " + LeftElbowAngle.ToString() + "count1 = " + countnumber1.ToString() + "result1= " + tempcount1.ToString()); // Console.WriteLine("angle2 = " + rotationAngle.ToString() + "count2 = " + countnumber2.ToString() + "result2= " + tempcount2.ToString()); } } // Console.WriteLine("angle1 = " + LeftElbowAngle.ToString() + "ElbowRotation= " + countnumber1.ToString()); // Console.WriteLine("angle2 = " + rotationAngle.ToString() + "LefthandRotation = " + countnumber2.ToString()); //Console.WriteLine("angle3 = " + rottwohand.ToString() + "HighKnees = " + countnumber3.ToString()); Console.WriteLine("angle4 = " + footrot.ToString() + "BothHandSwing = " + (countnumber4 - 1).ToString()); Point centerPoint = this.GetPosition2DLocation(depthImageFrame, skeleton.Position); // Scale the skeleton thickness // 1.0 is the desired size at 640 width double scale = this.RenderSize.Width / 640; skeletonCanvas.RefreshSkeleton(skeleton, jointMapping, centerPoint, scale); } if (ImageType == ImageType.Depth) { this.ChooseTrackedSkeletons(this.skeletonData); } } } } }
private void KinectAllFramesReady(object sender, AllFramesReadyEventArgs e) { if (!((KinectSensor)sender).SkeletonStream.IsEnabled) { return; } KinectSensor sensor = sender as KinectSensor; foreach (var skeletonCanvas in this.skeletonCanvases) { skeletonCanvas.Skeleton = null; } // Have we already been "shut down" by the user of this viewer, // or has the SkeletonStream been disabled since this event was posted? if ((null == this.KinectSensorManager) || (null == sensor) || (null == sensor.SkeletonStream) || !sensor.SkeletonStream.IsEnabled) { return; } bool haveSkeletonData = false; using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { if ((this.skeletonData == null) || (this.skeletonData.Length != skeletonFrame.SkeletonArrayLength)) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } skeletonFrame.CopySkeletonDataTo(this.skeletonData); haveSkeletonData = true; } } if (haveSkeletonData) { ColorImageFormat colorFormat = ColorImageFormat.Undefined; int colorWidth = 0; int colorHeight = 0; DepthImageFormat depthFormat = DepthImageFormat.Undefined; int depthWidth = 0; int depthHeight = 0; switch (this.ImageType) { case ImageType.Color: // Retrieve the current color format, from the frame if present, and from the sensor if not. using (ColorImageFrame colorImageFrame = e.OpenColorImageFrame()) { if (null != colorImageFrame) { colorFormat = colorImageFrame.Format; colorWidth = colorImageFrame.Width; colorHeight = colorImageFrame.Height; } else if (null != sensor.ColorStream) { colorFormat = sensor.ColorStream.Format; colorWidth = sensor.ColorStream.FrameWidth; colorHeight = sensor.ColorStream.FrameHeight; } } break; case ImageType.Depth: // Retrieve the current depth format, from the frame if present, and from the sensor if not. using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame()) { if (null != depthImageFrame) { depthFormat = depthImageFrame.Format; depthWidth = depthImageFrame.Width; depthHeight = depthImageFrame.Height; } else if (null != sensor.DepthStream) { depthFormat = sensor.DepthStream.Format; depthWidth = sensor.DepthStream.FrameWidth; depthHeight = sensor.DepthStream.FrameHeight; } } break; } for (int i = 0; i < this.skeletonData.Length && i < this.skeletonCanvases.Count; i++) { var skeleton = this.skeletonData[i]; var skeletonCanvas = this.skeletonCanvases[i]; var jointMapping = this.jointMappings[i]; jointMapping.Clear(); try { // Transform the data into the correct space // For each joint, we determine the exact X/Y coordinates for the target view foreach (Joint joint in skeleton.Joints) { Point mappedPoint = Get2DPosition( sensor, this.ImageType, this.RenderSize, joint.Position, colorFormat, colorWidth, colorHeight, depthFormat, depthWidth, depthHeight); jointMapping[joint.JointType] = new JointMapping { Joint = joint, MappedPoint = mappedPoint }; } } catch (UnauthorizedAccessException) { // Kinect is no longer available. return; } // Look up the center point Point centerPoint = Get2DPosition( sensor, this.ImageType, this.RenderSize, skeleton.Position, colorFormat, colorWidth, colorHeight, depthFormat, depthWidth, depthHeight); // Scale the skeleton thickness // 1.0 is the desired size at 640 width double scale = this.RenderSize.Width / 640; skeletonCanvas.Skeleton = skeleton; skeletonCanvas.JointMappings = jointMapping; skeletonCanvas.Center = centerPoint; skeletonCanvas.ScaleFactor = scale; } } }
void kinect_AllFramesReady(object sender, AllFramesReadyEventArgs imageFrames) { using (SkeletonFrame skeletonFrame = imageFrames.OpenSkeletonFrame()) if (skeletonFrame != null) { if ((skeletonData == null) || (this.skeletonData.Length != skeletonFrame.SkeletonArrayLength)) this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; //Copy the skeleton data to our array skeletonFrame.CopySkeletonDataTo(this.skeletonData); } if (skeletonData != null) foreach (Skeleton skel in skeletonData) if (skel.TrackingState == SkeletonTrackingState.Tracked) skeleton = skel; }
void Sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) { // Color using (var frame = e.OpenColorImageFrame()) { if (frame != null) { if (_mode == CameraMode.Color) { camera.Source = frame.ToBitmap(); } } } // Depth using (var frame = e.OpenDepthImageFrame()) { if (frame != null) { if (_mode == CameraMode.Depth) { camera.Source = frame.ToBitmap(); } } } // Body using (var frame = e.OpenSkeletonFrame()) { if (frame != null) { canvas.Children.Clear(); frame.CopySkeletonDataTo(_totalbodies); foreach (var body in _totalbodies) { if (body.TrackingState == SkeletonTrackingState.Tracked) { // COORDINATE MAPPING foreach (Joint joint in body.Joints) { // 3D coordinates in meters SkeletonPoint skeletonPoint = joint.Position; // 2D coordinates in pixels Point point = new Point(); if (_mode == CameraMode.Color) { // Skeleton-to-Color mapping ColorImagePoint colorPoint = _sensor.CoordinateMapper.MapSkeletonPointToColorPoint(skeletonPoint, ColorImageFormat.RgbResolution640x480Fps30); point.X = colorPoint.X; point.Y = colorPoint.Y; } else if (_mode == CameraMode.Depth) // Remember to change the Image and Canvas size to 320x240. { // Skeleton-to-Depth mapping DepthImagePoint depthPoint = _sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(skeletonPoint, DepthImageFormat.Resolution320x240Fps30); point.X = depthPoint.X; point.Y = depthPoint.Y; } // DRAWING... Ellipse ellipse = new Ellipse { Fill = Brushes.LightBlue, Width = 20, Height = 20 }; Canvas.SetLeft(ellipse, point.X - ellipse.Width / 2); Canvas.SetTop(ellipse, point.Y - ellipse.Height / 2); canvas.Children.Add(ellipse); } } } } } }