1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
43
#include "test_precomp.hpp"
46
TestHypothesesFilter::TestHypothesesFilter(std::string testName_, NCVTestSourceProvider<Ncv32u> &src_,
47
Ncv32u numDstRects_, Ncv32u minNeighbors_, Ncv32f eps_)
49
NCVTestProvider(testName_),
51
numDstRects(numDstRects_),
52
minNeighbors(minNeighbors_),
58
bool TestHypothesesFilter::toString(std::ofstream &strOut)
60
strOut << "numDstRects=" << numDstRects << std::endl;
61
strOut << "minNeighbors=" << minNeighbors << std::endl;
62
strOut << "eps=" << eps << std::endl;
67
bool TestHypothesesFilter::init()
69
this->canvasWidth = 4096;
70
this->canvasHeight = 4096;
75
bool compareRects(const NcvRect32u &r1, const NcvRect32u &r2, Ncv32f eps)
77
double delta = eps*(std::min(r1.width, r2.width) + std::min(r1.height, r2.height))*0.5;
78
return std::abs((Ncv32s)r1.x - (Ncv32s)r2.x) <= delta &&
79
std::abs((Ncv32s)r1.y - (Ncv32s)r2.y) <= delta &&
80
std::abs((Ncv32s)r1.x + (Ncv32s)r1.width - (Ncv32s)r2.x - (Ncv32s)r2.width) <= delta &&
81
std::abs((Ncv32s)r1.y + (Ncv32s)r1.height - (Ncv32s)r2.y - (Ncv32s)r2.height) <= delta;
85
inline bool operator < (const NcvRect32u &a, const NcvRect32u &b)
91
bool TestHypothesesFilter::process()
96
NCVVectorAlloc<Ncv32u> h_random32u(*this->allocatorCPU.get(), this->numDstRects * sizeof(NcvRect32u) / sizeof(Ncv32u));
97
ncvAssertReturn(h_random32u.isMemAllocated(), false);
99
Ncv32u srcSlotSize = 2 * this->minNeighbors + 1;
101
NCVVectorAlloc<NcvRect32u> h_vecSrc(*this->allocatorCPU.get(), this->numDstRects*srcSlotSize);
102
ncvAssertReturn(h_vecSrc.isMemAllocated(), false);
103
NCVVectorAlloc<NcvRect32u> h_vecDst_groundTruth(*this->allocatorCPU.get(), this->numDstRects);
104
ncvAssertReturn(h_vecDst_groundTruth.isMemAllocated(), false);
106
NCV_SET_SKIP_COND(this->allocatorCPU.get()->isCounting());
109
ncvAssertReturn(this->src.fill(h_random32u), false);
113
for (Ncv32u i=0; i<this->numDstRects; i++)
115
h_vecDst_groundTruth.ptr()[i].x = i * this->canvasWidth / this->numDstRects + this->canvasWidth / (this->numDstRects * 4);
116
h_vecDst_groundTruth.ptr()[i].y = i * this->canvasHeight / this->numDstRects + this->canvasHeight / (this->numDstRects * 4);
117
h_vecDst_groundTruth.ptr()[i].width = this->canvasWidth / (this->numDstRects * 2);
118
h_vecDst_groundTruth.ptr()[i].height = this->canvasHeight / (this->numDstRects * 2);
120
Ncv32u numNeighbors = this->minNeighbors + 1 + (Ncv32u)(((1.0 * h_random32u.ptr()[i]) * (this->minNeighbors + 1)) / 0xFFFFFFFF);
121
numNeighbors = (numNeighbors > srcSlotSize) ? srcSlotSize : numNeighbors;
123
//fill in strong hypotheses (2 * ((1.0 * randVal) / 0xFFFFFFFF) - 1)
124
for (Ncv32u j=0; j<numNeighbors; j++)
126
randVal = (1.0 * h_random32u.ptr()[randCnt++]) / 0xFFFFFFFF; randCnt = randCnt % h_random32u.length();
127
h_vecSrc.ptr()[srcSlotSize * i + j].x =
128
h_vecDst_groundTruth.ptr()[i].x +
129
(Ncv32s)(h_vecDst_groundTruth.ptr()[i].width * this->eps * (randVal - 0.5));
130
randVal = (1.0 * h_random32u.ptr()[randCnt++]) / 0xFFFFFFFF; randCnt = randCnt % h_random32u.length();
131
h_vecSrc.ptr()[srcSlotSize * i + j].y =
132
h_vecDst_groundTruth.ptr()[i].y +
133
(Ncv32s)(h_vecDst_groundTruth.ptr()[i].height * this->eps * (randVal - 0.5));
134
h_vecSrc.ptr()[srcSlotSize * i + j].width = h_vecDst_groundTruth.ptr()[i].width;
135
h_vecSrc.ptr()[srcSlotSize * i + j].height = h_vecDst_groundTruth.ptr()[i].height;
138
//generate weak hypotheses (to be removed in processing)
139
for (Ncv32u j=numNeighbors; j<srcSlotSize; j++)
141
randVal = (1.0 * h_random32u.ptr()[randCnt++]) / 0xFFFFFFFF; randCnt = randCnt % h_random32u.length();
142
h_vecSrc.ptr()[srcSlotSize * i + j].x =
143
this->canvasWidth + h_vecDst_groundTruth.ptr()[i].x +
144
(Ncv32s)(h_vecDst_groundTruth.ptr()[i].width * this->eps * (randVal - 0.5));
145
randVal = (1.0 * h_random32u.ptr()[randCnt++]) / 0xFFFFFFFF; randCnt = randCnt % h_random32u.length();
146
h_vecSrc.ptr()[srcSlotSize * i + j].y =
147
this->canvasHeight + h_vecDst_groundTruth.ptr()[i].y +
148
(Ncv32s)(h_vecDst_groundTruth.ptr()[i].height * this->eps * (randVal - 0.5));
149
h_vecSrc.ptr()[srcSlotSize * i + j].width = h_vecDst_groundTruth.ptr()[i].width;
150
h_vecSrc.ptr()[srcSlotSize * i + j].height = h_vecDst_groundTruth.ptr()[i].height;
155
for (Ncv32u i=0; i<this->numDstRects*srcSlotSize-1; i++)
157
Ncv32u randValLocal = h_random32u.ptr()[randCnt++]; randCnt = randCnt % h_random32u.length();
158
Ncv32u secondSwap = randValLocal % (this->numDstRects*srcSlotSize-1 - i);
159
NcvRect32u tmp = h_vecSrc.ptr()[i + secondSwap];
160
h_vecSrc.ptr()[i + secondSwap] = h_vecSrc.ptr()[i];
161
h_vecSrc.ptr()[i] = tmp;
165
Ncv32u numHypothesesSrc = static_cast<Ncv32u>(h_vecSrc.length());
167
ncvStat = ncvGroupRectangles_host(h_vecSrc, numHypothesesSrc, this->minNeighbors, this->eps, NULL);
168
ncvAssertReturn(ncvStat == NCV_SUCCESS, false);
172
bool bLoopVirgin = true;
175
if (numHypothesesSrc != this->numDstRects)
181
std::vector<NcvRect32u> tmpRects(numHypothesesSrc);
182
memcpy(&tmpRects[0], h_vecSrc.ptr(), numHypothesesSrc * sizeof(NcvRect32u));
183
std::sort(tmpRects.begin(), tmpRects.end());
184
for (Ncv32u i=0; i<numHypothesesSrc && bLoopVirgin; i++)
186
if (!compareRects(tmpRects[i], h_vecDst_groundTruth.ptr()[i], this->eps))
203
bool TestHypothesesFilter::deinit()