Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ちょっと整形したAzure Kinectの点群取得サンプル
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Rendering;
using Microsoft.Azure.Kinect.Sensor;
namespace TestKinect
{
public class TestKinect : MonoBehaviour
{
private Device _kinect;
private int _pointCount = 0;
private Mesh _mesh;
private Vector3[] _vertices;
private Color32[] _colors;
private int[] _indices;
private Task _imageFetchTask;
private Image _depthColor;
//NOTE: コレはKinect側の画像変換のやつ。UnityのTransformではないので注意
private Transformation _transformation;
private CancellationTokenSource _cts;
private async void Start()
{
InitKinect();
InitMesh();
_cts = new CancellationTokenSource();
await GetImageRoutine(_cts.Token);
}
private void InitKinect()
{
_kinect = Device.Open();
_kinect.StartCameras(new DeviceConfiguration
{
ColorFormat = ImageFormat.ColorBGRA32,
ColorResolution = ColorResolution.R720p,
DepthMode = DepthMode.NFOV_2x2Binned,
SynchronizedImagesOnly = true,
CameraFPS = FPS.FPS30
});
var calibration = _kinect.GetCalibration();
_transformation = calibration.CreateTransformation();
_depthColor = new Image(
ImageFormat.ColorBGRA32,
calibration.DepthCameraCalibration.ResolutionWidth,
calibration.DepthCameraCalibration.ResolutionHeight,
calibration.DepthCameraCalibration.ResolutionWidth * 4
);
}
private void InitMesh()
{
var calibration = _kinect.GetCalibration();
int width = calibration.DepthCameraCalibration.ResolutionWidth;
int height = calibration.DepthCameraCalibration.ResolutionHeight;
_pointCount = width * height;
_mesh = new Mesh
{
indexFormat = IndexFormat.UInt32,
};
_vertices = new Vector3[_pointCount];
_colors = new Color32[_pointCount];
_indices = new int[_pointCount];
for (int i = 0; i < _indices.Length; i++)
{
_indices[i] = i;
}
_mesh.vertices = _vertices;
_mesh.colors32 = _colors;
_mesh.SetIndices(_indices, MeshTopology.Points, 0);
GetComponent<MeshFilter>().mesh = _mesh;
}
private async Task GetImageRoutine(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
using (var capture = await Task
.Run(() => _kinect.GetCapture())
.ConfigureAwait(true))
{
if (token.IsCancellationRequested)
{
break;
}
_transformation.ColorImageToDepthCamera(capture, _depthColor);
var colorArray = _depthColor.GetPixels<BGRA>().ToArray();
var xyzImage = _transformation.DepthImageToPointCloud(capture.Depth);
var xyzArray = xyzImage.GetPixels<Short3>().ToArray();
for (int i = 0; i < _pointCount; i++)
{
_vertices[i].x = xyzArray[i].X * 0.001f;
_vertices[i].y = -xyzArray[i].Y * 0.001f;
_vertices[i].z = xyzArray[i].Z * 0.001f;
_colors[i].b = colorArray[i].B;
_colors[i].g = colorArray[i].G;
_colors[i].r = colorArray[i].R;
_colors[i].a = 255;
}
_mesh.vertices = _vertices;
_mesh.colors32 = _colors;
_mesh.RecalculateBounds();
}
}
}
private void OnDestroy()
{
_kinect.StopCameras();
_cts?.Cancel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment