test_mser.cpp 6.94 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
/*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.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., 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 the copyright holders 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 <vector>
#include <string>
using namespace std;
using namespace cv;

wester committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63
class CV_MserTest : public cvtest::BaseTest
{
public:
    CV_MserTest();
protected:
    void run(int);
    int LoadBoxes(const char* path, vector<CvBox2D>& boxes);
    int SaveBoxes(const char* path, const vector<CvBox2D>& boxes);
    int CompareBoxes(const vector<CvBox2D>& boxes1,const vector<CvBox2D>& boxes2, float max_rel_diff = 0.01f);
};

CV_MserTest::CV_MserTest()
{
}
wester committed
64

wester committed
65
int CV_MserTest::LoadBoxes(const char* path, vector<CvBox2D>& boxes)
wester committed
66
{
wester committed
67 68 69 70 71 72 73 74 75
    boxes.clear();
    FILE* f = fopen(path,"r");

    if (f==NULL)
    {
        return 0;
    }

    while (!feof(f))
wester committed
76
    {
wester committed
77 78 79 80
        CvBox2D box;
        int values_read = fscanf(f,"%f,%f,%f,%f,%f\n",&box.angle,&box.center.x,&box.center.y,&box.size.width,&box.size.height);
        CV_Assert(values_read == 5);
        boxes.push_back(box);
wester committed
81
    }
wester committed
82 83
    fclose(f);
    return 1;
wester committed
84 85
}

wester committed
86
int CV_MserTest::SaveBoxes(const char* path, const vector<CvBox2D>& boxes)
wester committed
87
{
wester committed
88 89
    FILE* f = fopen(path,"w");
    if (f==NULL)
wester committed
90
    {
wester committed
91 92 93 94 95 96 97 98 99
        return 0;
    }
    for (int i=0;i<(int)boxes.size();i++)
    {
        fprintf(f,"%f,%f,%f,%f,%f\n",boxes[i].angle,boxes[i].center.x,boxes[i].center.y,boxes[i].size.width,boxes[i].size.height);
    }
    fclose(f);
    return 1;
}
wester committed
100

wester committed
101 102 103 104
int CV_MserTest::CompareBoxes(const vector<CvBox2D>& boxes1,const vector<CvBox2D>& boxes2, float max_rel_diff)
{
    if (boxes1.size() != boxes2.size())
        return 0;
wester committed
105

wester committed
106
    for (int i=0; i<(int)boxes1.size();i++)
wester committed
107
    {
wester committed
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
        float rel_diff;
        if (!((boxes1[i].angle == 0.0f) && (abs(boxes2[i].angle) < max_rel_diff)))
        {
            float angle_diff = (float)fmod(boxes1[i].angle - boxes2[i].angle, 180);
            // for angular correctness, it makes no sense to use a "relative" error.
            // a 1-degree error around 5 degrees is equally bas as around 250 degrees.
            // in correct cases, angle_diff can now be a bit above 0 or a bit below 180
            if (angle_diff > 90.0f)
            {
                    angle_diff -= 180.0f;
            }
            rel_diff = (float)fabs(angle_diff);
            if (rel_diff > max_rel_diff)
                return i;
        }

        if (!((boxes1[i].center.x == 0.0f) && (abs(boxes2[i].center.x) < max_rel_diff)))
        {
            rel_diff = abs(boxes1[i].center.x-boxes2[i].center.x)/abs(boxes1[i].center.x);
            if (rel_diff > max_rel_diff)
                return i;
        }

        if (!((boxes1[i].center.y == 0.0f) && (abs(boxes2[i].center.y) < max_rel_diff)))
        {
            rel_diff = abs(boxes1[i].center.y-boxes2[i].center.y)/abs(boxes1[i].center.y);
            if (rel_diff > max_rel_diff)
                return i;
        }
        if (!((boxes1[i].size.width == 0.0f) && (abs(boxes2[i].size.width) < max_rel_diff)))
        {
            rel_diff = abs(boxes1[i].size.width-boxes2[i].size.width)/abs(boxes1[i].size.width);
            if (rel_diff > max_rel_diff)
            return i;
        }
wester committed
143

wester committed
144
        if (!((boxes1[i].size.height == 0.0f) && (abs(boxes2[i].size.height) < max_rel_diff)))
wester committed
145
        {
wester committed
146 147 148 149 150
            rel_diff = abs(boxes1[i].size.height-boxes2[i].size.height)/abs(boxes1[i].size.height);
            if (rel_diff > max_rel_diff)
                return i;
        }
    }
wester committed
151

wester committed
152 153
    return -1;
}
wester committed
154

wester committed
155 156 157
void CV_MserTest::run(int)
{
    string image_path = string(ts->get_data_path()) + "mser/puzzle.png";
wester committed
158

wester committed
159 160 161 162 163 164 165
    Mat img = imread( image_path );
    if (img.empty())
    {
        ts->printf( cvtest::TS::LOG, "Unable to open image mser/puzzle.png\n");
        ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
        return;
    }
wester committed
166

wester committed
167 168 169 170
    Mat yuv;
    cvtColor(img, yuv, COLOR_BGR2YCrCb);
    vector<vector<Point> > msers;
    MSER()(yuv, msers);
wester committed
171

wester committed
172 173 174 175 176 177 178 179
    vector<CvBox2D> boxes;
    vector<CvBox2D> boxes_orig;
    for ( size_t i = 0; i < msers.size(); i++ )
    {
        RotatedRect box = fitEllipse(msers[i]);
        box.angle=(float)CV_PI/2-box.angle;
        boxes.push_back(box);
    }
wester committed
180

wester committed
181 182
    string boxes_path = string(ts->get_data_path()) + "mser/boxes.txt";
    string calc_boxes_path = string(ts->get_data_path()) + "mser/boxes.calc.txt";
wester committed
183

wester committed
184 185 186 187 188 189 190
    if (!LoadBoxes(boxes_path.c_str(),boxes_orig))
    {
        SaveBoxes(boxes_path.c_str(),boxes);
        ts->printf( cvtest::TS::LOG, "Unable to open data file mser/boxes.txt\n");
        ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
        return;
    }
wester committed
191

wester committed
192 193 194 195 196 197 198 199 200 201 202
    const float dissimularity = 0.01f;
    int n_box = CompareBoxes(boxes_orig,boxes,dissimularity);
    if (n_box < 0)
    {
        ts->set_failed_test_info(cvtest::TS::OK);
    }
    else
    {
        SaveBoxes(calc_boxes_path.c_str(), boxes);
        ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
        ts->printf( cvtest::TS::LOG, "Incorrect correspondence in box %d\n",n_box);
wester committed
203 204
    }
}
wester committed
205 206

TEST(Features2d_MSER, DISABLED_regression) { CV_MserTest test; test.safe_run(); }