Skip to content

Instantly share code, notes, and snippets.

@ochilab
Last active September 30, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/c0659d97ba0f3a0488402c5cf4289a7f to your computer and use it in GitHub Desktop.
Save ochilab/c0659d97ba0f3a0488402c5cf4289a7f to your computer and use it in GitHub Desktop.
Kinect v2の初期設定テンプレート
//変数宣言
Body[] bodies;
KinectSensor kinect;
//Color宣言
FrameDescription colorFrameDesc;
byte[] colorBuffer;
//ColorSpacePoint[] colorPoints;
//Depth宣言
FrameDescription depthFrameDesc;
ushort[] depthBuffer;
WriteableBitmap depthImage;
Int32Rect depthRect;
int depthStride;
System.Windows.Point depthPoint;
//BodyIndex宣言
BodyIndexFrameReader bodyIndexFrameReader;
FrameDescription bodyIndexFrameDesc;
byte[] bodyIndexBuffer;
WriteableBitmap bodyIndexColorImage;
Int32Rect bodyIndexColorRect;
int bodyIndexColorStride;
int bodyIndexColorBytesPerPixel = 4;
byte[] bodyIndexColorBuffer;
// -----ここまで変数宣言 -----------------------
// 以下、しかるべきメソッドで(Window_Loaded等)
kinect = KinectSensor.GetDefault();
kinect.Open();
// 表示のためのデータを作成
colorFrameDesc = kinect.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
colorBuffer = new byte[colorFrameDesc.LengthInPixels*colorFrameDesc.BytesPerPixel];
depthFrameDesc = kinect.DepthFrameSource.FrameDescription;
depthImage = new WriteableBitmap(depthFrameDesc.Width, depthFrameDesc.Height, 96, 96, PixelFormats.Gray16, null);
depthBuffer = new ushort[depthFrameDesc.LengthInPixels];
depthRect = new Int32Rect(0, 0, depthFrameDesc.Width, depthFrameDesc.Height);
depthStride = (int)(depthFrameDesc.Width * depthFrameDesc.BytesPerPixel);
bodyIndexFrameDesc = kinect.BodyIndexFrameSource.FrameDescription;
bodyIndexBuffer = new byte[bodyIndexFrameDesc.LengthInPixels];
//下記は人物領域を表示する際に使う
bodyIndexColorImage = new WriteableBitmap(bodyIndexFrameDesc.Width, bodyIndexFrameDesc.Height, 96, 96, PixelFormats.Bgra32, null);
bodyIndexColorRect = new Int32Rect(0, 0, bodyIndexFrameDesc.Width, bodyIndexFrameDesc.Height);
bodyIndexColorStride = (int)(bodyIndexFrameDesc.Width * bodyIndexColorBytesPerPixel);
bodyIndexColorBuffer = new byte[bodyIndexFrameDesc.LengthInPixels * bodyIndexColorBytesPerPixel];
bodyIndexFrameReader = kinect.BodyIndexFrameSource.OpenReader();
depthPoint = new System.Windows.Point(depthFrameDesc.Width / 2, depthFrameDesc.Height / 2);
Width = depthFrameDesc.Width;
Height = depthFrameDesc.Height;
bodies = new Body[kinect.BodyFrameSource.BodyCount];
if (kinect != null){
// Multiリーダーを開く
MultiSourceFrameReader multiReader = kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Color|FrameSourceTypes.Depth | FrameSourceTypes.Body |FrameSourceTypes.BodyIndex);
multiReader.MultiSourceFrameArrived += multiReader_MultiSourceFrameArrived;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment