Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_distancetransform.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
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "test_precomp.hpp"
43
44
namespace opencv_test { namespace {
45
46
class CV_DisTransTest : public cvtest::ArrayTest
47
{
48
public:
49
CV_DisTransTest();
50
51
protected:
52
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
53
double get_success_error_level( int test_case_idx, int i, int j );
54
void run_func();
55
void prepare_to_validation( int );
56
57
void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
58
int prepare_test_case( int test_case_idx );
59
60
int mask_size;
61
int dist_type;
62
int fill_labels;
63
float mask[3];
64
};
65
66
67
CV_DisTransTest::CV_DisTransTest()
68
{
69
test_array[INPUT].push_back(NULL);
70
test_array[OUTPUT].push_back(NULL);
71
test_array[OUTPUT].push_back(NULL);
72
test_array[REF_OUTPUT].push_back(NULL);
73
test_array[REF_OUTPUT].push_back(NULL);
74
optional_mask = false;
75
element_wise_relative_error = true;
76
}
77
78
79
void CV_DisTransTest::get_test_array_types_and_sizes( int test_case_idx,
80
vector<vector<Size> >& sizes, vector<vector<int> >& types )
81
{
82
RNG& rng = ts->get_rng();
83
cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
84
85
types[INPUT][0] = CV_8UC1;
86
types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_32FC1;
87
types[OUTPUT][1] = types[REF_OUTPUT][1] = CV_32SC1;
88
89
if( cvtest::randInt(rng) & 1 )
90
{
91
mask_size = 3;
92
}
93
else
94
{
95
mask_size = 5;
96
}
97
98
dist_type = cvtest::randInt(rng) % 3;
99
dist_type = dist_type == 0 ? CV_DIST_C : dist_type == 1 ? CV_DIST_L1 : CV_DIST_L2;
100
101
// for now, check only the "labeled" distance transform mode
102
fill_labels = 0;
103
104
if( !fill_labels )
105
sizes[OUTPUT][1] = sizes[REF_OUTPUT][1] = cvSize(0,0);
106
}
107
108
109
double CV_DisTransTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
110
{
111
Size sz = test_mat[INPUT][0].size();
112
return dist_type == CV_DIST_C || dist_type == CV_DIST_L1 ? 0 : 0.01*MAX(sz.width, sz.height);
113
}
114
115
116
void CV_DisTransTest::get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high )
117
{
118
cvtest::ArrayTest::get_minmax_bounds( i, j, type, low, high );
119
if( i == INPUT && CV_MAT_DEPTH(type) == CV_8U )
120
{
121
low = Scalar::all(0);
122
high = Scalar::all(10);
123
}
124
}
125
126
int CV_DisTransTest::prepare_test_case( int test_case_idx )
127
{
128
int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
129
if( code > 0 )
130
{
131
// the function's response to an "all-nonzeros" image is not determined,
132
// so put at least one zero point
133
Mat& mat = test_mat[INPUT][0];
134
RNG& rng = ts->get_rng();
135
int i = cvtest::randInt(rng) % mat.rows;
136
int j = cvtest::randInt(rng) % mat.cols;
137
mat.at<uchar>(i,j) = 0;
138
}
139
140
return code;
141
}
142
143
144
void CV_DisTransTest::run_func()
145
{
146
cvDistTransform( test_array[INPUT][0], test_array[OUTPUT][0], dist_type, mask_size,
147
dist_type == CV_DIST_USER ? mask : 0, test_array[OUTPUT][1] );
148
}
149
150
151
static void
152
cvTsDistTransform( const CvMat* _src, CvMat* _dst, int dist_type,
153
int mask_size, float* _mask, CvMat* /*_labels*/ )
154
{
155
int i, j, k;
156
int width = _src->cols, height = _src->rows;
157
const float init_val = 1e6;
158
float mask[3];
159
CvMat* temp;
160
int ofs[16] = {0};
161
float delta[16];
162
int tstep, count;
163
164
assert( mask_size == 3 || mask_size == 5 );
165
166
if( dist_type == CV_DIST_USER )
167
memcpy( mask, _mask, sizeof(mask) );
168
else if( dist_type == CV_DIST_C )
169
{
170
mask_size = 3;
171
mask[0] = mask[1] = 1.f;
172
}
173
else if( dist_type == CV_DIST_L1 )
174
{
175
mask_size = 3;
176
mask[0] = 1.f;
177
mask[1] = 2.f;
178
}
179
else if( mask_size == 3 )
180
{
181
mask[0] = 0.955f;
182
mask[1] = 1.3693f;
183
}
184
else
185
{
186
mask[0] = 1.0f;
187
mask[1] = 1.4f;
188
mask[2] = 2.1969f;
189
}
190
191
temp = cvCreateMat( height + mask_size-1, width + mask_size-1, CV_32F );
192
tstep = temp->step / sizeof(float);
193
194
if( mask_size == 3 )
195
{
196
count = 4;
197
ofs[0] = -1; delta[0] = mask[0];
198
ofs[1] = -tstep-1; delta[1] = mask[1];
199
ofs[2] = -tstep; delta[2] = mask[0];
200
ofs[3] = -tstep+1; delta[3] = mask[1];
201
}
202
else
203
{
204
count = 8;
205
ofs[0] = -1; delta[0] = mask[0];
206
ofs[1] = -tstep-2; delta[1] = mask[2];
207
ofs[2] = -tstep-1; delta[2] = mask[1];
208
ofs[3] = -tstep; delta[3] = mask[0];
209
ofs[4] = -tstep+1; delta[4] = mask[1];
210
ofs[5] = -tstep+2; delta[5] = mask[2];
211
ofs[6] = -tstep*2-1; delta[6] = mask[2];
212
ofs[7] = -tstep*2+1; delta[7] = mask[2];
213
}
214
215
for( i = 0; i < mask_size/2; i++ )
216
{
217
float* t0 = (float*)(temp->data.ptr + i*temp->step);
218
float* t1 = (float*)(temp->data.ptr + (temp->rows - i - 1)*temp->step);
219
220
for( j = 0; j < width + mask_size - 1; j++ )
221
t0[j] = t1[j] = init_val;
222
}
223
224
for( i = 0; i < height; i++ )
225
{
226
uchar* s = _src->data.ptr + i*_src->step;
227
float* tmp = (float*)(temp->data.ptr + temp->step*(i + (mask_size/2))) + (mask_size/2);
228
229
for( j = 0; j < mask_size/2; j++ )
230
tmp[-j-1] = tmp[j + width] = init_val;
231
232
for( j = 0; j < width; j++ )
233
{
234
if( s[j] == 0 )
235
tmp[j] = 0;
236
else
237
{
238
float min_dist = init_val;
239
for( k = 0; k < count; k++ )
240
{
241
float t = tmp[j+ofs[k]] + delta[k];
242
if( min_dist > t )
243
min_dist = t;
244
}
245
tmp[j] = min_dist;
246
}
247
}
248
}
249
250
for( i = height - 1; i >= 0; i-- )
251
{
252
float* d = (float*)(_dst->data.ptr + i*_dst->step);
253
float* tmp = (float*)(temp->data.ptr + temp->step*(i + (mask_size/2))) + (mask_size/2);
254
255
for( j = width - 1; j >= 0; j-- )
256
{
257
float min_dist = tmp[j];
258
if( min_dist > mask[0] )
259
{
260
for( k = 0; k < count; k++ )
261
{
262
float t = tmp[j-ofs[k]] + delta[k];
263
if( min_dist > t )
264
min_dist = t;
265
}
266
tmp[j] = min_dist;
267
}
268
d[j] = min_dist;
269
}
270
}
271
272
cvReleaseMat( &temp );
273
}
274
275
276
void CV_DisTransTest::prepare_to_validation( int /*test_case_idx*/ )
277
{
278
CvMat _input = cvMat(test_mat[INPUT][0]), _output = cvMat(test_mat[REF_OUTPUT][0]);
279
280
cvTsDistTransform( &_input, &_output, dist_type, mask_size, mask, 0 );
281
}
282
283
284
TEST(Imgproc_DistanceTransform, accuracy) { CV_DisTransTest test; test.safe_run(); }
285
286
BIGDATA_TEST(Imgproc_DistanceTransform, large_image_12218)
287
{
288
const int lls_maxcnt = 79992000; // labels's maximum count
289
const int lls_mincnt = 1; // labels's minimum count
290
int i, j, nz;
291
Mat src(8000, 20000, CV_8UC1), dst, labels;
292
for( i = 0; i < src.rows; i++ )
293
for( j = 0; j < src.cols; j++ )
294
src.at<uchar>(i, j) = (j > (src.cols / 2)) ? 0 : 255;
295
296
distanceTransform(src, dst, labels, cv::DIST_L2, cv::DIST_MASK_3, DIST_LABEL_PIXEL);
297
298
double scale = (double)lls_mincnt / (double)lls_maxcnt;
299
labels.convertTo(labels, CV_32SC1, scale);
300
Size size = labels.size();
301
nz = cv::countNonZero(labels);
302
EXPECT_EQ(nz, (size.height*size.width / 2));
303
}
304
305
}} // namespace
306
307