test_odometry.cpp 12.2 KB
Newer Older
Vincent Rabaud's avatar
Vincent Rabaud committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2012, Willow Garage, Inc.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 */

36 37
#include "test_precomp.hpp"

38
namespace opencv_test { namespace {
Vincent Rabaud's avatar
Vincent Rabaud committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

#define SHOW_DEBUG_LOG     0
#define SHOW_DEBUG_IMAGES  0

static 
void warpFrame(const Mat& image, const Mat& depth, const Mat& rvec, const Mat& tvec, const Mat& K,
               Mat& warpedImage, Mat& warpedDepth)
{
    CV_Assert(!image.empty());
    CV_Assert(image.type() == CV_8UC1);
    
    CV_Assert(depth.size() == image.size());
    CV_Assert(depth.type() == CV_32FC1);
    
    CV_Assert(!rvec.empty());
    CV_Assert(rvec.total() == 3);
    CV_Assert(rvec.type() == CV_64FC1);
    
    CV_Assert(!tvec.empty());
    CV_Assert(tvec.size() == Size(1, 3));
    CV_Assert(tvec.type() == CV_64FC1);
    
    warpedImage.create(image.size(), CV_8UC1);
    warpedImage = Scalar(0);
    warpedDepth.create(image.size(), CV_32FC1);
    warpedDepth = Scalar(FLT_MAX);
    
    Mat cloud;
    depthTo3d(depth, K, cloud);
    Mat Rt = Mat::eye(4, 4, CV_64FC1);
    {
        Mat R, dst;
        Rodrigues(rvec, R);
        
        dst = Rt(Rect(0,0,3,3));
        R.copyTo(dst);
        
        dst = Rt(Rect(3,0,1,3));
        tvec.copyTo(dst);
    }
    Mat warpedCloud, warpedImagePoints;
    perspectiveTransform(cloud, warpedCloud, Rt);
    projectPoints(warpedCloud.reshape(3, 1), Mat(3,1,CV_32FC1, Scalar(0)), Mat(3,1,CV_32FC1, Scalar(0)), K, Mat(1,5,CV_32FC1, Scalar(0)), warpedImagePoints);
    warpedImagePoints = warpedImagePoints.reshape(2, cloud.rows);
    Rect r(0, 0, image.cols, image.rows);
    for(int y = 0; y < cloud.rows; y++)
    {
        for(int x = 0; x < cloud.cols; x++)
        {
            Point p = warpedImagePoints.at<Point2f>(y,x);
            if(r.contains(p))
            {
                float curDepth = warpedDepth.at<float>(p.y, p.x);
                float newDepth = warpedCloud.at<Point3f>(y, x).z;
                if(newDepth < curDepth && newDepth > 0)
                {
                    warpedImage.at<uchar>(p.y, p.x) = image.at<uchar>(y,x);
                    warpedDepth.at<float>(p.y, p.x) = newDepth;
                }
            }
        }
    }
    warpedDepth.setTo(std::numeric_limits<float>::quiet_NaN(), warpedDepth > 100);
}

static
void dilateFrame(Mat& image, Mat& depth)
{
    CV_Assert(!image.empty());
    CV_Assert(image.type() == CV_8UC1);

    CV_Assert(!depth.empty());
    CV_Assert(depth.type() == CV_32FC1);
    CV_Assert(depth.size() == image.size());

    Mat mask(image.size(), CV_8UC1, Scalar(255));
    for(int y = 0; y < depth.rows; y++)
        for(int x = 0; x < depth.cols; x++)
            if(cvIsNaN(depth.at<float>(y,x)) || depth.at<float>(y,x) > 10 || depth.at<float>(y,x) <= FLT_EPSILON)
                mask.at<uchar>(y,x) = 0;

    image.setTo(255, ~mask);
    Mat minImage;
    erode(image, minImage, Mat());

    image.setTo(0, ~mask);
    Mat maxImage;
    dilate(image, maxImage, Mat());

    depth.setTo(FLT_MAX, ~mask);
    Mat minDepth;
    erode(depth, minDepth, Mat());

    depth.setTo(0, ~mask);
    Mat maxDepth;
    dilate(depth, maxDepth, Mat());

    Mat dilatedMask;
    dilate(mask, dilatedMask, Mat(), Point(-1,-1), 1);
    for(int y = 0; y < depth.rows; y++)
        for(int x = 0; x < depth.cols; x++)
            if(!mask.at<uchar>(y,x) && dilatedMask.at<uchar>(y,x))
            {
                image.at<uchar>(y,x) = static_cast<uchar>(0.5f * (static_cast<float>(minImage.at<uchar>(y,x)) +
                                                                  static_cast<float>(maxImage.at<uchar>(y,x))));
                depth.at<float>(y,x) = 0.5f * (minDepth.at<float>(y,x) + maxDepth.at<float>(y,x));
            }
}

class CV_OdometryTest : public cvtest::BaseTest
{
public:
151
    CV_OdometryTest(const Ptr<Odometry>& _odometry,
Vincent Rabaud's avatar
Vincent Rabaud committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                    double _maxError1, 
                    double _maxError5) :
        odometry(_odometry), 
        maxError1(_maxError1), 
        maxError5(_maxError5) {}

protected:
    bool readData(Mat& image, Mat& depth) const;
    static void generateRandomTransformation(Mat& R, Mat& t);
    
    virtual void run(int);

    Ptr<Odometry> odometry;
    double maxError1;
    double maxError5;
};

bool CV_OdometryTest::readData(Mat& image, Mat& depth) const
{
171 172
    std::string imageFilename = ts->get_data_path() + "rgbd/rgb.png";
    std::string depthFilename = ts->get_data_path() + "rgbd/depth.png";
Vincent Rabaud's avatar
Vincent Rabaud committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    
    image = imread(imageFilename,  0);
    depth = imread(depthFilename, -1);
    
    if(image.empty())
    {
        ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imageFilename.c_str() );
        ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
        return false;
    }
    if(depth.empty())
    {
        ts->printf( cvtest::TS::LOG, "Depth %s can not be read.\n", depthFilename.c_str() );
        ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
        ts->set_gtest_status();
        return false;
    }
    
Ilya Lavrenov's avatar
Ilya Lavrenov committed
191
    CV_DbgAssert(image.type() == CV_8UC1);
Vincent Rabaud's avatar
Vincent Rabaud committed
192 193 194 195 196 197 198 199 200 201 202 203 204
    CV_DbgAssert(depth.type() == CV_16UC1);
    {
        Mat depth_flt;
        depth.convertTo(depth_flt, CV_32FC1, 1.f/5000.f);
        depth_flt.setTo(std::numeric_limits<float>::quiet_NaN(), depth_flt < FLT_EPSILON);
        depth = depth_flt;
    }
    
    return true;
}

void CV_OdometryTest::generateRandomTransformation(Mat& rvec, Mat& tvec)
{
205
    const float maxRotation = (float)(3.f / 180.f * CV_PI); //rad
Vincent Rabaud's avatar
Vincent Rabaud committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    const float maxTranslation = 0.02f; //m

    RNG& rng = theRNG();
    rvec.create(3, 1, CV_64FC1);
    tvec.create(3, 1, CV_64FC1);

    randu(rvec, Scalar(-1000), Scalar(1000));
    normalize(rvec, rvec, rng.uniform(0.007f, maxRotation));

    randu(tvec, Scalar(-1000), Scalar(1000));
    normalize(tvec, tvec, rng.uniform(0.007f, maxTranslation));
}

void CV_OdometryTest::run(int)
{
    float fx = 525.0f, // default
          fy = 525.0f,
          cx = 319.5f,
          cy = 239.5f;
    Mat K = Mat::eye(3,3,CV_32FC1);
    {
        K.at<float>(0,0) = fx;
        K.at<float>(1,1) = fy;
        K.at<float>(0,2) = cx;
        K.at<float>(1,2) = cy;
    }
    
    Mat image, depth;
    if(!readData(image, depth))
        return;
        
237
    odometry->setCameraMatrix(K);
Vincent Rabaud's avatar
Vincent Rabaud committed
238 239 240 241 242 243 244 245 246 247 248 249
    
    Mat calcRt;
    
    // 1. Try to find Rt between the same frame (try masks also).
    bool isComputed = odometry->compute(image, depth, Mat(image.size(), CV_8UC1, Scalar(255)), 
                                        image, depth, Mat(image.size(), CV_8UC1, Scalar(255)), 
                                        calcRt);
    if(!isComputed)
    {
        ts->printf(cvtest::TS::LOG, "Can not find Rt between the same frame");
        ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
    }
250
    double diff = cv::norm(calcRt, Mat::eye(4,4,CV_64FC1));
Vincent Rabaud's avatar
Vincent Rabaud committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    if(diff > DBL_EPSILON)
    {
        ts->printf(cvtest::TS::LOG, "Incorrect transformation between the same frame (not the identity matrix), diff = %f", diff);
        ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
    }
    
    // 2. Generate random rigid body motion in some ranges several times (iterCount). 
    // On each iteration an input frame is warped using generated transformation.
    // Odometry is run on the following pair: the original frame and the warped one. 
    // Comparing a computed transformation with an applied one we compute 2 errors:
    // better_1time_count - count of poses which error is less than ground thrush pose,
    // better_5times_count - count of poses which error is 5 times less than ground thrush pose.
    int iterCount = 100;
    int better_1time_count = 0; 
    int better_5times_count = 0;
    for(int iter = 0; iter < iterCount; iter++)
    {
        Mat rvec, tvec;
        generateRandomTransformation(rvec, tvec);
        Mat warpedImage, warpedDepth;
        warpFrame(image, depth, rvec, tvec, K, warpedImage, warpedDepth);
        dilateFrame(warpedImage, warpedDepth); // due to inaccuracy after warping
        
        isComputed = odometry->compute(image, depth, Mat(), warpedImage, warpedDepth, Mat(), calcRt);
        if(!isComputed)
            continue;
                
        Mat calcR = calcRt(Rect(0,0,3,3)), calcRvec;
        Rodrigues(calcR, calcRvec);
        calcRvec = calcRvec.reshape(rvec.channels(), rvec.rows);
        Mat calcTvec = calcRt(Rect(3,0,1,3));
282

Vincent Rabaud's avatar
Vincent Rabaud committed
283 284 285 286 287 288 289 290 291
#if SHOW_DEBUG_IMAGES
        imshow("image", image);
        imshow("warpedImage", warpedImage);
        Mat resultImage, resultDepth;
        warpFrame(image, depth, calcRvec, calcTvec, K, resultImage, resultDepth);
        imshow("resultImage", resultImage);
        waitKey();
#endif

292

Vincent Rabaud's avatar
Vincent Rabaud committed
293
        // compare rotation
294 295 296 297
        double rdiffnorm = cv::norm(rvec - calcRvec),
               rnorm = cv::norm(rvec);
        double tdiffnorm = cv::norm(tvec - calcTvec),
               tnorm = cv::norm(tvec);
Vincent Rabaud's avatar
Vincent Rabaud committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        if(rdiffnorm < rnorm &&  tdiffnorm < tnorm)
            better_1time_count++;
        if(5. * rdiffnorm < rnorm && 5 * tdiffnorm < tnorm)
            better_5times_count++;

#if SHOW_DEBUG_LOG
        std::cout << "Iter " << iter << std::endl;
        std::cout << "rdiffnorm " << rdiffnorm << "; rnorm " << rnorm << std::endl;
        std::cout << "tdiffnorm " << tdiffnorm << "; tnorm " << tnorm << std::endl;
        
        std::cout << "better_1time_count " << better_1time_count << "; better_5time_count " << better_5times_count << std::endl;
#endif
    }

    
    if(static_cast<double>(better_1time_count) < maxError1 * static_cast<double>(iterCount))
    {
        ts->printf(cvtest::TS::LOG, "\nIncorrect count of accurate poses [1st case]: %f / %f", static_cast<double>(better_1time_count), maxError1 * static_cast<double>(iterCount));
        ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
    }
    
    if(static_cast<double>(better_5times_count) < maxError5 * static_cast<double>(iterCount))
    {
        ts->printf(cvtest::TS::LOG, "\nIncorrect count of accurate poses [2nd case]: %f / %f", static_cast<double>(better_5times_count), maxError5 * static_cast<double>(iterCount));
        ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
    }
}

/****************************************************************************************\
*                                Tests registrations                                     *
\****************************************************************************************/

TEST(RGBD_Odometry_Rgbd, algorithmic)
{
332
    CV_OdometryTest test(cv::rgbd::Odometry::create("RgbdOdometry"), 0.99, 0.94);
Vincent Rabaud's avatar
Vincent Rabaud committed
333 334 335
    test.safe_run();
}

336
TEST(DISABLED_RGBD_Odometry_ICP, algorithmic)
Vincent Rabaud's avatar
Vincent Rabaud committed
337
{
338
    CV_OdometryTest test(cv::rgbd::Odometry::create("ICPOdometry"), 0.99, 0.99);
Vincent Rabaud's avatar
Vincent Rabaud committed
339 340 341
    test.safe_run();
}

342
TEST(DISABLED_RGBD_Odometry_RgbdICP, algorithmic)
Vincent Rabaud's avatar
Vincent Rabaud committed
343
{
344
    CV_OdometryTest test(cv::rgbd::Odometry::create("RgbdICPOdometry"), 0.99, 0.99);
Vincent Rabaud's avatar
Vincent Rabaud committed
345 346
    test.safe_run();
}
347 348 349


}} // namespace