Last active
July 12, 2023 10:17
-
-
Save UnaNancyOwen/be527932994adbe639521edb4462b1dd to your computer and use it in GitHub Desktop.
Double Exponential Smoothing Filter for Azure Kinect Body Tracking SDK
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
/* | |
k4a_double_exponential_filter.h | |
This file contains holt double exponential smoothing filter for filtering joints. | |
It was ported for Azure Kinect Body Tracking SDK based on following implementation. | |
https://social.msdn.microsoft.com/Forums/en-US/045b058a-ae3a-4d01-beb6-b756631b4b42 | |
std::unordered_map<int32_t, double_exponential_filter> double_exponential_filter; | |
while( true ) | |
{ | |
for( const k4abt_body_t& body : bodies ) | |
{ | |
double_exponential_filter[body.id].update( body, body_frame.get_timestamp() ); | |
k4abt_body_t filtered_body = double_exponential_filter[body.id].get_result(); | |
for( const k4abt_joint_t& filtered_joint : filtered_body.skeleton.joints ) | |
{ | |
// Access to filtered joint position ... | |
} | |
} | |
std::chrono::microseconds threshold( 50000 ); | |
std::for_each( double_exponential_filter.begin(), double_exponential_filter.end(), | |
[&]( auto& filter ){ | |
std::chrono::microseconds interval( body_frame.get_timestamp() - filter.second.get_timestamp() ); | |
if( interval.count() > threshold.count() ) | |
{ | |
double_exponential_filter.erase( filter.first ); | |
} | |
} | |
); | |
} | |
Copyright (c) 2019 Tsukasa Sugiura <t.sugiura0204@gmail.com> | |
Licensed 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. | |
*/ | |
#ifndef K4A_DOUBLE_EXPONENTIAL_FILTER | |
#define K4A_DOUBLE_EXPONENTIAL_FILTER | |
#include <k4a/k4a.h> | |
#include <k4abt.h> | |
#include <array> | |
#include <chrono> | |
k4a_float3_t operator * ( const float d, const k4a_float3_t& v ) | |
{ | |
k4a_float3_t r; | |
r.xyz.x = d * v.xyz.x; | |
r.xyz.y = d * v.xyz.y; | |
r.xyz.z = d * v.xyz.z; | |
return r; | |
} | |
k4a_float3_t operator + ( const k4a_float3_t& v1, const k4a_float3_t& v2 ) | |
{ | |
k4a_float3_t r; | |
r.xyz.x = v1.xyz.x + v2.xyz.x; | |
r.xyz.y = v1.xyz.y + v2.xyz.y; | |
r.xyz.z = v1.xyz.z + v2.xyz.z; | |
return r; | |
} | |
k4a_float3_t operator - ( const k4a_float3_t& v1, const k4a_float3_t& v2 ) | |
{ | |
k4a_float3_t r; | |
r.xyz.x = v1.xyz.x - v2.xyz.x; | |
r.xyz.y = v1.xyz.y - v2.xyz.y; | |
r.xyz.z = v1.xyz.z - v2.xyz.z; | |
return r; | |
} | |
class double_exponential_filter | |
{ | |
public: | |
double_exponential_filter() | |
: smoothing_( 0.5f ), | |
correction_( 0.5f ), | |
prediction_( 0.5f ), | |
jitter_radius_( 300.0f ), | |
max_deviation_radius_( 500.0f ), | |
id_( 0 ), | |
timestamp_( std::chrono::microseconds( 0 ) ) | |
{ | |
for( int32_t i = 0; i < K4ABT_JOINT_COUNT; i++ ) | |
{ | |
filtered_joints_[i].position = initialize(); | |
history_[i] = double_exponential_data(); | |
} | |
} | |
void set_parameter( const float smoothing, const float correction, const float prediction, const float jitter_radius, const float max_deviation_radius ) | |
{ | |
smoothing_ = smoothing; | |
correction_ = correction; | |
prediction_ = prediction; | |
jitter_radius_ = jitter_radius; | |
max_deviation_radius_ = max_deviation_radius; | |
} | |
void reset() | |
{ | |
set_parameter( 0.5f, 0.5f, 0.5f, 300.0f, 500.0f ); | |
id_ = 0; | |
timestamp_ = std::chrono::microseconds( 0 ); | |
for( int32_t i = 0; i < K4ABT_JOINT_COUNT; i++ ) | |
{ | |
filtered_joints_[i].position = initialize(); | |
history_[i] = double_exponential_data(); | |
} | |
} | |
void update( const k4abt_body_t& body, const std::chrono::microseconds timestamp ) | |
{ | |
id_ = body.id; | |
timestamp_ = timestamp; | |
for( int32_t i = 0; i < K4ABT_JOINT_COUNT; i++ ) | |
{ | |
k4a_float3_t raw_position = body.skeleton.joints[i].position; | |
k4a_float3_t filtered_position = initialize(); | |
k4a_float3_t trend = initialize(); | |
k4a_float3_t previous_raw_position = history_[i].raw_position; | |
k4a_float3_t previous_filtered_position = history_[i].filtered_position; | |
k4a_float3_t previous_trend = history_[i].trend; | |
k4a_float3_t diff = initialize(); | |
float length = 0.0f; | |
if( !is_valid( raw_position ) ) | |
{ | |
history_[i].frame_count = 0; | |
} | |
if( history_[i].frame_count == 0 ) | |
{ | |
filtered_position = raw_position; | |
trend = initialize(); | |
history_[i].frame_count++; | |
} | |
else if( history_[i].frame_count == 1 ) | |
{ | |
filtered_position = 0.5f * ( raw_position + previous_raw_position ); | |
diff = filtered_position - previous_filtered_position; | |
trend = ( correction_ * diff ) + ( ( 1.0f - correction_ ) * previous_trend ); | |
history_[i].frame_count++; | |
} | |
else | |
{ | |
diff = raw_position - previous_filtered_position; | |
length = std::sqrt( ( diff.xyz.x * diff.xyz.x ) + ( diff.xyz.y * diff.xyz.y ) + ( diff.xyz.z * diff.xyz.z ) ); | |
length = std::fabs( length ); | |
if( length <= jitter_radius_ ) | |
{ | |
filtered_position = ( ( length / jitter_radius_ ) * raw_position ) + ( ( 1.0f - length / jitter_radius_ ) * previous_filtered_position ); | |
} | |
else | |
{ | |
filtered_position = raw_position; | |
} | |
filtered_position = ( ( 1.0f - smoothing_ ) * filtered_position ) + ( smoothing_ * ( previous_filtered_position + previous_trend ) ); | |
diff = filtered_position - previous_filtered_position; | |
trend = ( correction_ * diff ) + ( ( 1.0f - correction_ ) * previous_trend ); | |
} | |
k4a_float3_t predicted_position = filtered_position + ( prediction_ * trend ); | |
diff = predicted_position - raw_position; | |
length = std::sqrt( ( diff.xyz.x * diff.xyz.x ) + ( diff.xyz.y * diff.xyz.y ) + ( diff.xyz.z * diff.xyz.z ) ); | |
length = std::fabs( length ); | |
if( length > max_deviation_radius_ ) | |
{ | |
predicted_position = ( ( ( max_deviation_radius_ / length ) * predicted_position ) + ( ( 1.0f - max_deviation_radius_ / length ) * raw_position ) ); | |
} | |
history_[i].raw_position = raw_position; | |
history_[i].filtered_position = filtered_position; | |
history_[i].trend = trend; | |
filtered_joints_[i].position.xyz.x = filtered_position.xyz.x; | |
filtered_joints_[i].position.xyz.y = filtered_position.xyz.y; | |
filtered_joints_[i].position.xyz.z = filtered_position.xyz.z; | |
filtered_joints_[i].orientation = body.skeleton.joints[i].orientation; | |
} | |
} | |
const k4abt_body_t get_result() | |
{ | |
k4abt_body_t result; | |
result.id = id_; | |
for( int32_t i = 0; i < K4ABT_JOINT_COUNT; i++ ) | |
{ | |
result.skeleton.joints[i].position.xyz.x = filtered_joints_[i].position.xyz.x; | |
result.skeleton.joints[i].position.xyz.y = filtered_joints_[i].position.xyz.y; | |
result.skeleton.joints[i].position.xyz.z = filtered_joints_[i].position.xyz.z; | |
result.skeleton.joints[i].orientation = filtered_joints_[i].orientation; | |
} | |
return result; | |
} | |
const std::chrono::microseconds get_timestamp() | |
{ | |
return timestamp_; | |
} | |
private: | |
inline bool is_valid( k4a_float3_t vector ) | |
{ | |
return ( vector.xyz.x != 0.0f || | |
vector.xyz.y != 0.0f || | |
vector.xyz.z != 0.0f ); | |
} | |
static k4a_float3_t initialize() | |
{ | |
k4a_float3_t vector; | |
vector.xyz.x = 0.0f; | |
vector.xyz.x = 0.0f; | |
vector.xyz.x = 0.0f; | |
return vector; | |
} | |
struct double_exponential_data | |
{ | |
k4a_float3_t raw_position; | |
k4a_float3_t filtered_position; | |
k4a_float3_t trend; | |
uint32_t frame_count; | |
double_exponential_data() | |
{ | |
raw_position = initialize(); | |
filtered_position = initialize(); | |
trend = initialize(); | |
frame_count = 0; | |
} | |
}; | |
std::array<k4abt_joint_t, K4ABT_JOINT_COUNT> filtered_joints_; | |
std::array <double_exponential_data, K4ABT_JOINT_COUNT> history_; | |
float smoothing_; | |
float correction_; | |
float prediction_; | |
float jitter_radius_; | |
float max_deviation_radius_; | |
uint32_t id_; | |
std::chrono::microseconds timestamp_; | |
}; | |
#endif |
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 <iostream> | |
#include <k4a/k4a.h> | |
#include <k4a/k4a.hpp> | |
#include <opencv2/opencv.hpp> | |
// Include Converter Utility Header | |
#include "util.h" | |
// Include C++ Wrapper for Azure Kinect Body Tracking SDK | |
#include "k4abt.hpp" | |
// Include Double Exponential Smoothing Filter for Azure Kinect Body Tracking SDK | |
#include "k4a_double_exponential_filter.h" | |
#include <unordered_map> | |
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 ); | |
// Initialize Transformation | |
k4a::calibration calibration = device.get_calibration( configuration.depth_mode, configuration.color_resolution ); | |
k4a::transformation transformation = k4a::transformation( calibration ); | |
// Create Tracker | |
k4abt::tracker tracker = k4abt::tracker::create( calibration ); | |
if( !tracker ){ | |
throw k4a::error( "Failed to create tracker!" ); | |
} | |
// Color Table for Visualize | |
std::vector<cv::Vec3b> colors; | |
colors.push_back( cv::Vec3b( 255, 0, 0 ) ); | |
colors.push_back( cv::Vec3b( 0, 255, 0 ) ); | |
colors.push_back( cv::Vec3b( 0, 0, 255 ) ); | |
colors.push_back( cv::Vec3b( 255, 255, 0 ) ); | |
colors.push_back( cv::Vec3b( 0, 255, 255 ) ); | |
colors.push_back( cv::Vec3b( 255, 0, 255 ) ); | |
// Initialize Double Exponential Filter | |
std::unordered_map<int32_t, double_exponential_filter> double_exponential_filter; | |
while( true ) | |
{ | |
// Get Capture | |
k4a::capture capture; | |
const std::chrono::milliseconds timeout( K4A_WAIT_INFINITE ); | |
const bool result = device.get_capture( &capture, timeout ); | |
if( !result ){ | |
break; | |
} | |
// Get Color Image | |
k4a::image color_image = capture.get_color_image(); | |
cv::Mat color = k4a::get_mat( color_image ); | |
// Get Depth Image | |
k4a::image depth_image = capture.get_depth_image(); | |
cv::Mat depth = k4a::get_mat( depth_image ); | |
// Enqueue Capture | |
tracker.enqueue_capture( capture ); | |
// Pop Tracker Result | |
k4abt::frame body_frame = tracker.pop_result(); | |
// Get Body Index Map | |
k4a::image body_index_map_image = body_frame.get_body_index_map(); | |
cv::Mat body_index_map = k4a::get_mat( body_index_map_image ); | |
// Get Body Skeleton | |
std::vector<k4abt_body_t> bodies = body_frame.get_bodies(); | |
// Clear Handle | |
capture.reset(); | |
color_image.reset(); | |
depth_image.reset(); | |
// Scaling Depth | |
depth.convertTo( depth, CV_8U, -255.0 / 5000.0, 255.0 ); | |
// Get Image that used for Inference | |
k4a::capture body_capture = body_frame.get_capture(); | |
k4a::image skeleton_image = body_capture.get_color_image(); | |
cv::Mat skeleton = k4a::get_mat( skeleton_image ); | |
// Draw Body Skeleton | |
for( k4abt_body_t& body : bodies ){ | |
// Draw Raw Skeleton | |
for( const k4abt_joint_t& joint : body.skeleton.joints ){ | |
k4a_float2_t position; | |
const bool result = calibration.convert_3d_to_2d( joint.position, K4A_CALIBRATION_TYPE_DEPTH, K4A_CALIBRATION_TYPE_COLOR, &position ); | |
if( !result ){ | |
continue; | |
} | |
const int32_t id = body.id; | |
const cv::Point point( static_cast<int32_t>( position.xy.x ), static_cast<int32_t>( position.xy.y ) ); | |
cv::circle( skeleton, point, 5, colors[( id - 1 ) % colors.size()], -1 ); | |
} | |
// Smooting Filter | |
double_exponential_filter[body.id].update( body, body_frame.get_timestamp() ); | |
// Draw Filtered Skeleton | |
k4abt_body_t filtered_body = double_exponential_filter[body.id].get_result(); | |
for( const k4abt_joint_t& filtered_joint : filtered_body.skeleton.joints ){ | |
k4a_float2_t filtered_position; | |
const bool result = calibration.convert_3d_to_2d( filtered_joint.position, K4A_CALIBRATION_TYPE_DEPTH, K4A_CALIBRATION_TYPE_COLOR, &filtered_position ); | |
if( !result ){ | |
continue; | |
} | |
const int32_t id = filtered_body.id; | |
const cv::Point filtered_point( static_cast<int32_t>( filtered_position.xy.x ), static_cast<int32_t>( filtered_position.xy.y ) ); | |
cv::circle( skeleton, filtered_point, 8, colors[( id - 1 ) % colors.size()], 1 ); | |
} | |
} | |
// Clean-Up Unused Filters (Sometimes) | |
const std::chrono::microseconds threshold( 50000 ); | |
std::for_each( double_exponential_filter.begin(), double_exponential_filter.end(), | |
[&]( auto& filter ){ | |
std::chrono::microseconds interval( body_frame.get_timestamp() - filter.second.get_timestamp() ); | |
if( interval.count() > threshold.count() ){ | |
double_exponential_filter.erase( filter.first ); | |
} | |
} | |
); | |
// Clear Handle | |
body_frame.reset(); | |
body_capture.reset(); | |
skeleton_image.reset(); | |
// Show Image | |
cv::imshow( "skeleton", skeleton ); | |
const int32_t key = cv::waitKey( 30 ); | |
if( key == 'q' ){ | |
break; | |
} | |
} | |
// Close Tracker | |
tracker.destroy(); | |
// Close Transformation | |
transformation.destroy(); | |
// 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