test_tvl1optflow.cpp 5.66 KB
Newer Older
wester 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 36 37 38 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's 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.
//
//   * The name of Intel Corporation may not 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 Intel Corporation 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.
//
//M*/

#include "test_precomp.hpp"
#include <fstream>

using namespace std;
using namespace cv;
using namespace cvtest;

//#define DUMP

namespace
{
    // first four bytes, should be the same in little endian
    const float FLO_TAG_FLOAT = 202021.25f;  // check for this when READING the file

#ifdef DUMP
    // binary file format for flow data specified here:
    // http://vision.middlebury.edu/flow/data/
    void writeOpticalFlowToFile(const Mat_<Point2f>& flow, const string& fileName)
    {
        const char FLO_TAG_STRING[] = "PIEH";    // use this when WRITING the file
        ofstream file(fileName.c_str(), ios_base::binary);

        file << FLO_TAG_STRING;

        file.write((const char*) &flow.cols, sizeof(int));
        file.write((const char*) &flow.rows, sizeof(int));

        for (int i = 0; i < flow.rows; ++i)
        {
            for (int j = 0; j < flow.cols; ++j)
            {
                const Point2f u = flow(i, j);

                file.write((const char*) &u.x, sizeof(float));
                file.write((const char*) &u.y, sizeof(float));
            }
        }
    }
#endif

    // binary file format for flow data specified here:
    // http://vision.middlebury.edu/flow/data/
    void readOpticalFlowFromFile(Mat_<Point2f>& flow, const string& fileName)
    {
        ifstream file(fileName.c_str(), ios_base::binary);

        float tag;
        file.read((char*) &tag, sizeof(float));
        CV_Assert( tag == FLO_TAG_FLOAT );

        Size size;

        file.read((char*) &size.width, sizeof(int));
        file.read((char*) &size.height, sizeof(int));

        flow.create(size);

        for (int i = 0; i < flow.rows; ++i)
        {
            for (int j = 0; j < flow.cols; ++j)
            {
                Point2f u;

                file.read((char*) &u.x, sizeof(float));
                file.read((char*) &u.y, sizeof(float));

                flow(i, j) = u;
            }
        }
        file.close();
    }

    bool isFlowCorrect(Point2f u)
    {
        return !cvIsNaN(u.x) && !cvIsNaN(u.y) && (fabs(u.x) < 1e9) && (fabs(u.y) < 1e9);
    }

    double calcRMSE(const Mat_<Point2f>& flow1, const Mat_<Point2f>& flow2)
    {
        double sum = 0.0;
        int counter = 0;

        for (int i = 0; i < flow1.rows; ++i)
        {
            for (int j = 0; j < flow1.cols; ++j)
            {
                const Point2f u1 = flow1(i, j);
                const Point2f u2 = flow2(i, j);

                if (isFlowCorrect(u1) && isFlowCorrect(u2))
                {
                    const Point2f diff = u1 - u2;
                    sum += diff.ddot(diff);
                    ++counter;
                }
            }
        }
        return sqrt(sum / (1e-9 + counter));
    }
}

TEST(Video_calcOpticalFlowDual_TVL1, Regression)
{
    const double MAX_RMSE = 0.03;

    const string frame1_path = TS::ptr()->get_data_path() + "optflow/RubberWhale1.png";
    const string frame2_path = TS::ptr()->get_data_path() + "optflow/RubberWhale2.png";
    const string gold_flow_path = TS::ptr()->get_data_path() + "optflow/tvl1_flow.flo";

    Mat frame1 = imread(frame1_path, IMREAD_GRAYSCALE);
    Mat frame2 = imread(frame2_path, IMREAD_GRAYSCALE);
    ASSERT_FALSE(frame1.empty());
    ASSERT_FALSE(frame2.empty());

    Mat_<Point2f> flow;
    Ptr<DenseOpticalFlow> tvl1 = createOptFlow_DualTVL1();

    tvl1->calc(frame1, frame2, flow);

#ifdef DUMP
    writeOpticalFlowToFile(flow, gold_flow_path);
#else
    Mat_<Point2f> gold;
    readOpticalFlowFromFile(gold, gold_flow_path);

    ASSERT_EQ(gold.rows, flow.rows);
    ASSERT_EQ(gold.cols, flow.cols);

    double err = calcRMSE(gold, flow);
    EXPECT_LE(err, MAX_RMSE);
#endif
}