Skip to content

Instantly share code, notes, and snippets.

@TakashiYoshinaga
Created December 5, 2019 03:35
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 TakashiYoshinaga/3542b27f206797f356ff420a84b11ae8 to your computer and use it in GitHub Desktop.
Save TakashiYoshinaga/3542b27f206797f356ff420a84b11ae8 to your computer and use it in GitHub Desktop.
Acquisition of DepthImage of Azure Kinecct
private void SetDepthBitmap(Capture capture)
{
//Depth画像を取得
Image depthImage = capture.Depth;
//Depth画像の各ピクセルの値(奥行)のみを取得
ushort[] depthArray = depthImage.GetPixels<ushort>().ToArray();
//depthBitmapの各画素に値を書き込む準備
BitmapData bitmapData = depthBitmap.LockBits(new Rectangle(0, 0, depthBitmap.Width, depthBitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
unsafe
{
//各ピクセルの値へのポインタ
byte* pixels = (byte*)bitmapData.Scan0;
int index;
int depth;
//1ピクセルずつ処理
for (int i = 0; i < depthArray.Length; i++)
{
//500~5000mmを0~255に変換
depth = (int)(255 * (depthArray[i] - 500) / 5000.0);
if (depth < 0 || depth > 255) depth = 0;
index = i * 4;
pixels[index++] = (byte)depth;
pixels[index++] = (byte)depth;
pixels[index++] = (byte)depth;
pixels[index++] = 255; //Alphaは不透明でOK
}
}
//書き込み終了
depthBitmap.UnlockBits(bitmapData);
//用済みのdepthImageのメモリを解放
depthImage.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment