Skip to content

Instantly share code, notes, and snippets.

@TakashiYoshinaga
Last active March 1, 2023 09:33
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/9c24f4c234f5fe438e6b578e83fe5d9e to your computer and use it in GitHub Desktop.
Save TakashiYoshinaga/9c24f4c234f5fe438e6b578e83fe5d9e to your computer and use it in GitHub Desktop.
Color Image Viewer of Azure Kinect
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;
//(追加)Kinectで取得した画像のポインタにアクセスするのに必要
using System.Buffers;
//(追加)AzureKinectとSystemの変数名の曖昧さをなくすため下記を追加
using Image = Microsoft.Azure.Kinect.Sensor.Image;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
namespace AzureKinectTest
{
public partial class Form1 : Form
{
//Kinectを扱う変数
Device kinect;
//カラー画像のBitmap
Bitmap colorBitmap;
//(追加)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))
{
unsafe
{
//カラー画像を取得
Image colorImage = capture.Color;
//画像のメモリのアドレスを取得
using (MemoryHandle pin =colorImage.Memory.Pin())
{
//Bitmap画像を作成
colorBitmap = new Bitmap(
colorImage.WidthPixels, //カラー画像の横幅
colorImage.HeightPixels,//カラー画像の縦幅
colorImage.StrideBytes, //横一列のバイト数(width*4)
PixelFormat.Format32bppArgb,//カラーフォーマット(RGBA)
(IntPtr)pin.Pointer); //各ピクセルの色情報
}
}
//pictureBoxに画像を貼り付ける
pictureBox1.Image = colorBitmap;
}
//表示を更新
this.Update();
}
//ループが終了したらKinectも停止
kinect.StopCameras();
}
//Bitmap画像に関する初期設定
private void InitBitmap()
{
//カラー画像の横幅(width)と縦幅(height)を取得
int width = kinect.GetCalibration().ColorCameraCalibration.ResolutionWidth;
int height = kinect.GetCalibration().ColorCameraCalibration.ResolutionHeight;
//PictureBoxに貼り付けるBitmap画像を作成。サイズはkinectのカラー画像と同じ
colorBitmap = 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;
}
}
}
@meschi89
Copy link

meschi89 commented Mar 5, 2021

I can not understand any of your comments, but your code helped me. :=)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment