Skip to content

Instantly share code, notes, and snippets.

@UnaNancyOwen
Last active September 8, 2019 17:00
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 UnaNancyOwen/90b898366eb908d29cb4c2b509ab6cfa to your computer and use it in GitHub Desktop.
Save UnaNancyOwen/90b898366eb908d29cb4c2b509ab6cfa to your computer and use it in GitHub Desktop.
cmake_minimum_required( VERSION 3.6 )
set( CMAKE_CXX_STANDARD 11 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
set( CMAKE_CXX_EXTENSIONS OFF )
# Create Project
project( K4A )
add_executable( K4A main.cpp )
# Set Startup Project
set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "K4A" )
# Find Package
# Azure Kinect Sensor SDK
set( CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" ${CMAKE_MODULE_PATH} )
find_package( k4a REQUIRED )
# OpenCV
set( OpenCV_DIR "C:/Program Files/opencv/build" CACHE PATH "Path to OpenCV config directory." )
find_package( OpenCV REQUIRED )
if( k4a_FOUND AND OpenCV_FOUND )
# Include Directories
include_directories( ${OpenCV_INCLUDE_DIRS} )
# Target Link Libraries
target_link_libraries( K4A k4a::k4a )
target_link_libraries( K4A ${OpenCV_LIBS} )
endif()
#.rst:
# Findk4a
# -------
#
# Find Azure Kinect Sensor SDK include dirs, and libraries.
#
# IMPORTED Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines the :prop_tgt:`IMPORTED` targets:
#
# ``k4a::k4a``
# Defined if the system has Azure Kinect Sensor SDK.
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# This module sets the following variables:
#
# ::
#
# k4a_FOUND True in case Azure Kinect Sensor SDK is found, otherwise false
# k4a_ROOT Path to the root of found Azure Kinect Sensor SDK installation
#
# Example usage
# ^^^^^^^^^^^^^
#
# ::
#
# find_package(k4a REQUIRED)
#
# add_executable(foo foo.cc)
# target_link_libraries(foo k4a::k4a)
#
# License
# ^^^^^^^
#
# Copyright (c) 2019 Tsukasa SUGIURA
# Distributed under the MIT License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
find_path(k4a_INCLUDE_DIR
NAMES
k4a/k4a.h
HINTS
$ENV{K4A_DIR}/sdk/
/usr/include
PATHS
"${K4A_DIR_PATH}/sdk/"
PATH_SUFFIXES
include
)
find_library(k4a_LIBRARY
NAMES
k4a.lib
libk4a.so
HINTS
$ENV{K4A_DIR}/windows-desktop/amd64/release
/usr/lib
PATHS
"${K4A_DIR_PATH}/sdk/windows-desktop/amd64/release"
PATH_SUFFIXES
lib
x86_64-linux-gnu
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
k4a DEFAULT_MSG
k4a_LIBRARY k4a_INCLUDE_DIR
)
if(k4a_FOUND)
add_library(k4a::k4a SHARED IMPORTED)
set_target_properties(k4a::k4a PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${k4a_INCLUDE_DIR}")
set_property(TARGET k4a::k4a APPEND PROPERTY IMPORTED_CONFIGURATIONS "RELEASE")
set_target_properties(k4a::k4a PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX")
if(WIN32)
set_target_properties(k4a::k4a PROPERTIES IMPORTED_IMPLIB_RELEASE "${k4a_LIBRARY}")
else()
set_target_properties(k4a::k4a PROPERTIES IMPORTED_LOCATION_RELEASE "${k4a_LIBRARY}")
endif()
set_property(TARGET k4a::k4a APPEND PROPERTY IMPORTED_CONFIGURATIONS "DEBUG")
set_target_properties(k4a::k4a PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX")
if(WIN32)
set_target_properties(k4a::k4a PROPERTIES IMPORTED_IMPLIB_DEBUG "${k4a_LIBRARY}")
else()
set_target_properties(k4a::k4a PROPERTIES IMPORTED_LOCATION_DEBUG "${k4a_LIBRARY}")
endif()
get_filename_component(K4a_ROOT "${k4a_INCLUDE_DIR}" PATH)
endif()
#include <iostream>
#include <k4a/k4a.h>
#include <k4a/k4a.hpp>
#include <opencv2/opencv.hpp>
int main( int argc, char* argv[] )
{
try{
// Get Connected Devices
const int32_t device_count = k4a_device_get_installed_count();
if( device_count == 0 ){
throw k4a::error( "Failed to found device!" );
}
// Open Default Device
k4a::device device = k4a::device::open( K4A_DEVICE_DEFAULT );
// Start Cameras with Configuration
k4a_device_configuration_t configuration = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
configuration.color_format = K4A_IMAGE_FORMAT_COLOR_BGRA32;
configuration.color_resolution = K4A_COLOR_RESOLUTION_720P;
configuration.depth_mode = K4A_DEPTH_MODE_NFOV_UNBINNED;
configuration.synchronized_images_only = true;
device.start_cameras( &configuration );
while( true ){
// Get Capture
k4a::capture capture;
const bool result = device.get_capture( &capture, std::chrono::milliseconds( K4A_WAIT_INFINITE ) );
if( !result ){
capture.reset();
continue;
}
// Get Color Image
k4a::image color_image = capture.get_color_image();
const int32_t color_width = color_image.get_width_pixels();
const int32_t color_height = color_image.get_height_pixels();
std::vector<uint8_t> color_buffer( color_image.get_buffer(), color_image.get_buffer() + color_image.get_size() );
cv::Mat color = cv::imdecode( color_buffer, cv::IMREAD_COLOR );
// Get Depth Image
k4a::image depth_image = capture.get_depth_image();
const int32_t depth_width = depth_image.get_width_pixels();
const int32_t depth_height = depth_image.get_height_pixels();
std::vector<uint8_t> depth_buffer( depth_image.get_buffer(), depth_image.get_buffer() + depth_image.get_size() );
cv::Mat depth = cv::Mat( depth_height, depth_width, CV_16UC1, &depth_buffer[0] );
// Get Infrared Image
k4a::image infrared_image = capture.get_ir_image();
const int32_t infrared_width = infrared_image.get_width_pixels();
const int32_t infrared_height = infrared_image.get_height_pixels();
std::vector<uint8_t> infrared_buffer( infrared_image.get_buffer(), infrared_image.get_buffer() + infrared_image.get_size() );
cv::Mat infrared = cv::Mat( infrared_height, infrared_width, CV_16UC1, &infrared_buffer[0] );
// Release Handles
color_image.reset();
depth_image.reset();
infrared_image.reset();
capture.reset();
// Show Images
depth.convertTo( depth, CV_8U, -255.0 / 5000.0, 255.0 );
infrared.convertTo( infrared, CV_8U, 0.5 );
cv::imshow( "color", color );
cv::imshow( "depth", depth );
cv::imshow( "infrared", infrared );
const int32_t key = cv::waitKey( 30 );
if( key == 'q' ){
break;
}
}
// Close Device
device.close();
// Close Window
cv::destroyAllWindows();
}
catch( const k4a::error& error ){
std::cout << error.what() << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment