Realsense SR300实现检测人手的个数

1.检测视频流中已存在的手的个数

/*
Created by Jinhua Zhao,2017.09.24.
Contact:3210513029@qq.com
vs2017+Opencv3.30+realsense SR300
*/

#include <pxcsensemanager.h>
#include <pxcsession.h>
#include <pxchandconfiguration.h>
#include <pxccursordata.h>
#include <pxchandmodule.h>
#include <pxchanddata.h>
#include <pxcprojection.h>
#include <pxcpowerstate.h>
#include <pxchandcursormodule.h>
#include <pxccursorconfiguration.h>
#include <utilities\pxcsmoother.h>
#include <opencv2\opencv.hpp>
#include <Windows.h>

using namespace std;
using namespace cv;

int main() {
    PXCSenseManager* sm = PXCSenseManager::CreateInstance();
    sm->EnableHand();

    PXCHandModule* handMoudle = sm->QueryHand();
    PXCHandConfiguration* handConfig = handMoudle->CreateActiveConfiguration();

    handConfig->SetTrackingMode(PXCHandData::TrackingModeType::TRACKING_MODE_FULL_HAND);
    handConfig->EnableStabilizer(true);
    handConfig->EnableTrackedJoints(true);
    handConfig->EnableNormalizedJoints(true);
    handConfig->EnableSegmentationImage(true);
    handConfig->ApplyChanges();

    sm->Init();

    PXCHandData* handData = handMoudle->CreateOutput();
    PXCHandData::IHand* ihand[3];
    pxcUID handId[3];
    while (sm->AcquireFrame(true) >= PXC_STATUS_NO_ERROR)
    {
        handData->Update();
        int nHands = handData->QueryNumberOfHands();
        cout << nHands << endl;
        sm->ReleaseFrame();
    }
    
    handData->Release();
    handConfig->Release();
    sm->Close();
    sm->Release();
    return 0;
}

2.用食指控制鼠标运动,其中需要根据要求的手指以及具体应用场景进行更改!

/*
Created by Jinhua Zhao,2017.09.24.
Contact:3210513029@qq.com
vs2017+Opencv3.30+realsense SR300
*/

#include <pxcsensemanager.h>
#include <pxcsession.h>
#include <pxchandconfiguration.h>
#include <pxccursordata.h>
#include <pxchandmodule.h>
#include <pxchanddata.h>
#include <pxcprojection.h>
#include <pxcpowerstate.h>
#include <pxchandcursormodule.h>
#include <pxccursorconfiguration.h>
#include <utilities\pxcsmoother.h>
#include <opencv2\opencv.hpp>
#include <Windows.h>

using namespace std;
using namespace cv;

