Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_watershed.cpp
16344 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "test_precomp.hpp"
44
45
namespace opencv_test { namespace {
46
47
class CV_WatershedTest : public cvtest::BaseTest
48
{
49
public:
50
CV_WatershedTest();
51
~CV_WatershedTest();
52
protected:
53
void run(int);
54
};
55
56
CV_WatershedTest::CV_WatershedTest() {}
57
CV_WatershedTest::~CV_WatershedTest() {}
58
59
void CV_WatershedTest::run( int /* start_from */)
60
{
61
string exp_path = string(ts->get_data_path()) + "watershed/wshed_exp.png";
62
Mat exp = imread(exp_path, 0);
63
Mat orig = imread(string(ts->get_data_path()) + "inpaint/orig.png");
64
FileStorage fs(string(ts->get_data_path()) + "watershed/comp.xml", FileStorage::READ);
65
66
if (orig.empty() || !fs.isOpened())
67
{
68
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
69
return;
70
}
71
72
CvSeq* cnts = (CvSeq*)fs["contours"].readObj();
73
74
Mat markers(orig.size(), CV_32SC1);
75
markers = Scalar(0);
76
IplImage iplmrks = cvIplImage(markers);
77
78
vector<unsigned char> colors(1);
79
for(int i = 0; cnts != 0; cnts = cnts->h_next, ++i )
80
{
81
cvDrawContours( &iplmrks, cnts, cvScalar(Scalar::all(i + 1)), cvScalar(Scalar::all(i + 1)), -1, CV_FILLED);
82
Point* p = (Point*)cvGetSeqElem(cnts, 0);
83
84
//expected image was added with 1 in order to save to png
85
//so now we substract 1 to get real color
86
if(!exp.empty())
87
colors.push_back(exp.ptr(p->y)[p->x] - 1);
88
}
89
fs.release();
90
const int compNum = (int)(colors.size() - 1);
91
92
watershed(orig, markers);
93
94
for(int j = 0; j < markers.rows; ++j)
95
{
96
int* line = markers.ptr<int>(j);
97
for(int i = 0; i < markers.cols; ++i)
98
{
99
int& pixel = line[i];
100
101
if (pixel == -1) // border
102
continue;
103
104
if (pixel <= 0 || pixel > compNum)
105
continue; // bad result, doing nothing and going to get error latter;
106
107
// repaint in saved color to compare with expected;
108
if(!exp.empty())
109
pixel = colors[pixel];
110
}
111
}
112
113
Mat markers8U;
114
markers.convertTo(markers8U, CV_8U, 1, 1);
115
116
if( exp.empty() || orig.size() != exp.size() )
117
{
118
imwrite(exp_path, markers8U);
119
exp = markers8U;
120
}
121
122
ASSERT_EQ(0, cvtest::norm(markers8U, exp, NORM_INF));
123
}
124
125
TEST(Imgproc_Watershed, regression) { CV_WatershedTest test; test.safe_run(); }
126
127
}} // namespace
128
129