forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeanshift.cpp
More file actions
157 lines (122 loc) · 5.21 KB
/
Copy pathmeanshift.cpp
File metadata and controls
157 lines (122 loc) · 5.21 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
/*******************************************************
* 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 <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <string>
#include <vector>
#include <testHelpers.hpp>
#include <cmath>
using std::string;
using std::vector;
using af::dim4;
template<typename T>
class Meanshift : public ::testing::Test
{
public:
virtual void SetUp() {}
};
typedef ::testing::Types<float, double, int, uint, char, uchar> TestTypes;
TYPED_TEST_CASE(Meanshift, TestTypes);
TYPED_TEST(Meanshift, InvalidArgs)
{
if (noDoubleTests<TypeParam>()) return;
vector<TypeParam> in(100,1);
af_array inArray = 0;
af_array outArray = 0;
// check for gray scale Meanshift
af::dim4 dims(5,5,2,2);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &in.front(),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<TypeParam>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_meanshift(&outArray, inArray, 0.12f, 0.34f, 5, false));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
// check for color image Meanshift
dims = af::dim4(100,1,1,1);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &in.front(),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<TypeParam>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_meanshift(&outArray, inArray, 0.12f, 0.34f, 5, true));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
}
template<typename T, bool isColor>
void meanshiftTest(string pTestFile)
{
if (noDoubleTests<T>()) return;
vector<dim4> inDims;
vector<string> inFiles;
vector<dim_type> outSizes;
vector<string> outFiles;
readImageTests(pTestFile, inDims, inFiles, outSizes, outFiles);
size_t testCount = inDims.size();
for (size_t testId=0; testId<testCount; ++testId) {
af_array inArray = 0;
af_array outArray = 0;
af_array goldArray= 0;
dim_type nElems = 0;
inFiles[testId].insert(0,string(TEST_DIR"/meanshift/"));
outFiles[testId].insert(0,string(TEST_DIR"/meanshift/"));
ASSERT_EQ(AF_SUCCESS, af_load_image(&inArray, inFiles[testId].c_str(), isColor));
ASSERT_EQ(AF_SUCCESS, af_load_image(&goldArray, outFiles[testId].c_str(), isColor));
ASSERT_EQ(AF_SUCCESS, af_get_elements(&nElems, goldArray));
ASSERT_EQ(AF_SUCCESS, af_meanshift(&outArray, inArray, 2.25f, 25.56f, 5, isColor));
T * outData = new T[nElems];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
T * goldData= new T[nElems];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)goldData, goldArray));
ASSERT_EQ(true, compareArraysRMSD(nElems, goldData, outData, 0.07f));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(outArray));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(goldArray));
}
}
// create a list of types to be tested
// FIXME: since af_load_image returns only f32 type arrays
// only float, double data types test are enabled & passing
// Note: compareArraysRMSD is handling upcasting while working
// with two different type of types
//
#define IMAGE_TESTS(T) \
TEST(Meanshift, Grayscale_##T) \
{ \
meanshiftTest<T, false>(string(TEST_DIR"/meanshift/gray.test")); \
} \
TEST(Meanshift, Color_##T) \
{ \
meanshiftTest<T, true>(string(TEST_DIR"/meanshift/color.test")); \
}
IMAGE_TESTS(float )
IMAGE_TESTS(double)
//////////////////////////////////////// CPP ///////////////////////////////
//
TEST(Meanshift, Color_CPP)
{
if (noDoubleTests<float>()) return;
vector<dim4> inDims;
vector<string> inFiles;
vector<dim_type> outSizes;
vector<string> outFiles;
readImageTests(string(TEST_DIR"/meanshift/color.test"), inDims, inFiles, outSizes, outFiles);
size_t testCount = inDims.size();
for (size_t testId=0; testId<testCount; ++testId) {
inFiles[testId].insert(0,string(TEST_DIR"/meanshift/"));
outFiles[testId].insert(0,string(TEST_DIR"/meanshift/"));
af::array img = af::loadImage(inFiles[testId].c_str(), true);
af::array gold = af::loadImage(outFiles[testId].c_str(), true);
dim_type nElems = gold.elements();
af::array output= af::meanshift(img, 2.25f, 25.56f, 5, true);
float * outData = new float[nElems];
output.host((void*)outData);
float * goldData= new float[nElems];
gold.host((void*)goldData);
ASSERT_EQ(true, compareArraysRMSD(nElems, goldData, outData, 0.07f));
// cleanup
delete[] outData;
delete[] goldData;
}
}