-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathconfidence_connected.cpp
More file actions
250 lines (213 loc) · 10.4 KB
/
confidence_connected.cpp
File metadata and controls
250 lines (213 loc) · 10.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*******************************************************
* 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_SUITE(ConfidenceConnectedImageTest, TestTypes);
struct CCCTestParams {
const char *prefix;
unsigned int radius;
unsigned int multiplier;
unsigned int iterations;
double replace;
};
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);
IMAGEIO_ENABLED_CHECK();
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;
ASSERT_SUCCESS(af_confidence_cc(&outArray, inArray, seedxArr, seedyArr,
params.radius, params.multiplier,
params.iterations, params.replace));
int device = 0;
ASSERT_SUCCESS(af_get_device(&device));
ASSERT_SUCCESS(af_sync(device));
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;
ASSERT_SUCCESS(af_confidence_cc(&outArray, inArray, seedxArr, seedyArr,
params.radius, params.multiplier,
params.iterations, params.replace));
int device = 0;
ASSERT_SUCCESS(af_get_device(&device));
ASSERT_SUCCESS(af_sync(device));
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) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
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) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
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) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
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) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
testData<unsigned char>(GetParam());
}
INSTANTIATE_TEST_SUITE_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();
});
#define TEST_FORMATS(form) \
TEST(TEMP_FORMAT, form##_2Dseed) { \
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI); \
const string filename(string(TEST_DIR) + "/confidence_cc/donut.png"); \
const af::array image(af::loadImage(filename.c_str())); \
const af::array seed(dim4(1, 2), {10u, 8u}); \
\
const af::array out = \
af::confidenceCC(toTempFormat(form, image), \
toTempFormat(form, seed), 3, 3, 25, 255.0); \
const af::array gold = af::confidenceCC(image, seed, 3, 3, 25, 255.0); \
\
EXPECT_ARRAYS_EQ(out, gold); \
} \
\
TEST(TEMP_FORMAT, form##_2xSeed) { \
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI); \
const string filename(string(TEST_DIR) + "/confidence_cc/donut.png"); \
const af::array image(af::loadImage(filename.c_str())); \
const af::array seedx({10u}); \
const af::array seedy({8u}); \
\
const af::array out = af::confidenceCC( \
toTempFormat(form, image), toTempFormat(form, seedx), \
toTempFormat(form, seedy), 3, 3, 25, 255.0); \
const af::array gold = \
af::confidenceCC(image, seedx, seedy, 3, 3, 25, 255.0); \
\
EXPECT_ARRAYS_EQ(out, gold); \
} \
TEST(TEMP_FORMAT, form##_vectSeed) { \
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI); \
const string filename(string(TEST_DIR) + "/confidence_cc/donut.png"); \
const af::array image(af::loadImage(filename.c_str())); \
const unsigned seedx[1] = {10u}; \
const unsigned seedy[1] = {8u}; \
\
const af::array out = af::confidenceCC(toTempFormat(form, image), 1, \
seedx, seedy, 3, 3, 25, 255.0); \
const af::array gold = \
af::confidenceCC(image, 1, seedx, seedy, 3, 3, 25, 255.0); \
\
EXPECT_ARRAYS_EQ(out, gold); \
}
FOREACH_TEMP_FORMAT(TEST_FORMATS)