emgucv 抠图

来源:互联网 发布:淘宝品牌 编辑:程序博客网 时间:2024/06/16 08:48

我的环境的KinectSDK2.0+EmguCV3.0.0

依旧还是WinFrom和ImageBox

因为需要用到BodyIndex的数据,但BodyIndex的分辨率和RGB图像的分辨率不同,所以需要用的CoordinateMap类中的坐标转换函数。

 

 

代码和注释如下:

[csharp] view plain copy
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using Microsoft.Kinect;  
  10. using Emgu.CV;  
  11. using Emgu.CV.Structure;  
  12. using Emgu.Util;  
  13.   
  14. namespace Kinect_koutu_2  
  15. {  
  16.     public partial class Form1 : Form  
  17.     {  
  18.         KinectSensor kinect = null;  
  19.         MultiSourceFrameReader framereader = null;  
  20.         FrameDescription fd = null;  
  21.         FrameDescription cfd = null;  
  22.         CoordinateMapper coordinate = null;  
  23.         Image<Bgra, byte> colorimg = null;  
  24.         DepthSpacePoint[] colorMappedToDepthPoints = null;  
  25.         byte[] colordata = null;  
  26.         public Form1()  
  27.         {  
  28.             InitializeComponent();  
  29.             CvInvoke.UseOpenCL = true;  
  30.             kinect = KinectSensor.GetDefault();  
  31.             coordinate = kinect.CoordinateMapper;  
  32.             framereader = kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Color | FrameSourceTypes.BodyIndex);  
  33.             framereader.MultiSourceFrameArrived += Framereader_MultiSourceFrameArrived;  
  34.             fd = kinect.BodyIndexFrameSource.FrameDescription;  
  35.             cfd = kinect.ColorFrameSource.FrameDescription;  
  36.             colorMappedToDepthPoints = new DepthSpacePoint[cfd.Width * cfd.Height];  
  37.             colorimg = new Image<Bgra, byte>(cfd.Width, cfd.Height);  
  38.             colordata = new byte[colorimg.Bytes.Count<byte>()];  
  39.             kinect.Open();  
  40.         }  
  41.         private void Framereader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)  
  42.         {  
  43.             MultiSourceFrame multiSourceFrame = e.FrameReference.AcquireFrame();  
  44.             if (multiSourceFrame == null)  
  45.                 return;  
  46.             ColorFrame cFrame = multiSourceFrame.ColorFrameReference.AcquireFrame();  
  47.             BodyIndexFrame bframe = multiSourceFrame.BodyIndexFrameReference.AcquireFrame();  
  48.             DepthFrame dframe = multiSourceFrame.DepthFrameReference.AcquireFrame();  
  49.             if (dframe == null || bframe == null || cFrame == null)  
  50.             {  
  51.                 Console.WriteLine("null");  
  52.                 return;  
  53.             }  
  54.             cFrame.CopyConvertedFrameDataToArray(colordata, ColorImageFormat.Bgra);  
  55.             //colorimg.Bytes = colordata;  
  56.             //imageBox1.Image = colorimg;  
  57.             using (KinectBuffer dB = dframe.LockImageBuffer())  
  58.             {  
  59.                 coordinate.MapColorFrameToDepthSpaceUsingIntPtr(dB.UnderlyingBuffer, dB.Size, colorMappedToDepthPoints);       //坐标转换并储存到数组  
  60.             }  
  61.               
  62.             using (KinectBuffer kB = bframe.LockImageBuffer())  
  63.             {  
  64.                 ProcessBodyIndexFrameData(kB.UnderlyingBuffer);  
  65.                 colorimg.Bytes = colordata;  
  66.                 imageBox1.Image = colorimg;  
  67.             }  
  68.               
  69.             dframe.Dispose();  
  70.             cFrame.Dispose();  
  71.             bframe.Dispose();  
  72.         }  
  73.   
  74.         private unsafe void ProcessBodyIndexFrameData(IntPtr bodyIndexFrameData)  
  75.         {  
  76.             byte* frameData = (byte*)bodyIndexFrameData;  
  77.             int colorMappedToDepthPointCount = this.colorMappedToDepthPoints.Length;  
  78.             fixed (DepthSpacePoint* colorMappedToDepthPointsPointer = this.colorMappedToDepthPoints)  
  79.             {  
  80.                 for (int i = 0; i < colorMappedToDepthPointCount; ++i)  
  81.                 {  
  82.                     float colorMappedToDepthX = colorMappedToDepthPointsPointer[i].X;  
  83.                     float colorMappedToDepthY = colorMappedToDepthPointsPointer[i].Y;  
  84.                     int depthX = (int)(colorMappedToDepthX + 0.5f);         //colorimage的像素点的位置在景深图的对应位置  
  85.                     int depthY = (int)(colorMappedToDepthY + 0.5f);  
  86.                     if ((depthX >= 0) && (depthX < 512) && (depthY >= 0) && (depthY < 424))  
  87.                     {  
  88.                         int depthIndex = (depthY * 512) + depthX;  
  89.                         if (frameData[depthIndex] ==255)             //在检测范围内frameData[depthIndex] !=255 为检测到人的像素点,不予以操作,并将其他像素点设置为黑色  
  90.                         {  
  91.                             colordata[i * 4] = 0;  
  92.                             colordata[i * 4 + 1] = 0;  
  93.                             colordata[i * 4 + 2] = 0;  
  94.                             colordata[i * 4 + 3] = 255;  
  95.                         }  
  96.                     }  
  97.                     else  
  98.                     {  
  99.                         colordata[i * 4] = 0;  
  100.                         colordata[i * 4 + 1] = 0;  
  101.                         colordata[i * 4 + 2] = 0;  
  102.                         colordata[i * 4 + 3] = 255;  
  103.                     }  
  104.                 }  
  105.             }  
  106.         }  
  107.   
  108.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  109.         {  
  110.             if (this.kinect != null)  
  111.             {  
  112.                 this.kinect.Close();  
  113.                 this.kinect = null;  
  114.             }  
  115.         }  
  116.     }  
  117. }  
0 0
原创粉丝点击