int main() {
    PXCSenseManager* sm = PXCSenseManager::CreateInstance();
    sm->EnableHand();

    PXCHandModule* handMoudle = sm->QueryHand();
    PXCHandConfiguration* handConfig = handMoudle->CreateActiveConfiguration();

    handConfig->SetTrackingMode(PXCHandData::TrackingModeType::TRACKING_MODE_FULL_HAND);
    handConfig->EnableStabilizer(true);
    handConfig->EnableTrackedJoints(true);
    handConfig->EnableNormalizedJoints(true);
    handConfig->EnableSegmentationImage(true);
    handConfig->ApplyChanges();

    sm->Init();

    PXCHandData* handData = handMoudle->CreateOutput();
    PXCHandData::IHand* ihand[3];
    PXCHandData::JointData jointdata;
    PXCHandData::FingerData fingerdata;
    PXCHandData::JointType jointtype=PXCHandData::JointType::JOINT_CENTER;
    PXCHandData::FingerType fingertye = PXCHandData::FingerType::FINGER_INDEX;

    pxcUID handId[3];
    int nHands;
    time_t time1, time2;
    for (size_t i = 0; i < 3; i++) {
        ihand[i] = nullptr;
        handId[i] = 0;
    }

    float before_x, before_y, before_z;
    float now_x, now_y, now_z;
    float delta_x, delta_y, delta_z;
    float tip;

    while (sm->AcquireFrame(true) >= PXC_STATUS_NO_ERROR)
    {
        handData->Update();
        nHands = handData->QueryNumberOfHands();//Query the numbers of the hands.
        if (nHands == 0){ sm->ReleaseFrame(); continue;}
        for (size_t i = 0; i < nHands; i++) //Dealing with the every piece of data of the hand.
        {
            handData->QueryHandId(PXCHandData::ACCESS_ORDER_NEAR_TO_FAR, i, handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]);
            handData->QueryHandDataById(handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN], ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]);

            auto side = ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]->QueryBodySide();

            ihand[side] = ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN];
            handId[side] = handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN];

            ihand[i]->QueryTrackedJoint(jointtype, jointdata);
            ihand[i]->QueryFingerData(fingertye, fingerdata);//The tip of index finger.
            
            before_x = 1000 * jointdata.positionWorld.x;
            before_y = 1000 * jointdata.positionWorld.y;
            before_z = 1000 * jointdata.positionWorld.z;
            delta_x = now_x - before_x;
            delta_y = now_y - before_y;
            delta_z = now_z - before_z;
            now_x = before_x;
            now_y = before_y;
            now_z = before_z;

            tip = fingerdata.foldedness;
            cout << tip << endl;
            if (tip<10)
            {
                mouse_event(MOUSEEVENTF_LEFTDOWN, delta_x, delta_y, 0, 0);
                Sleep(500);
            }
            else
            mouse_event(MOUSEEVENTF_MOVE|MOUSEEVENTF_LEFTUP|MOUSEEVENTF_RIGHTUP, delta_x, delta_y, 0, 0);
            cout << "x=" << delta_x << endl;
            cout << "y=" << delta_y << endl;
            cout << "z=" << delta_z << endl;

        }
        sm->ReleaseFrame();
    }
    handData->Release();
    handConfig->Release();
    sm->Close();
    sm->Release();
    return 0;
}

3.中指控制左鼠标,食指控制右鼠标(测试用的左手,实际左右手都行)

/*
Created by Jinhua Zhao,2017.09.24.
Contact:3210513029@qq.com
vs2017+Opencv3.30+realsense SR300
*/

#include <pxcsensemanager.h>
#include <pxcsession.h>
#include <pxchandconfiguration.h>
#include <pxccursordata.h>
#include <pxchandmodule.h>
#include <pxchanddata.h>
#include <pxcprojection.h>
#include <pxcpowerstate.h>
#include <pxchandcursormodule.h>
#include <pxccursorconfiguration.h>
#include <utilities\pxcsmoother.h>
#include <opencv2\opencv.hpp>
#include <Windows.h>

using namespace std;
using namespace cv;

