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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/*******************************************************
* 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>
using std::string;
using std::vector;
using std::abs;
template<typename T>
class MedianFilter : public ::testing::Test
{
public:
virtual void SetUp() {}
};
// create a list of types to be tested
typedef ::testing::Types<float, double, int, uint, char, uchar, short, ushort> TestTypes;
// register the type list
TYPED_TEST_CASE(MedianFilter, TestTypes);
template<typename T>
void medfiltTest(string pTestFile, dim_t w_len, dim_t w_wid, af_border_type pad)
{
if (noDoubleTests<T>()) return;
vector<af::dim4> numDims;
vector<vector<T> > in;
vector<vector<T> > tests;
readTests<T,T,int>(pTestFile, numDims, in, tests);
af::dim4 dims = numDims[0];
af_array outArray = 0;
af_array inArray = 0;
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in[0].front()),
dims.ndims(), dims.get(), (af_dtype)af::dtype_traits<T>::af_type));
ASSERT_EQ(AF_SUCCESS, af_medfilt(&outArray, inArray, w_len, w_wid, pad));
T *outData = new T[dims.elements()];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
vector<T> currGoldBar = tests[0];
size_t nElems = currGoldBar.size();
for (size_t elIter=0; elIter<nElems; ++elIter) {
ASSERT_EQ(currGoldBar[elIter], outData[elIter])<< "at: " << elIter<< std::endl;
}
// cleanup
delete[] outData;
ASSERT_EQ(AF_SUCCESS, af_release_array(inArray));
ASSERT_EQ(AF_SUCCESS, af_release_array(outArray));
}
TYPED_TEST(MedianFilter, ZERO_PAD_3x3)
{
medfiltTest<TypeParam>(string(TEST_DIR"/medianfilter/zero_pad_3x3_window.test"), 3, 3, AF_PAD_ZERO);
}
TYPED_TEST(MedianFilter, SYMMETRIC_PAD_3x3)
{
medfiltTest<TypeParam>(string(TEST_DIR"/medianfilter/symmetric_pad_3x3_window.test"), 3, 3, AF_PAD_SYM);
}
TYPED_TEST(MedianFilter, BATCH_ZERO_PAD_3x3)
{
medfiltTest<TypeParam>(string(TEST_DIR"/medianfilter/batch_zero_pad_3x3_window.test"), 3, 3, AF_PAD_ZERO);
}
TYPED_TEST(MedianFilter, BATCH_SYMMETRIC_PAD_3x3)
{
medfiltTest<TypeParam>(string(TEST_DIR"/medianfilter/batch_symmetric_pad_3x3_window.test"), 3, 3, AF_PAD_SYM);
}
template<typename T,bool isColor>
void medfiltImageTest(string pTestFile, dim_t w_len, dim_t w_wid)
{
if (noDoubleTests<T>()) return;
if (noImageIOTests()) return;
using af::dim4;
vector<dim4> inDims;
vector<string> inFiles;
vector<dim_t> 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_t nElems = 0;
inFiles[testId].insert(0,string(TEST_DIR"/medianfilter/"));
outFiles[testId].insert(0,string(TEST_DIR"/medianfilter/"));
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_medfilt(&outArray, inArray, w_len, w_wid, AF_PAD_ZERO));
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.018f));
ASSERT_EQ(AF_SUCCESS, af_release_array(inArray));
ASSERT_EQ(AF_SUCCESS, af_release_array(outArray));
ASSERT_EQ(AF_SUCCESS, af_release_array(goldArray));
}
}
template<typename T>
void medfiltInputTest(void)
{
if (noDoubleTests<T>()) return;
af_array inArray = 0;
af_array outArray = 0;
vector<T> in(100, 1);
// Check for 1D inputs
af::dim4 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<T>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_medfilt(&outArray, inArray, 1, 1, AF_PAD_ZERO));
ASSERT_EQ(AF_SUCCESS, af_release_array(inArray));
}
TYPED_TEST(MedianFilter, InvalidArray)
{
medfiltInputTest<TypeParam>();
}
template<typename T>
void medfiltWindowTest(void)
{
if (noDoubleTests<T>()) return;
af_array inArray = 0;
af_array outArray = 0;
vector<T> in(100, 1);
// Check for 4D inputs
af::dim4 dims(10, 10, 1, 1);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &in.front(),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<T>::af_type));
ASSERT_EQ(AF_ERR_ARG, af_medfilt(&outArray, inArray, 3, 5, AF_PAD_ZERO));
ASSERT_EQ(AF_SUCCESS, af_release_array(inArray));
}
TYPED_TEST(MedianFilter, InvalidWindow)
{
medfiltWindowTest<TypeParam>();
}
template<typename T>
void medfiltPadTest(void)
{
if (noDoubleTests<T>()) return;
af_array inArray = 0;
af_array outArray = 0;
vector<T> in(100, 1);
// Check for 4D inputs
af::dim4 dims(10, 10, 1, 1);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &in.front(),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<T>::af_type));
ASSERT_EQ(AF_ERR_ARG, af_medfilt(&outArray, inArray, 3, 3, af_border_type(3)));
ASSERT_EQ(AF_ERR_ARG, af_medfilt(&outArray, inArray, 3, 3, af_border_type(-1)));
ASSERT_EQ(AF_SUCCESS, af_release_array(inArray));
}
TYPED_TEST(MedianFilter, InvalidPadType)
{
medfiltPadTest<TypeParam>();
}
//////////////////////////////////// CPP ////////////////////////////////////
//
TEST(MedianFilter, CPP)
{
if (noDoubleTests<float>()) return;
const dim_t w_len = 3;
const dim_t w_wid = 3;
vector<af::dim4> numDims;
vector<vector<float> > in;
vector<vector<float> > tests;
readTests<float,float,int>(string(TEST_DIR"/medianfilter/batch_symmetric_pad_3x3_window.test"),
numDims, in, tests);
af::dim4 dims = numDims[0];
af::array input(dims, &(in[0].front()));
af::array output = af::medfilt(input, w_len, w_wid, AF_PAD_SYM);
float *outData = new float[dims.elements()];
output.host((void*)outData);
vector<float> currGoldBar = tests[0];
size_t nElems = currGoldBar.size();
for (size_t elIter=0; elIter<nElems; ++elIter) {
ASSERT_EQ(currGoldBar[elIter], outData[elIter])<< "at: " << elIter<< std::endl;
}
// cleanup
delete[] outData;
}
TEST(MedianFilter, Docs)
{
using af::array;
float input[] = {
1.0000, 2.0000, 3.0000, 4.0000,
5.0000, 6.0000, 7.0000, 8.0000,
9.0000, 10.0000, 11.0000, 12.0000,
13.0000, 14.0000, 15.0000, 16.0000
};
float gold[] = {
0.0000, 2.0000, 3.0000, 0.0000,
2.0000, 6.0000, 7.0000, 4.0000,
6.0000, 10.0000, 11.0000, 8.0000,
0.0000, 10.0000, 11.0000, 0.0000
};
//![ex_image_medfilt]
array a = array(4, 4, input);
//af_print(a);
//a = 1.0000 5.0000 9.0000 13.0000
// 2.0000 6.0000 10.0000 14.0000
// 3.0000 7.0000 11.0000 15.0000
// 4.0000 8.0000 12.0000 16.0000
array b = af::medfilt(a, 3, 3, AF_PAD_ZERO);
//af_print(b);
//b= 0.0000 2.0000 6.0000 0.0000
// 2.0000 6.0000 10.0000 10.0000
// 3.0000 7.0000 11.0000 11.0000
// 0.0000 4.0000 8.0000 0.0000
//![ex_image_medfilt]
float output[16];
b.host((void*)output);
for (int i=0; i<16; ++i) {
ASSERT_EQ(output[i], gold[i]) << "output mismatch at i = " << i << std::endl;
}
}
using namespace af;
TEST(MedianFilter, GFOR)
{
dim4 dims = dim4(10, 10, 3);
array A = iota(dims);
array B = constant(0, dims);
gfor(seq ii, 3) {
B(span, span, ii) = medfilt(A(span, span, ii));
}
for(int ii = 0; ii < 3; ii++) {
array c_ii = medfilt(A(span, span, ii));
array b_ii = B(span, span, ii);
ASSERT_EQ(max<double>(abs(c_ii - b_ii)) < 1E-5, true);
}
}
|