人體骨骼座標在彩色圖像中顯示


複製代碼
// YeNite2SimpleUsingOpenCV.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <iostream>

    // OpenCV 頭文件
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>


#include <OpenNI.h>
#include <NiTE.h>

using namespace std;
using namespace openni;
using namespace nite;

int main( int argc, char **argv )
{
    // 初始化OpenNI
    OpenNI::initialize();

    // 打開Kinect設備
    Device  mDevice;
    mDevice.open( ANY_DEVICE );

    // 創建深度數據流
    VideoStream mDepthStream;
    mDepthStream.create( mDevice, SENSOR_DEPTH );

    // 設置VideoMode模式
    VideoMode mDepthMode;
    mDepthMode.setResolution( 640, 480 );
    mDepthMode.setFps( 30 );
    mDepthMode.setPixelFormat( PIXEL_FORMAT_DEPTH_1_MM );
    mDepthStream.setVideoMode(mDepthMode);

    // 同樣的設置彩色數據流
    VideoStream mColorStream;
    mColorStream.create( mDevice, SENSOR_COLOR );
    // 設置VideoMode模式
    VideoMode mColorMode;
    mColorMode.setResolution( 640, 480 );
    mColorMode.setFps( 30 );
    mColorMode.setPixelFormat( PIXEL_FORMAT_RGB888 );
    mColorStream.setVideoMode( mColorMode);

    // 設置深度圖像映射到彩色圖像
    mDevice.setImageRegistrationMode( IMAGE_REGISTRATION_DEPTH_TO_COLOR );

    // 爲了得到骨骼數據,先初始化NiTE
    NiTE::initialize();

    // 創建用戶跟蹤器
    UserTracker mUserTracker;
    mUserTracker.create( &mDevice );

    // Control the smoothing factor of the skeleton joints. Factor should be between 0 (no smoothing at all) and 1 (no movement at all)
    mUserTracker.setSkeletonSmoothingFactor( 0.1f );

    // 創建User彩色圖像顯示
    cv::namedWindow( "User Image",  CV_WINDOW_AUTOSIZE );

    // 環境初始化後,開始獲取深度數據流和彩色數據流
    mDepthStream.start();
    mColorStream.start();

    while( true )
    {
        // 創建OpenCV::Mat,用於顯示彩色數據圖像
        cv::Mat cImageBGR;

        // 讀取彩色圖像數據幀信息流
        VideoFrameRef mColorFrame;
        mColorStream.readFrame( &mColorFrame );

        // 將彩色數據流轉換爲OpenCV格式,記得格式是:CV_8UC3(含R\G\B)
        const cv::Mat mImageRGB( mColorFrame.getHeight(), mColorFrame.getWidth(),
            CV_8UC3, (void*)mColorFrame.getData() );

        // RGB ==> BGR
        cv::cvtColor( mImageRGB, cImageBGR, CV_RGB2BGR );

        // 讀取User用戶數據幀信息流
        UserTrackerFrameRef  mUserFrame;
        mUserTracker.readFrame( &mUserFrame );

        // 得到Users信息
        const nite::Array<UserData>& aUsers = mUserFrame.getUsers();
        for( int i = 0; i < aUsers.getSize(); ++ i )
        {
            const UserData& rUser = aUsers[i];

            // 檢查用戶狀態
            if( rUser.isNew() )
            {
                // 開始對該用戶的骨骼跟蹤
                mUserTracker.startSkeletonTracking( rUser.getId() );
            }

            if( rUser.isVisible() )
            {
                // 得到用戶骨骼數據
                const Skeleton& rSkeleton = rUser.getSkeleton();

                // 檢查骨骼狀態是否爲“跟蹤狀態”
                if( rSkeleton.getState() == SKELETON_TRACKED )
                {
                    // 得到15個骨骼數據
SkeletonJoint aJoints[15]; aJoints[ 0] = rSkeleton.getJoint( JOINT_HEAD ); aJoints[ 1] = rSkeleton.getJoint( JOINT_NECK ); aJoints[ 2] = rSkeleton.getJoint( JOINT_LEFT_SHOULDER ); aJoints[ 3] = rSkeleton.getJoint( JOINT_RIGHT_SHOULDER ); aJoints[ 4] = rSkeleton.getJoint( JOINT_LEFT_ELBOW ); aJoints[ 5] = rSkeleton.getJoint( JOINT_RIGHT_ELBOW ); aJoints[ 6] = rSkeleton.getJoint( JOINT_LEFT_HAND ); aJoints[ 7] = rSkeleton.getJoint( JOINT_RIGHT_HAND ); aJoints[ 8] = rSkeleton.getJoint( JOINT_TORSO ); aJoints[ 9] = rSkeleton.getJoint( JOINT_LEFT_HIP ); aJoints[10] = rSkeleton.getJoint( JOINT_RIGHT_HIP ); aJoints[11] = rSkeleton.getJoint( JOINT_LEFT_KNEE ); aJoints[12] = rSkeleton.getJoint( JOINT_RIGHT_KNEE ); aJoints[13] = rSkeleton.getJoint( JOINT_LEFT_FOOT ); aJoints[14] = rSkeleton.getJoint( JOINT_RIGHT_FOOT ); // 將骨骼3D座標轉換爲深度座標下骨骼位置座標,並保存在數組中 cv::Point2f aPoint[15]; for( int s = 0; s < 15; ++ s ) { const Point3f& rPos = aJoints[s].getPosition(); mUserTracker.convertJointCoordinatesToDepth( rPos.x, rPos.y, rPos.z, &(aPoint[s].x), &(aPoint[s].y) ); } // 在彩色圖像中畫出骨骼間的連接線 cv::line( cImageBGR, aPoint[ 0], aPoint[ 1], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 1], aPoint[ 2], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 1], aPoint[ 3], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 2], aPoint[ 4], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 3], aPoint[ 5], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 4], aPoint[ 6], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 5], aPoint[ 7], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 1], aPoint[ 8], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 8], aPoint[ 9], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 8], aPoint[10], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[ 9], aPoint[11], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[10], aPoint[12], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[11], aPoint[13], cv::Scalar( 255, 0, 0 ), 3 ); cv::line( cImageBGR, aPoint[12], aPoint[14], cv::Scalar( 255, 0, 0 ), 3 ); // 同樣的在彩色圖像中骨骼位置上畫“圓” for( int s = 0; s < 15; ++ s ) { if( aJoints[s].getPositionConfidence() > 0.5 ) cv::circle( cImageBGR, aPoint[s], 3, cv::Scalar( 0, 0, 255 ), 2 ); else cv::circle( cImageBGR, aPoint[s], 3, cv::Scalar( 0, 255, 0 ), 2 ); } } } } // 顯示圖像 cv::imshow( "User Image", cImageBGR ); // 按鍵“q”退出循環 if( cv::waitKey( 1 ) == 'q' ) break; } // 先銷燬User跟蹤器 mUserTracker.destroy(); // 銷燬彩色數據流和深度數據流 mColorStream.destroy(); mDepthStream.destroy(); // 關閉Kinect設備 mDevice.close(); // 關閉NITE和OpenNI環境 NiTE::shutdown(); OpenNI::shutdown(); return 0; }
複製代碼

    效果圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章