Last active
July 21, 2019 06:07
-
-
Save kaorun55/8a56fcfdca25ddd05a490b9c6c0cabdb to your computer and use it in GitHub Desktop.
Azure Kinect SDKで複数台Kinectから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 | |
{ | |
auto getDefaultConfig = []() | |
{ | |
// パラメーターは全部設定しないとエラーになる | |
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_2X2BINNED; | |
config.wired_sync_mode = K4A_WIRED_SYNC_MODE_STANDALONE; | |
config.synchronized_images_only = true; | |
config.depth_delay_off_color_usec = 0; | |
config.subordinate_delay_off_master_usec = 0; | |
config.disable_streaming_indicator = false; | |
return config; | |
}; | |
// Kinectの接続数を取得する | |
auto count = k4a::device::get_installed_count(); | |
std::cout << "Kinect Count : " << count << std::endl; | |
if (count == 0) { | |
throw std::runtime_error("Kinectが接続されていません"); | |
} | |
// Kinectを開く | |
std::vector<k4a::device> kinect(count); | |
k4a::device* master = nullptr; | |
for (int i = 0; i < kinect.size(); ++i) { | |
auto& k = kinect[i]; | |
k = k4a::device::open(i); | |
std::cout << "Open Kinect " << i << std::endl; | |
// Inに接続されているKinect(Sub)を先に起動する | |
if (k.is_sync_in_connected()) { | |
std::cout << "sub " << k.get_serialnum() << std::endl; | |
// Subにして起動する | |
auto config = getDefaultConfig(); | |
config.wired_sync_mode = K4A_WIRED_SYNC_MODE_SUBORDINATE; | |
k.start_cameras(&config); | |
} | |
// Inに接続されていないKinect(正確にはOut接続/In未接続)がMaster | |
else { | |
master = &k; | |
} | |
} | |
if (master == nullptr) { | |
throw std::runtime_error("MasterのKinectがありません。接続を確認してください。"); | |
} | |
// Masterを起動する | |
std::cout << "master " << master->get_serialnum() << std::endl; | |
auto config = getDefaultConfig(); | |
config.wired_sync_mode = K4A_WIRED_SYNC_MODE_MASTER; | |
master->start_cameras(&config); | |
while (1) { | |
for (int i = 0; i < kinect.size(); ++i) { | |
auto& k = kinect[i]; | |
// フレームデータを取得する | |
k4a::capture capture; | |
auto ret = k.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 " + std::to_string(i), 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 " + std::to_string(i), 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 " + std::to_string(i), ir); | |
std::cout << "Kinect " << i << ": " | |
<< colorImage.get_device_timestamp().count() << " " | |
<< depthImage.get_device_timestamp().count() << " " | |
<< irImage.get_device_timestamp().count() << std::endl; | |
} | |
// 表示、キー入力処理 | |
auto key = cv::waitKey(1); | |
if (key == 'q') { | |
break; | |
} | |
} | |
} | |
catch (const std::exception& ex) | |
{ | |
std::cout << ex.what() << std::endl; | |
return -1; | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment