forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfidence_connected.cpp
More file actions
200 lines (163 loc) · 6.74 KB
/
confidence_connected.cpp
File metadata and controls
200 lines (163 loc) · 6.74 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/traits.hpp>
#include <sstream>
#include <string>
#include <vector>
using af::dim4;
using std::abs;
using std::string;
using std::stringstream;
using std::to_string;
using std::vector;
template<typename T>
class ConfidenceConnectedImageTest : public testing::Test {
public:
virtual void SetUp() {}
};
typedef ::testing::Types<float, uint, ushort, uchar> TestTypes;
TYPED_TEST_CASE(ConfidenceConnectedImageTest, TestTypes);
struct CCCTestParams {
const char *prefix;
unsigned int radius;
unsigned int multiplier;
unsigned int iterations;
double replace;
};
void apiWrapper(af_array *out, const af_array in, const af_array seedx,
const af_array seedy, const CCCTestParams params) {
ASSERT_SUCCESS(af_confidence_cc(out, in, seedx, seedy, params.radius,
params.multiplier, params.iterations,
params.replace));
int device = 0;
ASSERT_SUCCESS(af_get_device(&device));
ASSERT_SUCCESS(af_sync(device));
}
template<typename T>
void testImage(const std::string pTestFile, const size_t numSeeds,
const unsigned *seedx, const unsigned *seedy,
const int multiplier, const unsigned neighborhood_radius,
const int iter) {
SUPPORTED_TYPE_CHECK(T);
if (noImageIOTests()) return;
vector<af::dim4> inDims;
vector<string> inFiles;
vector<dim_t> outSizes;
vector<string> outFiles;
readImageTests(std::string(TEST_DIR) + "/confidence_cc/" + pTestFile,
inDims, inFiles, outSizes, outFiles);
size_t testCount = inDims.size();
af_array seedxArr = 0, seedyArr = 0;
dim4 seedDims(numSeeds);
ASSERT_SUCCESS(af_create_array(&seedxArr, seedx, seedDims.ndims(),
seedDims.get(), u32));
ASSERT_SUCCESS(af_create_array(&seedyArr, seedy, seedDims.ndims(),
seedDims.get(), u32));
for (size_t testId = 0; testId < testCount; ++testId) {
af_array _inArray = 0;
af_array inArray = 0;
af_array outArray = 0;
af_array _goldArray = 0;
af_array goldArray = 0;
inFiles[testId].insert(0, string(TEST_DIR "/confidence_cc/"));
outFiles[testId].insert(0, string(TEST_DIR "/confidence_cc/"));
ASSERT_SUCCESS(
af_load_image(&_inArray, inFiles[testId].c_str(), false));
ASSERT_SUCCESS(
af_load_image(&_goldArray, outFiles[testId].c_str(), false));
// af_load_image always returns float array, so convert to output type
ASSERT_SUCCESS(conv_image<T>(&inArray, _inArray));
ASSERT_SUCCESS(conv_image<T>(&goldArray, _goldArray));
CCCTestParams params;
params.prefix = "Image";
params.radius = neighborhood_radius;
params.multiplier = multiplier;
params.iterations = iter;
params.replace = 255.0;
apiWrapper(&outArray, inArray, seedxArr, seedyArr, params);
ASSERT_ARRAYS_EQ(outArray, goldArray);
ASSERT_SUCCESS(af_release_array(_inArray));
ASSERT_SUCCESS(af_release_array(inArray));
ASSERT_SUCCESS(af_release_array(outArray));
ASSERT_SUCCESS(af_release_array(_goldArray));
ASSERT_SUCCESS(af_release_array(goldArray));
}
ASSERT_SUCCESS(af_release_array(seedxArr));
ASSERT_SUCCESS(af_release_array(seedyArr));
}
template<typename T>
void testData(CCCTestParams params) {
SUPPORTED_TYPE_CHECK(T);
vector<dim4> numDims;
vector<vector<T> > in;
vector<vector<T> > tests;
string file = string(TEST_DIR) + "/confidence_cc/" + string(params.prefix) +
"_" + to_string(params.radius) + "_" +
to_string(params.multiplier) + ".test";
readTests<T, T, int>(file, numDims, in, tests);
dim4 dims = numDims[0];
af_array inArray = 0;
af_array seedxArr = 0, seedyArr = 0;
vector<uint> seedCoords(in[1].begin(), in[1].end());
const unsigned *seedxy = seedCoords.data();
dim4 seedDims(1);
ASSERT_SUCCESS(af_create_array(&seedxArr, seedxy + 0, seedDims.ndims(),
seedDims.get(), u32));
ASSERT_SUCCESS(af_create_array(&seedyArr, seedxy + 1, seedDims.ndims(),
seedDims.get(), u32));
ASSERT_SUCCESS(af_create_array(&inArray, &(in[0].front()), dims.ndims(),
dims.get(),
(af_dtype)af::dtype_traits<T>::af_type));
af_array outArray = 0;
apiWrapper(&outArray, inArray, seedxArr, seedyArr, params);
ASSERT_VEC_ARRAY_EQ(tests[0], dims, outArray);
ASSERT_SUCCESS(af_release_array(inArray));
ASSERT_SUCCESS(af_release_array(outArray));
ASSERT_SUCCESS(af_release_array(seedxArr));
ASSERT_SUCCESS(af_release_array(seedyArr));
}
class ConfidenceConnectedDataTest
: public testing::TestWithParam<CCCTestParams> {};
TYPED_TEST(ConfidenceConnectedImageTest, DonutBackgroundExtraction) {
const unsigned seedx = 10;
const unsigned seedy = 10;
testImage<TypeParam>(std::string("donut_background.test"), 1, &seedx,
&seedy, 3, 3, 25);
}
TYPED_TEST(ConfidenceConnectedImageTest, DonutRingExtraction) {
const unsigned seedx = 132;
const unsigned seedy = 132;
testImage<TypeParam>(std::string("donut_ring.test"), 1, &seedx, &seedy, 3,
3, 25);
}
TYPED_TEST(ConfidenceConnectedImageTest, DonutKernelExtraction) {
const unsigned seedx = 150;
const unsigned seedy = 150;
testImage<TypeParam>(std::string("donut_core.test"), 1, &seedx, &seedy, 3,
3, 25);
}
TEST_P(ConfidenceConnectedDataTest, SegmentARegion) {
testData<unsigned char>(GetParam());
}
INSTANTIATE_TEST_CASE_P(
SingleSeed, ConfidenceConnectedDataTest,
testing::Values(CCCTestParams{"core", 0u, 1u, 5u, 255.0},
CCCTestParams{"background", 0u, 1u, 5u, 255.0},
CCCTestParams{"ring", 0u, 1u, 5u, 255.0}),
[](const ::testing::TestParamInfo<ConfidenceConnectedDataTest::ParamType>
info) {
stringstream ss;
ss << "_prefix_" << info.param.prefix << "_radius_" << info.param.radius
<< "_multiplier_" << info.param.multiplier << "_iterations_"
<< info.param.iterations << "_replace_" << info.param.replace;
return ss.str();
});