int main() {
    PXCSenseManager* sm = PXCSenseManager::CreateInstance();
    sm->EnableHand();

    PXCHandModule* handMoudle = sm->QueryHand();
    PXCHandConfiguration* handConfig = handMoudle->CreateActiveConfiguration();

    handConfig->SetTrackingMode(PXCHandData::TrackingModeType::TRACKING_MODE_FULL_HAND);
    handConfig->EnableStabilizer(true);
    handConfig->EnableTrackedJoints(true);
    handConfig->EnableNormalizedJoints(true);
    handConfig->EnableSegmentationImage(true);
    handConfig->ApplyChanges();

    sm->Init();

    PXCHandData* handData = handMoudle->CreateOutput();
    PXCHandData::IHand* ihand[3];
    PXCHandData::JointData jointdata;
    PXCHandData::FingerData fingerdata;
    PXCHandData::FingerData fingerdata1;
    PXCHandData::JointType jointtype=PXCHandData::JointType::JOINT_CENTER;
    PXCHandData::FingerType fingertye = PXCHandData::FingerType::FINGER_INDEX;
    PXCHandData::FingerType fingertye1 = PXCHandData::FingerType::FINGER_MIDDLE;

    pxcUID handId[3];
    int nHands;
    time_t time1, time2;
    for (size_t i = 0; i < 3; i++) {
        ihand[i] = nullptr;
        handId[i] = 0;
    }

    float before_x, before_y, before_z;
    float now_x, now_y, now_z;
    float delta_x, delta_y, delta_z;
    float tip, tip1;

    while (sm->AcquireFrame(true) >= PXC_STATUS_NO_ERROR)
    {
        handData->Update();
        nHands = handData->QueryNumberOfHands();//Query the numbers of the hands.
        if (nHands == 0){ 
            sm->ReleaseFrame();
            SetCursorPos(960,540); 
            continue;
        }
        for (size_t i = 0; i < nHands; i++) //Dealing with the every piece of data of the hand.
        {
            handData->QueryHandId(PXCHandData::ACCESS_ORDER_NEAR_TO_FAR, i, handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]);
            handData->QueryHandDataById(handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN], ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]);

            auto side = ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]->QueryBodySide();

            ihand[side] = ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN];
            handId[side] = handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN];

            ihand[i]->QueryTrackedJoint(jointtype, jointdata);
            ihand[i]->QueryFingerData(fingertye, fingerdata);//The tip of index finger.
            ihand[i]->QueryFingerData(fingertye1, fingerdata1);//The tip of index finger.
            
            before_x = 3000 * jointdata.positionWorld.x;
            before_y = 3000 * jointdata.positionWorld.y;
            //before_z = 1000 * jointdata.positionWorld.z;
            delta_x = now_x - before_x;
            delta_y = now_y - before_y;
            //delta_z = now_z - before_z;
            now_x = before_x;
            now_y = before_y;
            //now_z = before_z;

            tip = fingerdata.foldedness;
            tip1 = fingerdata1.foldedness;
            cout << tip << endl;
            cout << tip1 << endl;
            if (tip1<5)
            {
                if (tip>90)
                {
                    mouse_event(MOUSEEVENTF_LEFTDOWN, delta_x, delta_y, 0, 0);
                    mouse_event(MOUSEEVENTF_LEFTUP, delta_x, delta_y, 0, 0);
                    Sleep(100);
                }
                mouse_event(MOUSEEVENTF_RIGHTDOWN, delta_x, delta_y, 0, 0);
                mouse_event(MOUSEEVENTF_RIGHTUP, delta_x, delta_y, 0, 0);
                Sleep(100);
            }
            mouse_event(MOUSEEVENTF_MOVE, delta_x, delta_y, 0, 0);
            cout << "x=" << delta_x << endl;
            cout << "y=" << delta_y << endl;
            //cout << "z=" << delta_z << endl;
        }
        sm->ReleaseFrame();
    }
    handData->Release();
    handConfig->Release();
    sm->Close();
    sm->Release();
    return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,098评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,213评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,960评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,519评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,512评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,533评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,914评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,804评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,563评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,644评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,350评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,933评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,908评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,146评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,847评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,361评论 2 342

推荐阅读更多精彩内容

  • 陈氏太极拳老架一路讲义 前言 写给喜爱太极拳的武术朋友们 中华武术,源远流长,今虽门派繁多,实一脉相承。太极拳以它...
    阿德乐阅读 5,571评论 0 12
  • 好像是说从今天开始就是猴年马月了吧,我是一觉睡到了猴年马月,也是醉了。感觉好久没睡过这么久了,浑身酸疼,罪过罪过。...
    阿拉斯加南海岸线阅读 200评论 0 0
  • 《跑步三字经》 初跑者 勿心急 讲科学 身心益 晚早睡 不熬夜 晨早起 跑步去 刚开始 靠毅力 时间长 会痴迷 周...
    李春阳Maxwell阅读 712评论 0 0
  • 缘分,让他们相遇;所谓的天意让他拔出了那把剑。就在那一秒他爱上了她,并许下了诺言,曾经有一份真挚的爱情摆在我...
    白蔷薇12阅读 174评论 0 0