使用OpenCV和OpenNI测量2点之间的距离

时间:2012-08-14 23:17:54

标签: c++ opencv kinect openni

我正在使用OpenCV 2.4.0中的内置OpenNI访问,我正在尝试测量深度图中两点之间的距离。到目前为止我试过这个:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

Point startPt(0,0);
Point endPt(0,0);

void onMouse( int event, int x, int y, int flags, void* )
{
    if( event == CV_EVENT_LBUTTONUP) startPt = Point(x,y);
    if( event == CV_EVENT_RBUTTONUP) endPt   = Point(x,y);
}

int main( int argc, char* argv[] ){
    VideoCapture capture;
    capture.open(CV_CAP_OPENNI);
    //capture.set( CV_CAP_PROP_OPENNI_REGISTRATION , 0);
    unsigned t0=clock();
    if( !capture.isOpened() ){
        cout << "Can not open a capture object." << endl;
        return -1;
    }
    unsigned elapsed=clock()-t0;
    cout << "initialized in "<< elapsed <<" s. ready!" << endl;
    namedWindow( "depth", 1 );
    setMouseCallback( "depth", onMouse, 0 );
    for(;;){
        Mat depthMap;
        if( !capture.grab() ){
            cout << "Can not grab images." << endl;
            return -1;
        }else{
            Mat show,real,valid;
            if( capture.retrieve( depthMap, CV_CAP_OPENNI_DEPTH_MAP ) ){
                depthMap.convertTo( show, CV_8UC1, 0.05f);
            }
            capture.retrieve(valid,CV_CAP_OPENNI_VALID_DEPTH_MASK);
            if( capture.retrieve(real, CV_CAP_OPENNI_POINT_CLOUD_MAP)){
                unsigned int sp = valid.at<unsigned char>(startPt.x, startPt.y);
                unsigned int ep = valid.at<unsigned char>(endPt.x, endPt.y);
                if(sp == 255 && ep == 255){
                    Vec3f s = real.at<Vec3f>(startPt.x, startPt.y);
                    Vec3f e = real.at<Vec3f>(endPt.x, endPt.y);
                    float dx = e[0]-s[0];
                    float dy = e[1]-s[1];
                    float dz = e[2]-s[2];
                    float dist = sqrt(dx*dx + dy*dy + dz*dz);
                    putText(show,format("distance: %f m\n",dist),Point(10,10),FONT_HERSHEY_PLAIN,1,Scalar(255));
                }
            }
            circle(show,startPt,3,Scalar(255),3);
            circle(show,endPt,3,Scalar(192),3);
            line(show,startPt,endPt,Scalar(128));
            imshow("depth",show);
        }
        if( waitKey( 30 ) >= 0 )    break;
    }

}

但我有几个问题:

  1. 除非我首先在程序中检查有效深度掩码中的值 有时会崩溃(我想是因为数据不好)
  2. 据我所知,点云图以米为单位返回XYZ数据(CV_32FC3),但我在屏幕上看到的数字看起来不对。
  3. 我是否使用正确的方法来检索xyz值并计算距离?

    我可能遗失的任何细节?

1 个答案:

答案 0 :(得分:4)

  1. 我运行了代码,但即使我注释掉if(sp == 255 && ep == 255),也无法让它崩溃。经常发生吗?我没有使用OpenNI驱动程序,但在Microsoft驱动程序中,例如也存在无效值(例如表面是镜面反射时)。 “太近”或“太远”也有不同的值。可能是OpenNI在这样的情况下返回垃圾吗?

  2. 尝试切换x和y:

    Vec3f s = real.at<Vec3f>(startPt.y, startPt.x);        
    Vec3f e = real.at<Vec3f>(endPt.y, endPt.x);        
    
  3. (因此:)

    unsigned int sp = valid.at<unsigned char>(startPt.y, startPt.x);
    unsigned int ep = valid.at<unsigned char>(endPt.y, endPt.x);
    

    语法为cv::Mat::at<_Tp>(int y, int x)或者它是否有助于您更好cv::Mat::at<_Tp>(int row, int column) 我测量了一些距离,它对我来说很好。

    另外,你实际上计算了你得到的每一帧的3D点数和距离,这是故意的吗?因为即使您在图像中选择了两个2D点,​​用于计算距离的相应3D点也可能因噪声而在每帧中发生变化。