1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
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.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
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.
26 //
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.
29 //
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.
40 //
41 //M*/
42
43 #include "test_precomp.hpp"
44
45
TestHypothesesFilter(std::string testName_,NCVTestSourceProvider<Ncv32u> & src_,Ncv32u numDstRects_,Ncv32u minNeighbors_,Ncv32f eps_)46 TestHypothesesFilter::TestHypothesesFilter(std::string testName_, NCVTestSourceProvider<Ncv32u> &src_,
47 Ncv32u numDstRects_, Ncv32u minNeighbors_, Ncv32f eps_)
48 :
49 NCVTestProvider(testName_),
50 src(src_),
51 numDstRects(numDstRects_),
52 minNeighbors(minNeighbors_),
53 eps(eps_)
54 {
55 }
56
57
toString(std::ofstream & strOut)58 bool TestHypothesesFilter::toString(std::ofstream &strOut)
59 {
60 strOut << "numDstRects=" << numDstRects << std::endl;
61 strOut << "minNeighbors=" << minNeighbors << std::endl;
62 strOut << "eps=" << eps << std::endl;
63 return true;
64 }
65
66
init()67 bool TestHypothesesFilter::init()
68 {
69 this->canvasWidth = 4096;
70 this->canvasHeight = 4096;
71 return true;
72 }
73
74
compareRects(const NcvRect32u & r1,const NcvRect32u & r2,Ncv32f eps)75 bool compareRects(const NcvRect32u &r1, const NcvRect32u &r2, Ncv32f eps)
76 {
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;
82 }
83
84
operator <(const NcvRect32u & a,const NcvRect32u & b)85 inline bool operator < (const NcvRect32u &a, const NcvRect32u &b)
86 {
87 return a.x < b.x;
88 }
89
90
process()91 bool TestHypothesesFilter::process()
92 {
93 NCVStatus ncvStat;
94 bool rcode = false;
95
96 NCVVectorAlloc<Ncv32u> h_random32u(*this->allocatorCPU.get(), this->numDstRects * sizeof(NcvRect32u) / sizeof(Ncv32u));
97 ncvAssertReturn(h_random32u.isMemAllocated(), false);
98
99 Ncv32u srcSlotSize = 2 * this->minNeighbors + 1;
100
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);
105
106 NCV_SET_SKIP_COND(this->allocatorCPU.get()->isCounting());
107
108 NCV_SKIP_COND_BEGIN
109 ncvAssertReturn(this->src.fill(h_random32u), false);
110 Ncv32u randCnt = 0;
111 Ncv64f randVal;
112
113 for (Ncv32u i=0; i<this->numDstRects; i++)
114 {
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);
119
120 Ncv32u numNeighbors = this->minNeighbors + 1 + (Ncv32u)(((1.0 * h_random32u.ptr()[i]) * (this->minNeighbors + 1)) / 0xFFFFFFFF);
121 numNeighbors = (numNeighbors > srcSlotSize) ? srcSlotSize : numNeighbors;
122
123 //fill in strong hypotheses (2 * ((1.0 * randVal) / 0xFFFFFFFF) - 1)
124 for (Ncv32u j=0; j<numNeighbors; j++)
125 {
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;
136 }
137
138 //generate weak hypotheses (to be removed in processing)
139 for (Ncv32u j=numNeighbors; j<srcSlotSize; j++)
140 {
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;
151 }
152 }
153
154 //shuffle
155 for (Ncv32u i=0; i<this->numDstRects*srcSlotSize-1; i++)
156 {
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;
162 }
163 NCV_SKIP_COND_END
164
165 Ncv32u numHypothesesSrc = static_cast<Ncv32u>(h_vecSrc.length());
166 NCV_SKIP_COND_BEGIN
167 ncvStat = ncvGroupRectangles_host(h_vecSrc, numHypothesesSrc, this->minNeighbors, this->eps, NULL);
168 ncvAssertReturn(ncvStat == NCV_SUCCESS, false);
169 NCV_SKIP_COND_END
170
171 //verification
172 bool bLoopVirgin = true;
173
174 NCV_SKIP_COND_BEGIN
175 if (numHypothesesSrc != this->numDstRects)
176 {
177 bLoopVirgin = false;
178 }
179 else
180 {
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++)
185 {
186 if (!compareRects(tmpRects[i], h_vecDst_groundTruth.ptr()[i], this->eps))
187 {
188 bLoopVirgin = false;
189 }
190 }
191 }
192 NCV_SKIP_COND_END
193
194 if (bLoopVirgin)
195 {
196 rcode = true;
197 }
198
199 return rcode;
200 }
201
202
deinit()203 bool TestHypothesesFilter::deinit()
204 {
205 return true;
206 }
207