public static UpdateWriteBMP ( System.Windows.Media.Imaging.WriteableBitmap wbmp, |
||
wbmp | System.Windows.Media.Imaging.WriteableBitmap | |
bmp | ||
Результат | void |
public bool ProcessFrame() { if (CurrentFrame >= TotalFrames - 1) { return(false); } if (_CCapture != null) { var r = _CCapture.Grab(); var imgc = _CCapture.RetrieveBgrFrame(); ImageConverter.UpdateWriteBMP(colorWriteBitmap, imgc.ToBitmap()); } if (_DCapture != null) { var r = _DCapture.Grab(); var img = _DCapture.RetrieveBgrFrame(); int framenumber = (int)_DCapture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES); //Console.WriteLine("frame:"+framenumber.ToString()); CurrentFrame = framenumber - 1; if (img == null) { return(false); } //Show time stamp double time_index = _DCapture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_MSEC); PointF rightVector = new PointF(-10, -10); PointF leftVector = new PointF(10, -10); bool isSkip = false; bool leftHandRaise = false; if (CurrentFrame >= sktList.Count || !sktList[CurrentFrame].Tracked) { // no skeleton detected headDepth = 0; } else { headPosition = sktList[CurrentFrame][MyJointType.Head].PosDepth; headDepth = Math.Min((int)img[headPosition.Y, headPosition.X].Green, (int)img[headPosition.Y, headPosition.X].Red); headDepth = Math.Min(headDepth, (int)img[headPosition.Y, headPosition.X].Blue); PointF hr = sktList[CurrentFrame][MyJointType.HandR].PosDepth; PointF hl = sktList[CurrentFrame][MyJointType.HandL].PosDepth; PointF er = sktList[CurrentFrame][MyJointType.ElbowR].PosDepth; PointF el = sktList[CurrentFrame][MyJointType.ElbowL].PosDepth; PointF hip = sktList[CurrentFrame][MyJointType.HipCenter].PosDepth; // hand is lower than hip //Console.WriteLine(sktList[CurrentFrame][MyJointType.HandR].Pos3D.Y); //Console.WriteLine(sktList[CurrentFrame][MyJointType.ElbowR].Pos3D.Y); //Console.WriteLine(sktList[CurrentFrame][MyJointType.HipCenter].Pos3D.Y); //Console.WriteLine("-------------"); if (sktList[CurrentFrame][MyJointType.HandR].Pos3D.Y < sktList[CurrentFrame][MyJointType.HipCenter].Pos3D.Y + 0.05) { isSkip = true; } if (sktList[CurrentFrame][MyJointType.HandL].Pos3D.Y > -0.02f) { 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); } #region temp //isSkip = false; //leftHandRaise = false; //rightVector = new PointF(-10, -10); //leftVector = new PointF(10, -10); //headDepth = (int)img[85, 315].Blue; #endregion // Console.WriteLine("headdepth:"+headDepth.ToString()); //***********cull image***************** double cull = headDepth - CullingThresh; var depthImg = img.ThresholdToZeroInv(new Bgr(cull, cull, cull)); //Image<Gray, Byte> depthImg = img.Convert<Gray, byte>().ThresholdBinary(new Gray(160), new Gray(255)); var sw = Stopwatch.StartNew(); int handDepth = (int)(2600.0 / 255 * cull + 400); HandShapeModel handModel = null; Image <Gray, Byte> rightFront = null; Image <Gray, Byte> leftFront = null; // isskip is invalid coz no hip data isSkip = false; if (cull > 0) { handModel = m_opencv.FindHandPart(ref depthImg, out rightFront, out leftFront, handDepth, rightVector, leftVector, leftHandRaise); } viewer.Image = depthImg; // no hands detected if (handModel == null) { handModel = new HandShapeModel(0, HandEnum.None); } var row = new CsvRow(); row.Add(CurrentFrame.ToString()); row.Add(handModel.type.ToString()); switch (handModel.type) { case HandEnum.Intersect: case HandEnum.Right: row.Add(handModel.handPosRight.center.X.ToString()); row.Add(handModel.handPosRight.center.Y.ToString()); row.Add(handModel.handPosRight.size.Width.ToString()); row.Add(handModel.handPosRight.size.Height.ToString()); row.Add(handModel.handPosRight.angle.ToString()); break; case HandEnum.Both: row.Add(handModel.handPosRight.center.X.ToString()); row.Add(handModel.handPosRight.center.Y.ToString()); row.Add(handModel.handPosRight.size.Width.ToString()); row.Add(handModel.handPosRight.size.Height.ToString()); row.Add(handModel.handPosRight.angle.ToString()); row.Add(handModel.handPosLeft.center.X.ToString()); row.Add(handModel.handPosLeft.center.Y.ToString()); row.Add(handModel.handPosLeft.size.Width.ToString()); row.Add(handModel.handPosLeft.size.Height.ToString()); row.Add(handModel.handPosLeft.angle.ToString()); break; } row.Add(cull.ToString()); labelWriter.WriteRow(row); if (rightFront != null) { ImageConverter.UpdateWriteBMP(WrtBMP_RightHandFront, rightFront.ToBitmap()); } ImageConverter.UpdateWriteBMP(depthWriteBitmap, img.ToBitmap()); if (CurrentFrame < TotalFrames - 1) { return(true); } } return(false); }
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); } } }