Created
December 3, 2019 09:53
-
-
Save TakashiYoshinaga/483e41808e73121580856d439c56bed2 to your computer and use it in GitHub Desktop.
Color Image Viewer of Azure Kinect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
//AzureKinectSDKの読み込み | |
using Microsoft.Azure.Kinect.Sensor; | |
//(追加)AzureKinectとSystemの変数名の曖昧さをなくすため下記を追加 | |
using Image = Microsoft.Azure.Kinect.Sensor.Image; | |
using BitmapData = System.Drawing.Imaging.BitmapData; | |
using PixelFormat = System.Drawing.Imaging.PixelFormat; | |
namespace AzureKinectTest | |
{ | |
public partial class Form1 : Form | |
{ | |
//Kinectを扱う変数 | |
Device kinect; | |
//Depth画像のBitmap | |
Bitmap depthBitmap; | |
//(追加)Kinectの画像取得の可否 | |
bool loop = true; | |
public Form1() | |
{ | |
InitializeComponent(); | |
InitKinect(); | |
//Kinectの設定情報に基づいてBitmap関連情報を初期化 | |
InitBitmap(); | |
//(追加)初期化が終わったのでデータ取得開始 | |
Task t = KinectLoop(); | |
} | |
//(追加)Kinectからデータを取得して表示するメソッド | |
private async Task KinectLoop() | |
{ | |
//loopがtrueの間はデータを取り続ける | |
while (loop) | |
{ | |
//kinectから新しいデータをもらう | |
using (Capture capture = await Task.Run(() => kinect.GetCapture()).ConfigureAwait(true)) | |
{ | |
//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; | |
//一ピクセルずつ処理 | |
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; | |
} | |
} | |
//書き込み終了 | |
depthBitmap.UnlockBits(bitmapData); | |
//pictureBoxに画像を貼り付け | |
pictureBox1.Image = depthBitmap; | |
} | |
//表示を更新 | |
this.Update(); | |
} | |
//ループが終了したらKinectも停止 | |
kinect.StopCameras(); | |
} | |
//Bitmap画像に関する初期設定 | |
private void InitBitmap() | |
{ | |
//Depth画像の横幅(width)と縦幅(height)を取得 | |
int width = kinect.GetCalibration().DepthCameraCalibration.ResolutionWidth; | |
int height = kinect.GetCalibration().DepthCameraCalibration.ResolutionHeight; | |
//PictureBoxに貼り付けるBitmap画像を作成。サイズはkinectのDepth画像と同じ | |
depthBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); | |
} | |
//Kinectの初期化(Form1コンストラクタから呼び出す) | |
private void InitKinect() | |
{ | |
//0番目のKinectと接続 | |
kinect = Device.Open(0); | |
//Kinectの各種モードを設定して動作開始(設定内容自体は今回は特に考えなくてOK) | |
kinect.StartCameras(new DeviceConfiguration | |
{ | |
ColorFormat = ImageFormat.ColorBGRA32, | |
ColorResolution = ColorResolution.R720p, | |
DepthMode = DepthMode.NFOV_2x2Binned, | |
SynchronizedImagesOnly = true, | |
CameraFPS = FPS.FPS30 | |
}); | |
} | |
//アプリ終了時にKinect終了 | |
private void Form1_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
kinect.StopCameras(); | |
//(追加)前回の記事では上記のコードを用い、本メソッド内で | |
//Kinectを止めていたが本プログラムではフラグをfalseにして | |
//whileループを止めるようにする | |
loop = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment