tldDetector.cpp 20.3 KB
Newer Older
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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

Vladimir's avatar
Vladimir committed
42
#include "tldDetector.hpp"
43
#include "tracking_utils.hpp"
Vladimir's avatar
Vladimir committed
44

45 46
#include <opencv2/core/utility.hpp>

Vladimir's avatar
Vladimir committed
47 48 49 50 51 52 53 54 55 56 57 58
namespace cv
{
	namespace tld
	{
		// Calculate offsets for classifiers
		void TLDDetector::prepareClassifiers(int rowstep)
		{
			for (int i = 0; i < (int)classifiers.size(); i++)
				classifiers[i].prepareClassifier(rowstep);
		}

		// Calculate posterior probability, that the patch belongs to the current EC model
Vladimir's avatar
Vladimir committed
59
		double TLDDetector::ensembleClassifierNum(const uchar* data)
Vladimir's avatar
Vladimir committed
60 61 62 63 64 65 66 67
		{
			double p = 0;
			for (int k = 0; k < (int)classifiers.size(); k++)
				p += classifiers[k].posteriorProbabilityFast(data);
			p /= classifiers.size();
			return p;
		}

68 69 70 71 72 73 74 75 76 77 78 79
        double TLDDetector::computeSminus(const Mat_<uchar>& patch) const
        {
            double sminus = 0.0;
            Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
            for (int i = 0; i < *negNum; i++)
            {
                modelSample.data = &(negExp->data[i * 225]);
                sminus = std::max(sminus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
            }
            return sminus;
        }

Vladimir's avatar
Vladimir committed
80
		// Calculate Relative similarity of the patch (NN-Model)
81
		double TLDDetector::Sr(const Mat_<uchar>& patch) const
Vladimir's avatar
Vladimir committed
82
		{
83
			double splus = 0.0, sminus = 0.0;
84
            Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
85 86 87
			for (int i = 0; i < *posNum; i++)
			{
				modelSample.data = &(posExp->data[i * 225]);
88
                splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
89
			}
90
            sminus = computeSminus(patch);
91

92 93 94 95 96
			if (splus + sminus == 0.0)
				return 0.0;
			return splus / (sminus + splus);
		}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        std::pair<double, double> TLDDetector::SrAndSc(const Mat_<uchar>& patch) const
        {
            double splusC = 0.0, sminus = 0.0, splus = 0.0;
            Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
            int med = tracking_internal::getMedian((*timeStampsPositive));
            for (int i = 0; i < *posNum; i++)
            {
                modelSample.data = &(posExp->data[i * 225]);
                double s = 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0);

                if ((int)(*timeStampsPositive)[i] <= med)
                    splusC = std::max(splusC, s);

                splus = std::max(splus, s);
            }
            sminus = computeSminus(patch);

            double sr = (splus + sminus == 0.0) ? 0. : splus / (sminus + splus);
            double sc = (splusC + sminus == 0.0) ? 0. : splusC / (sminus + splusC);

            return std::pair<double, double>(sr, sc);
        }

120
#ifdef HAVE_OPENCL
121 122
		double TLDDetector::ocl_Sr(const Mat_<uchar>& patch)
		{
123 124
			double splus = 0.0, sminus = 0.0;

125 126 127 128 129 130

			UMat devPatch = patch.getUMat(ACCESS_READ, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devPositiveSamples = posExp->getUMat(ACCESS_READ, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devNegativeSamples = negExp->getUMat(ACCESS_READ, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devNCC(1, 2*MAX_EXAMPLES_IN_MODEL, CV_32FC1, ACCESS_RW, USAGE_ALLOCATE_DEVICE_MEMORY);

131

132 133 134
			ocl::Kernel k;
			ocl::ProgramSource src = ocl::tracking::tldDetector_oclsrc;
			String error;
135
			ocl::Program prog(src, String(), error);
136 137 138 139 140 141 142 143
			k.create("NCC", prog);
			if (k.empty())
				printf("Kernel create failed!!!\n");
			k.args(
				ocl::KernelArg::PtrReadOnly(devPatch),
				ocl::KernelArg::PtrReadOnly(devPositiveSamples),
				ocl::KernelArg::PtrReadOnly(devNegativeSamples),
				ocl::KernelArg::PtrWriteOnly(devNCC),
144 145
				*posNum,
				*negNum);
146 147

			size_t globSize = 1000;
148

Vladimir's avatar
Vladimir committed
149
			if (!k.run(1, &globSize, NULL, false))
150 151 152
				printf("Kernel Run Error!!!");

			Mat resNCC = devNCC.getMat(ACCESS_READ);
153

154 155 156 157 158 159
			for (int i = 0; i < *posNum; i++)
				splus = std::max(splus, 0.5 * (resNCC.at<float>(i) + 1.0));

			for (int i = 0; i < *negNum; i++)
				sminus = std::max(sminus, 0.5 * (resNCC.at<float>(i+500) +1.0));

Vladimir's avatar
Vladimir committed
160 161 162 163 164
			if (splus + sminus == 0.0)
				return 0.0;
			return splus / (sminus + splus);
		}

165 166 167 168 169 170 171 172 173 174 175
		void TLDDetector::ocl_batchSrSc(const Mat_<uchar>& patches, double *resultSr, double *resultSc, int numOfPatches)
		{
			UMat devPatches = patches.getUMat(ACCESS_READ, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devPositiveSamples = posExp->getUMat(ACCESS_READ, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devNegativeSamples = negExp->getUMat(ACCESS_READ, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devPosNCC(MAX_EXAMPLES_IN_MODEL, numOfPatches, CV_32FC1, ACCESS_RW, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devNegNCC(MAX_EXAMPLES_IN_MODEL, numOfPatches, CV_32FC1, ACCESS_RW, USAGE_ALLOCATE_DEVICE_MEMORY);

			ocl::Kernel k;
			ocl::ProgramSource src = ocl::tracking::tldDetector_oclsrc;
			String error;
176
			ocl::Program prog(src, String(), error);
177 178 179 180 181 182 183 184 185
			k.create("batchNCC", prog);
			if (k.empty())
				printf("Kernel create failed!!!\n");
			k.args(
				ocl::KernelArg::PtrReadOnly(devPatches),
				ocl::KernelArg::PtrReadOnly(devPositiveSamples),
				ocl::KernelArg::PtrReadOnly(devNegativeSamples),
				ocl::KernelArg::PtrWriteOnly(devPosNCC),
				ocl::KernelArg::PtrWriteOnly(devNegNCC),
186 187
				*posNum,
				*negNum,
188 189 190
				numOfPatches);

			size_t globSize = 2 * numOfPatches*MAX_EXAMPLES_IN_MODEL;
191

192
			if (!k.run(1, &globSize, NULL, true))
193 194 195 196 197 198
				printf("Kernel Run Error!!!");

			Mat posNCC = devPosNCC.getMat(ACCESS_READ);
			Mat negNCC = devNegNCC.getMat(ACCESS_READ);

			//Calculate Srs
Vladimir's avatar
Vladimir committed
199
			for (int id = 0; id < numOfPatches; id++)
200 201
			{
				double spr = 0.0, smr = 0.0, spc = 0.0, smc = 0;
202
                int med = tracking_internal::getMedian((*timeStampsPositive));
203 204
				for (int i = 0; i < *posNum; i++)
				{
Vladimir's avatar
Vladimir committed
205
					spr = std::max(spr, 0.5 * (posNCC.at<float>(id * 500 + i) + 1.0));
206
					if ((int)(*timeStampsPositive)[i] <= med)
Vladimir's avatar
Vladimir committed
207
						spc = std::max(spr, 0.5 * (posNCC.at<float>(id * 500 + i) + 1.0));
208 209
				}
				for (int i = 0; i < *negNum; i++)
Vladimir's avatar
Vladimir committed
210
					smc = smr = std::max(smr, 0.5 * (negNCC.at<float>(id * 500 + i) + 1.0));
211 212

				if (spr + smr == 0.0)
Vladimir's avatar
Vladimir committed
213
					resultSr[id] = 0.0;
214
				else
Vladimir's avatar
Vladimir committed
215
					resultSr[id] = spr / (smr + spr);
216 217

				if (spc + smc == 0.0)
Vladimir's avatar
Vladimir committed
218
					resultSc[id] = 0.0;
219
				else
Vladimir's avatar
Vladimir committed
220
					resultSc[id] = spc / (smc + spc);
221 222
			}
		}
223
#endif
224

Vladimir's avatar
Vladimir committed
225
		// Calculate Conservative similarity of the patch (NN-Model)
226
		double TLDDetector::Sc(const Mat_<uchar>& patch) const
Vladimir's avatar
Vladimir committed
227
		{
228 229
			double splus = 0.0, sminus = 0.0;
			Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
230
            int med = tracking_internal::getMedian((*timeStampsPositive));
231 232 233 234 235
			for (int i = 0; i < *posNum; i++)
			{
				if ((int)(*timeStampsPositive)[i] <= med)
				{
					modelSample.data = &(posExp->data[i * 225]);
236
                    splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
237 238
				}
			}
239
            sminus = computeSminus(patch);
240

241 242 243 244 245 246
			if (splus + sminus == 0.0)
				return 0.0;

			return splus / (sminus + splus);
		}

247
#ifdef HAVE_OPENCL
248 249 250 251 252 253 254 255 256 257 258 259 260
		double TLDDetector::ocl_Sc(const Mat_<uchar>& patch)
		{
			double splus = 0.0, sminus = 0.0;

			UMat devPatch = patch.getUMat(ACCESS_READ, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devPositiveSamples = posExp->getUMat(ACCESS_READ, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devNegativeSamples = negExp->getUMat(ACCESS_READ, USAGE_ALLOCATE_DEVICE_MEMORY);
			UMat devNCC(1, 2 * MAX_EXAMPLES_IN_MODEL, CV_32FC1, ACCESS_RW, USAGE_ALLOCATE_DEVICE_MEMORY);


			ocl::Kernel k;
			ocl::ProgramSource src = ocl::tracking::tldDetector_oclsrc;
			String error;
261
			ocl::Program prog(src, String(), error);
262 263 264 265 266 267 268 269
			k.create("NCC", prog);
			if (k.empty())
				printf("Kernel create failed!!!\n");
			k.args(
				ocl::KernelArg::PtrReadOnly(devPatch),
				ocl::KernelArg::PtrReadOnly(devPositiveSamples),
				ocl::KernelArg::PtrReadOnly(devNegativeSamples),
				ocl::KernelArg::PtrWriteOnly(devNCC),
270 271
				*posNum,
				*negNum);
272 273

			size_t globSize = 1000;
274

Vladimir's avatar
Vladimir committed
275
			if (!k.run(1, &globSize, NULL, false))
276 277 278 279
				printf("Kernel Run Error!!!");

			Mat resNCC = devNCC.getMat(ACCESS_READ);

280
            int med = tracking_internal::getMedian((*timeStampsPositive));
281 282 283 284 285 286 287
			for (int i = 0; i < *posNum; i++)
				if ((int)(*timeStampsPositive)[i] <= med)
					splus = std::max(splus, 0.5 * (resNCC.at<float>(i) +1.0));

			for (int i = 0; i < *negNum; i++)
				sminus = std::max(sminus, 0.5 * (resNCC.at<float>(i + 500) + 1.0));

Vladimir's avatar
Vladimir committed
288 289 290 291
			if (splus + sminus == 0.0)
				return 0.0;
			return splus / (sminus + splus);
		}
292
#endif // HAVE_OPENCL
Vladimir's avatar
Vladimir committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

		// Generate Search Windows for detector from aspect ratio of initial BBs
		void TLDDetector::generateScanGrid(int rows, int cols, Size initBox, std::vector<Rect2d>& res, bool withScaling)
		{
			res.clear();
			//Scales step: SCALE_STEP; Translation steps: 10% of width & 10% of height; minSize: 20pix
			for (double h = initBox.height, w = initBox.width; h < cols && w < rows;)
			{
				for (double x = 0; (x + w + 1.0) <= cols; x += (0.1 * w))
				{
					for (double y = 0; (y + h + 1.0) <= rows; y += (0.1 * h))
						res.push_back(Rect2d(x, y, w, h));
				}
				if (withScaling)
				{
					if (h <= initBox.height)
					{
						h /= SCALE_STEP; w /= SCALE_STEP;
						if (h < 20 || w < 20)
						{
							h = initBox.height * SCALE_STEP; w = initBox.width * SCALE_STEP;
							CV_Assert(h > initBox.height || w > initBox.width);
						}
					}
					else
					{
						h *= SCALE_STEP; w *= SCALE_STEP;
					}
				}
				else
				{
					break;
				}
			}
		}

		//Detection - returns most probable new target location (Max Sc)
330

331 332 333 334 335 336 337 338 339
		class CalcScSrParallelLoopBody: public cv::ParallelLoopBody
		{
		public:
			explicit CalcScSrParallelLoopBody (TLDDetector * detector, Size initSize):
				detectorF (detector),
				initSizeF (initSize)
			{
			}

340
			virtual void operator () (const cv::Range & r) const CV_OVERRIDE
341 342 343 344 345 346
			{
				for (int ind = r.start; ind < r.end; ++ind)
				{
					resample(detectorF->resized_imgs[detectorF->ensScaleIDs[ind]],
						Rect2d(detectorF->ensBuffer[ind], initSizeF),
						detectorF->standardPatches[ind]);
347 348 349
                    std::pair<double, double> values = detectorF->SrAndSc(detectorF->standardPatches[ind]);
                    detectorF->scValues[ind] = values.second;
                    detectorF->srValues[ind] = values.first;
350 351 352 353 354 355 356 357 358 359
				}
			}

			TLDDetector * detectorF;
			const Size initSizeF;
		private:
			CalcScSrParallelLoopBody (const CalcScSrParallelLoopBody&);
			CalcScSrParallelLoopBody& operator= (const CalcScSrParallelLoopBody&);
		};

Vladimir's avatar
Vladimir committed
360 361 362
		bool TLDDetector::detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize)
		{
			patches.clear();
363
			Mat tmp;
Vladimir's avatar
Vladimir committed
364 365 366 367
			int dx = initSize.width / 10, dy = initSize.height / 10;
			Size2d size = img.size();
			double scale = 1.0;
			int npos = 0, nneg = 0;
368
			double maxSc = -5.0;
Vladimir's avatar
Vladimir committed
369
			Rect2d maxScRect;
370
			int scaleID;
371 372 373 374 375 376 377

			resized_imgs.clear ();
			blurred_imgs.clear ();
			varBuffer.clear ();
			ensBuffer.clear ();
			varScaleIDs.clear ();
			ensScaleIDs.clear ();
Vladimir's avatar
Vladimir committed
378

Vladimir's avatar
Vladimir committed
379
			//Detection part
380 381 382 383
			//Generate windows and filter by variance
			scaleID = 0;
			resized_imgs.push_back(img);
			blurred_imgs.push_back(imgBlurred);
Vladimir's avatar
Vladimir committed
384 385 386
			do
			{
				Mat_<double> intImgP, intImgP2;
387 388
				computeIntegralImages(resized_imgs[scaleID], intImgP, intImgP2);
				for (int i = 0, imax = cvFloor((0.0 + resized_imgs[scaleID].cols - initSize.width) / dx); i < imax; i++)
Vladimir's avatar
Vladimir committed
389
				{
390
					for (int j = 0, jmax = cvFloor((0.0 + resized_imgs[scaleID].rows - initSize.height) / dy); j < jmax; j++)
Vladimir's avatar
Vladimir committed
391
					{
Vladimir's avatar
Vladimir committed
392
						if (!patchVariance(intImgP, intImgP2, originalVariancePtr, Point(dx * i, dy * j), initSize))
Vladimir's avatar
Vladimir committed
393
							continue;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
						varBuffer.push_back(Point(dx * i, dy * j));
						varScaleIDs.push_back(scaleID);
					}
				}
				scaleID++;
				size.width /= SCALE_STEP;
				size.height /= SCALE_STEP;
				scale *= SCALE_STEP;
				resize(img, tmp, size, 0, 0, DOWNSCALE_MODE);
				resized_imgs.push_back(tmp);
				GaussianBlur(resized_imgs[scaleID], tmp, GaussBlurKernelSize, 0.0f);
				blurred_imgs.push_back(tmp);
			} while (size.width >= initSize.width && size.height >= initSize.height);

			//Encsemble classification
Vladimir's avatar
Vladimir committed
409
			for (int i = 0; i < (int)varBuffer.size(); i++)
410
			{
Vladimir's avatar
Vladimir committed
411
				prepareClassifiers(static_cast<int> (blurred_imgs[varScaleIDs[i]].step[0]));
412 413 414 415 416 417
				if (ensembleClassifierNum(&blurred_imgs[varScaleIDs[i]].at<uchar>(varBuffer[i].y, varBuffer[i].x)) <= ENSEMBLE_THRESHOLD)
					continue;
				ensBuffer.push_back(varBuffer[i]);
				ensScaleIDs.push_back(varScaleIDs[i]);
			}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
			//Batch preparation
			srValues.resize (ensBuffer.size());
			scValues.resize (ensBuffer.size());

			//Carefully resize standard patches with reference-counted Mat members
			const int oldPatchesSize = (int)standardPatches.size();
			standardPatches.resize (ensBuffer.size());
			if ((int)ensBuffer.size() > oldPatchesSize)
			{
				Mat_<uchar> standardPatch(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
				for (int i = oldPatchesSize; i < (int)ensBuffer.size(); ++i)
				{
					standardPatches[i] = standardPatch.clone();
				}
			}

			//Batch calculation
			cv::parallel_for_ (cv::Range (0, (int)ensBuffer.size ()), CalcScSrParallelLoopBody (this, initSize));

437
			//NN classification
Vladimir's avatar
Vladimir committed
438
			for (int i = 0; i < (int)ensBuffer.size(); i++)
439 440 441 442 443
			{
				LabeledPatch labPatch;
				double curScale = pow(SCALE_STEP, ensScaleIDs[i]);
				labPatch.rect = Rect2d(ensBuffer[i].x*curScale, ensBuffer[i].y*curScale, initSize.width * curScale, initSize.height * curScale);

444 445
				const double srValue = srValues[i];
				const double scValue = scValues[i];
446 447 448

				////To fix: Check the paper, probably this cause wrong learning
				//
449
                labPatch.isObject = srValue > THETA_NN;
450
                labPatch.shouldBeIntegrated = abs(srValue - THETA_NN) < CLASSIFIER_MARGIN;
451
                patches.push_back(labPatch);
452 453 454 455 456 457 458 459 460 461 462
				//

				if (!labPatch.isObject)
				{
					nneg++;
					continue;
				}
				else
				{
					npos++;
				}
463

464 465 466 467 468 469
				if (scValue > maxSc)
				{
					maxSc = scValue;
					maxScRect = labPatch.rect;
				}
			}
Vladimir's avatar
Vladimir committed
470

471
            if (maxSc < 0)
472
				return false;
473 474 475 476 477
			else
			{
				res = maxScRect;
				return true;
			}
478
		}
Vladimir's avatar
Vladimir committed
479

480
#ifdef HAVE_OPENCL
481 482 483 484 485 486 487 488 489 490 491 492 493 494
		bool TLDDetector::ocl_detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize)
		{
			patches.clear();
			Mat_<uchar> standardPatch(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
			Mat tmp;
			int dx = initSize.width / 10, dy = initSize.height / 10;
			Size2d size = img.size();
			double scale = 1.0;
			int npos = 0, nneg = 0;
			double maxSc = -5.0;
			Rect2d maxScRect;
			int scaleID;
			std::vector <Mat> resized_imgs, blurred_imgs;
			std::vector <Point> varBuffer, ensBuffer;
Vladimir's avatar
Vladimir committed
495
			std::vector <int> varScaleIDs, ensScaleIDs;
Vladimir's avatar
Vladimir committed
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510
			//Detection part
			//Generate windows and filter by variance
			scaleID = 0;
			resized_imgs.push_back(img);
			blurred_imgs.push_back(imgBlurred);
			do
			{
				Mat_<double> intImgP, intImgP2;
				computeIntegralImages(resized_imgs[scaleID], intImgP, intImgP2);
				for (int i = 0, imax = cvFloor((0.0 + resized_imgs[scaleID].cols - initSize.width) / dx); i < imax; i++)
				{
					for (int j = 0, jmax = cvFloor((0.0 + resized_imgs[scaleID].rows - initSize.height) / dy); j < jmax; j++)
					{
						if (!patchVariance(intImgP, intImgP2, originalVariancePtr, Point(dx * i, dy * j), initSize))
Vladimir's avatar
Vladimir committed
511
							continue;
512 513
						varBuffer.push_back(Point(dx * i, dy * j));
						varScaleIDs.push_back(scaleID);
Vladimir's avatar
Vladimir committed
514 515
					}
				}
516
				scaleID++;
Vladimir's avatar
Vladimir committed
517 518 519
				size.width /= SCALE_STEP;
				size.height /= SCALE_STEP;
				scale *= SCALE_STEP;
520 521 522 523
				resize(img, tmp, size, 0, 0, DOWNSCALE_MODE);
				resized_imgs.push_back(tmp);
				GaussianBlur(resized_imgs[scaleID], tmp, GaussBlurKernelSize, 0.0f);
				blurred_imgs.push_back(tmp);
Vladimir's avatar
Vladimir committed
524
			} while (size.width >= initSize.width && size.height >= initSize.height);
525 526

			//Encsemble classification
Vladimir's avatar
Vladimir committed
527
			for (int i = 0; i < (int)varBuffer.size(); i++)
528 529 530 531 532 533 534 535 536 537
			{
				prepareClassifiers((int)blurred_imgs[varScaleIDs[i]].step[0]);
				if (ensembleClassifierNum(&blurred_imgs[varScaleIDs[i]].at<uchar>(varBuffer[i].y, varBuffer[i].x)) <= ENSEMBLE_THRESHOLD)
					continue;
				ensBuffer.push_back(varBuffer[i]);
				ensScaleIDs.push_back(varScaleIDs[i]);
			}

			//NN classification
			//Prepare batch of patches
Vladimir's avatar
Vladimir committed
538
			int numOfPatches = (int)ensBuffer.size();
539 540 541 542 543
			Mat_<uchar> stdPatches(numOfPatches, 225);
			double *resultSr = new double[numOfPatches];
			double *resultSc = new double[numOfPatches];

			uchar *patchesData = stdPatches.data;
Vladimir's avatar
Vladimir committed
544
			for (int i = 0; i < (int)ensBuffer.size(); i++)
545 546 547 548 549 550 551 552 553 554
			{
				resample(resized_imgs[ensScaleIDs[i]], Rect2d(ensBuffer[i], initSize), standardPatch);
				uchar *stdPatchData = standardPatch.data;
				for (int j = 0; j < 225; j++)
					patchesData[225*i+j] = stdPatchData[j];
			}
			//Calculate Sr and Sc batches
			ocl_batchSrSc(stdPatches, resultSr, resultSc, numOfPatches);


Vladimir's avatar
Vladimir committed
555
			for (int i = 0; i < (int)ensBuffer.size(); i++)
556 557 558 559 560 561 562 563 564 565 566 567 568
			{
				LabeledPatch labPatch;
				standardPatch.data = &stdPatches.data[225 * i];
				double curScale = pow(SCALE_STEP, ensScaleIDs[i]);
				labPatch.rect = Rect2d(ensBuffer[i].x*curScale, ensBuffer[i].y*curScale, initSize.width * curScale, initSize.height * curScale);

				double srValue, scValue;

				srValue = resultSr[i];

				////To fix: Check the paper, probably this cause wrong learning
				//
				labPatch.isObject = srValue > THETA_NN;
569
                labPatch.shouldBeIntegrated = abs(srValue - THETA_NN) < CLASSIFIER_MARGIN;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
				patches.push_back(labPatch);
				//

				if (!labPatch.isObject)
				{
					nneg++;
					continue;
				}
				else
				{
					npos++;
				}
				scValue = resultSc[i];
				if (scValue > maxSc)
				{
					maxSc = scValue;
					maxScRect = labPatch.rect;
				}
			}
Vladimir's avatar
Vladimir committed
589 590 591 592 593 594

			if (maxSc < 0)
				return false;
			res = maxScRect;
			return true;
		}
595
#endif // HAVE_OPENCL
Vladimir's avatar
Vladimir committed
596 597 598

		// Computes the variance of subimage given by box, with the help of two integral
		// images intImgP and intImgP2 (sum of squares), which should be also provided.
Vladimir's avatar
Vladimir committed
599
		bool TLDDetector::patchVariance(Mat_<double>& intImgP, Mat_<double>& intImgP2, double *originalVariance, Point pt, Size size)
Vladimir's avatar
Vladimir committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		{
			int x = (pt.x), y = (pt.y), width = (size.width), height = (size.height);
			CV_Assert(0 <= x && (x + width) < intImgP.cols && (x + width) < intImgP2.cols);
			CV_Assert(0 <= y && (y + height) < intImgP.rows && (y + height) < intImgP2.rows);
			double p = 0, p2 = 0;
			double A, B, C, D;

			A = intImgP(y, x);
			B = intImgP(y, x + width);
			C = intImgP(y + height, x);
			D = intImgP(y + height, x + width);
			p = (A + D - B - C) / (width * height);

			A = intImgP2(y, x);
			B = intImgP2(y, x + width);
			C = intImgP2(y + height, x);
			D = intImgP2(y + height, x + width);
			p2 = (A + D - B - C) / (width * height);

Vladimir's avatar
Vladimir committed
619
			return ((p2 - p * p) > VARIANCE_THRESHOLD * *originalVariance);
Vladimir's avatar
Vladimir committed
620 621 622
		}

	}
623
}