Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cv::rgbd::Odometry::compute

Tags:

c++

opencv

ros

I am using C++ and OpenCV with combination of ROS. I use live images from my camera (intel realsense R200). I get depth and RGB images from my camera. In my c++ code I want to use these images to get odometry data and make a trajectory out of it.

I am trying to use the "cv::rgbd::Odometry::compute" function for odometry, but I always get false as return value ("isSuccess" value in the code is always 0). But I dont know which part I am doing wrong.

I read my images from camera using ROS and then in the Callback function, first I convert all images to grayscale and then I use Surf function for detecting the features. Then I want to use "compute" to get the transformation between current and previous frame.

As far as I understood "Rt" and "inintRt" are the output of function so it is enough to cunstruct them with correct size.

Can anyone see the problem? Am I missing anything?

boost::shared_ptr<rgbd::Odometry> odom;

Mat Rt = Mat(4,4, CV_64FC1);
Mat initRt = Mat(4,4, CV_64FC1);

Mat prevFtrM; //mask Matrix of previous image
Mat currFtrM; //mask Matrix of current image
Mat tempFtrM;

Mat imgprev;// previous depth image
Mat imgcurr;// current depth image

Mat imgprevC;// previous colored image
Mat imgcurrC;// current colored image


void Surf(Mat img) // detect features of the img and fill currFtrM
{
    int minHessian = 400;
    Ptr<SURF> detector = SURF::create( minHessian );
    vector<KeyPoint> keypoints_1;

    currFtrM = Mat::zeros(img.size(), CV_8U);  // type of mask is CV_8U
    Mat roi(currFtrM, cv::Rect(0,0,img.size().width,img.size().height));
    roi = Scalar(255, 255, 255);

    detector->detect( img, keypoints_1, currFtrM );

    Mat img_keypoints_1;
    drawKeypoints( img, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
    //-- Show detected (drawn) keypoints
    imshow("Keypoints 1", img_keypoints_1 );
}


void Callback(const sensor_msgs::ImageConstPtr& clr, const sensor_msgs::ImageConstPtr& dpt)
{

    if(!imgcurr.data || !imgcurrC.data) // first frame
    {
        // depth image
        imgcurr = cv_bridge::toCvShare(dpt, sensor_msgs::image_encodings::TYPE_32FC1)->image;

        // colored image
        imgcurrC = cv_bridge::toCvShare(clr, "bgr8")->image;
        cvtColor(imgcurrC, imgcurrC, COLOR_BGR2GRAY);

        //find features in the image
        Surf(imgcurrC);
        prevFtrM = currFtrM;

        //scale color image to size of depth image
        resize(imgcurrC,imgcurrC, imgcurr.size());

        return;
    }

    odom = boost::make_shared<rgbd::RgbdOdometry>(imgcurrC, Odometry::DEFAULT_MIN_DEPTH(), Odometry::DEFAULT_MAX_DEPTH(),               Odometry::DEFAULT_MAX_DEPTH_DIFF(), std::vector< int >(), std::vector< float >(),               Odometry::DEFAULT_MAX_POINTS_PART(), Odometry::RIGID_BODY_MOTION);



    // depth image
    imgprev = imgcurr;
    imgcurr = cv_bridge::toCvShare(dpt, sensor_msgs::image_encodings::TYPE_32FC1)->image;

    // colored image
    imgprevC = imgcurrC;
    imgcurrC = cv_bridge::toCvShare(clr, "bgr8")->image;
    cvtColor(imgcurrC, imgcurrC, COLOR_BGR2GRAY);

    //scale color image to size of depth image
    resize(imgcurrC,imgcurrC, imgcurr.size());
    cv::imshow("Color resized", imgcurrC);

    tempFtrM = currFtrM;
    //detect new features in imgcurrC and save in a vector<Point2f>
    Surf( imgcurrC);

    prevFtrM = tempFtrM;

    //set camera matrix to identity matrix
    float vals[] = {619.137635, 0., 304.793791, 0., 625.407449, 223.984030, 0., 0., 1.};

    const Mat cameraMatrix = Mat(3, 3, CV_32FC1, vals);
    odom->setCameraMatrix(cameraMatrix);


    bool isSuccess = odom->compute( imgprevC, imgprev, prevFtrM,  imgcurrC, imgcurr, currFtrM, Rt, initRt );

    if(isSuccess)
        cout << "isSuccess   " << isSuccess << endl;

}

Update: I calibrated my camera and replaced the camera matrix with real values.

like image 830
dieKoderin Avatar asked Jan 11 '18 12:01

dieKoderin


1 Answers

A bit late, but could be still useful for someone.

It seems to me that you are missing extrinsic calibration from the calculation: in my experiments, R200 has a translation component between RGB and Depth camera that you are not taking into account. Furthermore, looking at the camera parameters, Depth and RGB have different intrinsics and the Color frame has a MODIFIED_BROWN_CONRADY lens distortion (but this is minimal), are you undistorting that?

Obviously, I can be wrong if you already do all those steps and save registered RGB and Depth on files.

like image 155
Mouze Avatar answered Nov 12 '22 23:11

Mouze