Created
July 21, 2019 06:12
-
-
Save kaorun55/1cdc5deb3d8d044a94c3142688f9f460 to your computer and use it in GitHub Desktop.
Azure Kinect SDKでColor,Depth,IRのデータを表示するサンプルです。OpenCVを使っています。
This file contains 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
#include <opencv2/opencv.hpp> | |
#include <k4a/k4a.hpp> | |
int main() | |
{ | |
try | |
{ | |
// Kinectの接続数を取得する | |
auto count = k4a::device::get_installed_count(); | |
if (count == 0) { | |
throw std::runtime_error("Kinectが接続されていません"); | |
} | |
// Kinectを開く | |
auto kinect = k4a::device::open(K4A_DEVICE_DEFAULT); | |
// Kinectの設定をしてカメラを開始する | |
k4a_device_configuration_t config = {}; | |
config.camera_fps = K4A_FRAMES_PER_SECOND_30; | |
config.color_format = K4A_IMAGE_FORMAT_COLOR_BGRA32; | |
config.color_resolution = K4A_COLOR_RESOLUTION_1080P; | |
config.depth_mode = K4A_DEPTH_MODE_NFOV_UNBINNED; | |
config.synchronized_images_only = true; | |
config.depth_delay_off_color_usec = 0; | |
config.wired_sync_mode = K4A_WIRED_SYNC_MODE_STANDALONE; | |
config.subordinate_delay_off_master_usec = 0; | |
config.disable_streaming_indicator = false; | |
kinect.start_cameras(&config); | |
// 差表変換を作成する | |
k4a::transformation transformation(kinect.get_calibration(config.depth_mode, config.color_resolution)); | |
while (1) { | |
// フレームデータを取得する | |
k4a::capture capture; | |
auto ret = kinect.get_capture(&capture, std::chrono::milliseconds(K4A_WAIT_INFINITE)); | |
if (!ret) { | |
continue; | |
} | |
// カラーデータを取得する | |
auto colorImage = capture.get_color_image(); | |
cv::Mat color = cv::Mat(colorImage.get_height_pixels(), colorImage.get_width_pixels(), CV_8UC4, colorImage.get_buffer()); | |
cv::imshow("Color", color); | |
// Depthデータを取得する | |
auto depthImage = capture.get_depth_image(); | |
cv::Mat depth = cv::Mat(depthImage.get_height_pixels(), depthImage.get_width_pixels(), CV_16U, depthImage.get_buffer()); | |
depth.convertTo(depth, CV_8U, 255.0 / 3000); | |
cv::imshow("Depth", depth); | |
// IR画像 | |
auto irImage = capture.get_ir_image(); | |
cv::Mat ir(irImage.get_height_pixels(), irImage.get_width_pixels(), CV_16U, irImage.get_buffer()); | |
//ir.convertTo(ir, CV_8U, 1 / 15.0f); | |
cv::imshow("IR", ir); | |
// Depth座標系のカラーデータ | |
// バッファは事前に作成する | |
// サイズはDepthと同じサイズ | |
// Strideはdepthのwidth * 4(= BGRA) | |
k4a::image depthColorImage = k4a::image::create(K4A_IMAGE_FORMAT_COLOR_BGRA32, | |
depthImage.get_width_pixels(), depthImage.get_height_pixels(), depthImage.get_width_pixels() * 4); | |
transformation.color_image_to_depth_camera(depthImage, colorImage, &depthColorImage); | |
cv::Mat depthColor(depthColorImage.get_height_pixels(), depthColorImage.get_width_pixels(), CV_8UC4, depthColorImage.get_buffer()); | |
cv::imshow("ColorToDepth", depthColor); | |
// 表示、キー入力処理 | |
auto key = cv::waitKey(1); | |
if (key == 'q') { | |
break; | |
} | |
} | |
} | |
catch (const std::exception & ex) | |
{ | |
std::cout << ex.what() << std::endl; | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment