2010-05-12 01:44:00 +08:00
|
|
|
/*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.
|
|
|
|
//
|
|
|
|
//
|
2013-12-13 01:58:42 +08:00
|
|
|
// License Agreement
|
2010-05-12 01:44:00 +08:00
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
2013-12-13 01:58:42 +08:00
|
|
|
// Copyright (C) 2008-2013, Itseez Inc., all rights reserved.
|
2010-05-12 01:44:00 +08:00
|
|
|
// 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.
|
|
|
|
//
|
2013-12-13 01:58:42 +08:00
|
|
|
// * The name of Itseez Inc. may not be used to endorse or promote products
|
2010-05-12 01:44:00 +08:00
|
|
|
// 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.
|
2013-12-13 01:58:42 +08:00
|
|
|
// In no event shall the copyright holders or contributors be liable for any direct,
|
2010-05-12 01:44:00 +08:00
|
|
|
// 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*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
#include <cstdio>
|
|
|
|
|
2011-07-29 22:18:45 +08:00
|
|
|
#include "cascadedetect.hpp"
|
2013-04-12 16:11:11 +08:00
|
|
|
#include "opencv2/objdetect/objdetect_c.h"
|
2013-12-18 00:55:49 +08:00
|
|
|
#include "opencl_kernels.hpp"
|
2011-07-29 22:18:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
2013-12-19 20:57:02 +08:00
|
|
|
|
2013-12-17 18:29:30 +08:00
|
|
|
template<typename _Tp> void copyVectorToUMat(const std::vector<_Tp>& v, UMat& um)
|
|
|
|
{
|
2013-12-19 20:31:30 +08:00
|
|
|
if(v.empty())
|
|
|
|
um.release();
|
|
|
|
Mat(1, (int)(v.size()*sizeof(v[0])), CV_8U, (void*)&v[0]).copyTo(um);
|
2013-12-17 18:29:30 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
void groupRectangles(std::vector<Rect>& rectList, int groupThreshold, double eps,
|
|
|
|
std::vector<int>* weights, std::vector<double>* levelWeights)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
if( groupThreshold <= 0 || rectList.empty() )
|
|
|
|
{
|
|
|
|
if( weights )
|
|
|
|
{
|
|
|
|
size_t i, sz = rectList.size();
|
|
|
|
weights->resize(sz);
|
|
|
|
for( i = 0; i < sz; i++ )
|
|
|
|
(*weights)[i] = 1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<int> labels;
|
2010-05-12 01:44:00 +08:00
|
|
|
int nclasses = partition(rectList, labels, SimilarRects(eps));
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Rect> rrects(nclasses);
|
|
|
|
std::vector<int> rweights(nclasses, 0);
|
|
|
|
std::vector<int> rejectLevels(nclasses, 0);
|
|
|
|
std::vector<double> rejectWeights(nclasses, DBL_MIN);
|
2010-05-12 01:44:00 +08:00
|
|
|
int i, j, nlabels = (int)labels.size();
|
|
|
|
for( i = 0; i < nlabels; i++ )
|
|
|
|
{
|
|
|
|
int cls = labels[i];
|
|
|
|
rrects[cls].x += rectList[i].x;
|
|
|
|
rrects[cls].y += rectList[i].y;
|
|
|
|
rrects[cls].width += rectList[i].width;
|
|
|
|
rrects[cls].height += rectList[i].height;
|
|
|
|
rweights[cls]++;
|
|
|
|
}
|
2014-02-12 21:42:09 +08:00
|
|
|
|
2014-02-13 00:28:55 +08:00
|
|
|
bool useDefaultWeights = false;
|
2014-02-12 21:42:09 +08:00
|
|
|
|
2011-04-22 18:03:05 +08:00
|
|
|
if ( levelWeights && weights && !weights->empty() && !levelWeights->empty() )
|
2012-06-09 23:00:04 +08:00
|
|
|
{
|
|
|
|
for( i = 0; i < nlabels; i++ )
|
|
|
|
{
|
|
|
|
int cls = labels[i];
|
2011-04-22 18:03:05 +08:00
|
|
|
if( (*weights)[i] > rejectLevels[cls] )
|
|
|
|
{
|
|
|
|
rejectLevels[cls] = (*weights)[i];
|
|
|
|
rejectWeights[cls] = (*levelWeights)[i];
|
|
|
|
}
|
|
|
|
else if( ( (*weights)[i] == rejectLevels[cls] ) && ( (*levelWeights)[i] > rejectWeights[cls] ) )
|
|
|
|
rejectWeights[cls] = (*levelWeights)[i];
|
2012-06-09 23:00:04 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-13 00:28:55 +08:00
|
|
|
else
|
|
|
|
useDefaultWeights = true;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( i = 0; i < nclasses; i++ )
|
|
|
|
{
|
|
|
|
Rect r = rrects[i];
|
|
|
|
float s = 1.f/rweights[i];
|
|
|
|
rrects[i] = Rect(saturate_cast<int>(r.x*s),
|
|
|
|
saturate_cast<int>(r.y*s),
|
|
|
|
saturate_cast<int>(r.width*s),
|
|
|
|
saturate_cast<int>(r.height*s));
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
rectList.clear();
|
|
|
|
if( weights )
|
|
|
|
weights->clear();
|
2012-06-09 23:00:04 +08:00
|
|
|
if( levelWeights )
|
|
|
|
levelWeights->clear();
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( i = 0; i < nclasses; i++ )
|
|
|
|
{
|
|
|
|
Rect r1 = rrects[i];
|
2013-06-01 08:34:20 +08:00
|
|
|
int n1 = rweights[i];
|
2012-06-09 23:00:04 +08:00
|
|
|
double w1 = rejectWeights[i];
|
2013-06-01 08:34:20 +08:00
|
|
|
int l1 = rejectLevels[i];
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-06-01 08:34:20 +08:00
|
|
|
// filter out rectangles which don't have enough similar rectangles
|
2010-05-12 01:44:00 +08:00
|
|
|
if( n1 <= groupThreshold )
|
|
|
|
continue;
|
|
|
|
// filter out small face rectangles inside large rectangles
|
|
|
|
for( j = 0; j < nclasses; j++ )
|
|
|
|
{
|
|
|
|
int n2 = rweights[j];
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( j == i || n2 <= groupThreshold )
|
|
|
|
continue;
|
|
|
|
Rect r2 = rrects[j];
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int dx = saturate_cast<int>( r2.width * eps );
|
|
|
|
int dy = saturate_cast<int>( r2.height * eps );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( i != j &&
|
|
|
|
r1.x >= r2.x - dx &&
|
|
|
|
r1.y >= r2.y - dy &&
|
|
|
|
r1.x + r1.width <= r2.x + r2.width + dx &&
|
|
|
|
r1.y + r1.height <= r2.y + r2.height + dy &&
|
|
|
|
(n2 > std::max(3, n1) || n1 < 3) )
|
|
|
|
break;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( j == nclasses )
|
|
|
|
{
|
|
|
|
rectList.push_back(r1);
|
|
|
|
if( weights )
|
2014-02-12 21:42:09 +08:00
|
|
|
weights->push_back(useDefaultWeights ? n1 : l1);
|
2012-06-09 23:00:04 +08:00
|
|
|
if( levelWeights )
|
|
|
|
levelWeights->push_back(w1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-23 00:11:35 +08:00
|
|
|
class MeanshiftGrouping
|
|
|
|
{
|
|
|
|
public:
|
2013-02-25 00:14:01 +08:00
|
|
|
MeanshiftGrouping(const Point3d& densKer, const std::vector<Point3d>& posV,
|
|
|
|
const std::vector<double>& wV, double eps, int maxIter = 20)
|
2011-04-23 00:11:35 +08:00
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
densityKernel = densKer;
|
2011-04-23 00:11:35 +08:00
|
|
|
weightsV = wV;
|
|
|
|
positionsV = posV;
|
2011-07-19 20:27:07 +08:00
|
|
|
positionsCount = (int)posV.size();
|
2012-06-09 23:00:04 +08:00
|
|
|
meanshiftV.resize(positionsCount);
|
2011-04-23 00:11:35 +08:00
|
|
|
distanceV.resize(positionsCount);
|
2012-06-09 23:00:04 +08:00
|
|
|
iterMax = maxIter;
|
2013-01-24 18:40:35 +08:00
|
|
|
modeEps = eps;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i<positionsV.size(); i++)
|
|
|
|
{
|
|
|
|
meanshiftV[i] = getNewValue(positionsV[i]);
|
|
|
|
distanceV[i] = moveToMode(meanshiftV[i]);
|
|
|
|
meanshiftV[i] -= positionsV[i];
|
|
|
|
}
|
2011-04-23 00:11:35 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void getModes(std::vector<Point3d>& modesV, std::vector<double>& resWeightsV, const double eps)
|
2011-04-23 00:11:35 +08:00
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
for (size_t i=0; i <distanceV.size(); i++)
|
|
|
|
{
|
|
|
|
bool is_found = false;
|
|
|
|
for(size_t j=0; j<modesV.size(); j++)
|
|
|
|
{
|
|
|
|
if ( getDistance(distanceV[i], modesV[j]) < eps)
|
|
|
|
{
|
|
|
|
is_found=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_found)
|
|
|
|
{
|
|
|
|
modesV.push_back(distanceV[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resWeightsV.resize(modesV.size());
|
|
|
|
|
|
|
|
for (size_t i=0; i<modesV.size(); i++)
|
|
|
|
{
|
|
|
|
resWeightsV[i] = getResultWeight(modesV[i]);
|
|
|
|
}
|
2011-04-23 00:11:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Point3d> positionsV;
|
|
|
|
std::vector<double> weightsV;
|
2011-04-23 00:11:35 +08:00
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
Point3d densityKernel;
|
|
|
|
int positionsCount;
|
2011-04-23 00:11:35 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Point3d> meanshiftV;
|
|
|
|
std::vector<Point3d> distanceV;
|
2012-06-09 23:00:04 +08:00
|
|
|
int iterMax;
|
|
|
|
double modeEps;
|
2011-04-23 00:11:35 +08:00
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
Point3d getNewValue(const Point3d& inPt) const
|
2011-04-23 00:11:35 +08:00
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
Point3d resPoint(.0);
|
|
|
|
Point3d ratPoint(.0);
|
|
|
|
for (size_t i=0; i<positionsV.size(); i++)
|
|
|
|
{
|
|
|
|
Point3d aPt= positionsV[i];
|
|
|
|
Point3d bPt = inPt;
|
|
|
|
Point3d sPt = densityKernel;
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
sPt.x *= std::exp(aPt.z);
|
|
|
|
sPt.y *= std::exp(aPt.z);
|
2012-06-09 23:00:04 +08:00
|
|
|
|
|
|
|
aPt.x /= sPt.x;
|
|
|
|
aPt.y /= sPt.y;
|
|
|
|
aPt.z /= sPt.z;
|
|
|
|
|
|
|
|
bPt.x /= sPt.x;
|
|
|
|
bPt.y /= sPt.y;
|
|
|
|
bPt.z /= sPt.z;
|
|
|
|
|
|
|
|
double w = (weightsV[i])*std::exp(-((aPt-bPt).dot(aPt-bPt))/2)/std::sqrt(sPt.dot(Point3d(1,1,1)));
|
|
|
|
|
|
|
|
resPoint += w*aPt;
|
|
|
|
|
|
|
|
ratPoint.x += w/sPt.x;
|
|
|
|
ratPoint.y += w/sPt.y;
|
|
|
|
ratPoint.z += w/sPt.z;
|
|
|
|
}
|
|
|
|
resPoint.x /= ratPoint.x;
|
|
|
|
resPoint.y /= ratPoint.y;
|
|
|
|
resPoint.z /= ratPoint.z;
|
|
|
|
return resPoint;
|
2011-04-23 00:11:35 +08:00
|
|
|
}
|
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
double getResultWeight(const Point3d& inPt) const
|
2011-04-23 00:11:35 +08:00
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
double sumW=0;
|
|
|
|
for (size_t i=0; i<positionsV.size(); i++)
|
|
|
|
{
|
|
|
|
Point3d aPt = positionsV[i];
|
|
|
|
Point3d sPt = densityKernel;
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
sPt.x *= std::exp(aPt.z);
|
|
|
|
sPt.y *= std::exp(aPt.z);
|
2012-06-09 23:00:04 +08:00
|
|
|
|
|
|
|
aPt -= inPt;
|
|
|
|
|
|
|
|
aPt.x /= sPt.x;
|
|
|
|
aPt.y /= sPt.y;
|
|
|
|
aPt.z /= sPt.z;
|
|
|
|
|
|
|
|
sumW+=(weightsV[i])*std::exp(-(aPt.dot(aPt))/2)/std::sqrt(sPt.dot(Point3d(1,1,1)));
|
|
|
|
}
|
|
|
|
return sumW;
|
2011-04-23 00:11:35 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
|
|
|
Point3d moveToMode(Point3d aPt) const
|
2011-04-23 00:11:35 +08:00
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
Point3d bPt;
|
|
|
|
for (int i = 0; i<iterMax; i++)
|
|
|
|
{
|
|
|
|
bPt = aPt;
|
|
|
|
aPt = getNewValue(bPt);
|
|
|
|
if ( getDistance(aPt, bPt) <= modeEps )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aPt;
|
2011-04-23 00:11:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double getDistance(Point3d p1, Point3d p2) const
|
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
Point3d ns = densityKernel;
|
2013-02-25 00:14:01 +08:00
|
|
|
ns.x *= std::exp(p2.z);
|
|
|
|
ns.y *= std::exp(p2.z);
|
2012-06-09 23:00:04 +08:00
|
|
|
p2 -= p1;
|
|
|
|
p2.x /= ns.x;
|
|
|
|
p2.y /= ns.y;
|
|
|
|
p2.z /= ns.z;
|
|
|
|
return p2.dot(p2);
|
2011-04-23 00:11:35 +08:00
|
|
|
}
|
|
|
|
};
|
2011-04-19 17:05:15 +08:00
|
|
|
//new grouping function with using meanshift
|
2013-02-25 00:14:01 +08:00
|
|
|
static void groupRectangles_meanshift(std::vector<Rect>& rectList, double detectThreshold, std::vector<double>* foundWeights,
|
|
|
|
std::vector<double>& scales, Size winDetSize)
|
2011-04-19 17:05:15 +08:00
|
|
|
{
|
2011-07-19 20:27:07 +08:00
|
|
|
int detectionCount = (int)rectList.size();
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Point3d> hits(detectionCount), resultHits;
|
|
|
|
std::vector<double> hitWeights(detectionCount), resultWeights;
|
2011-04-19 17:05:15 +08:00
|
|
|
Point2d hitCenter;
|
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
for (int i=0; i < detectionCount; i++)
|
2011-04-19 17:05:15 +08:00
|
|
|
{
|
|
|
|
hitWeights[i] = (*foundWeights)[i];
|
|
|
|
hitCenter = (rectList[i].tl() + rectList[i].br())*(0.5); //center of rectangles
|
|
|
|
hits[i] = Point3d(hitCenter.x, hitCenter.y, std::log(scales[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
rectList.clear();
|
|
|
|
if (foundWeights)
|
|
|
|
foundWeights->clear();
|
|
|
|
|
|
|
|
double logZ = std::log(1.3);
|
|
|
|
Point3d smothing(8, 16, logZ);
|
|
|
|
|
|
|
|
MeanshiftGrouping msGrouping(smothing, hits, hitWeights, 1e-5, 100);
|
|
|
|
|
|
|
|
msGrouping.getModes(resultHits, resultWeights, 1);
|
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
for (unsigned i=0; i < resultHits.size(); ++i)
|
2011-04-19 17:05:15 +08:00
|
|
|
{
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
double scale = std::exp(resultHits[i].z);
|
2011-04-19 17:05:15 +08:00
|
|
|
hitCenter.x = resultHits[i].x;
|
|
|
|
hitCenter.y = resultHits[i].y;
|
|
|
|
Size s( int(winDetSize.width * scale), int(winDetSize.height * scale) );
|
2012-06-09 23:00:04 +08:00
|
|
|
Rect resultRect( int(hitCenter.x-s.width/2), int(hitCenter.y-s.height/2),
|
|
|
|
int(s.width), int(s.height) );
|
2011-04-19 17:05:15 +08:00
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
if (resultWeights[i] > detectThreshold)
|
2011-04-19 17:05:15 +08:00
|
|
|
{
|
|
|
|
rectList.push_back(resultRect);
|
|
|
|
foundWeights->push_back(resultWeights[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void groupRectangles(std::vector<Rect>& rectList, int groupThreshold, double eps)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-19 17:05:15 +08:00
|
|
|
groupRectangles(rectList, groupThreshold, eps, 0, 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2011-04-19 17:05:15 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void groupRectangles(std::vector<Rect>& rectList, std::vector<int>& weights, int groupThreshold, double eps)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-19 17:05:15 +08:00
|
|
|
groupRectangles(rectList, groupThreshold, eps, &weights, 0);
|
|
|
|
}
|
2011-04-22 18:03:05 +08:00
|
|
|
//used for cascade detection algorithm for ROC-curve calculating
|
2014-02-03 19:12:27 +08:00
|
|
|
void groupRectangles(std::vector<Rect>& rectList, std::vector<int>& rejectLevels,
|
|
|
|
std::vector<double>& levelWeights, int groupThreshold, double eps)
|
2011-04-19 17:05:15 +08:00
|
|
|
{
|
2011-04-22 18:03:05 +08:00
|
|
|
groupRectangles(rectList, groupThreshold, eps, &rejectLevels, &levelWeights);
|
2011-04-19 17:05:15 +08:00
|
|
|
}
|
2011-04-22 18:03:05 +08:00
|
|
|
//can be used for HOG detection algorithm only
|
2013-02-25 00:14:01 +08:00
|
|
|
void groupRectangles_meanshift(std::vector<Rect>& rectList, std::vector<double>& foundWeights,
|
|
|
|
std::vector<double>& foundScales, double detectThreshold, Size winDetSize)
|
2011-04-19 17:05:15 +08:00
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
groupRectangles_meanshift(rectList, detectThreshold, &foundWeights, foundScales, winDetSize);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
FeatureEvaluator::~FeatureEvaluator() {}
|
2014-02-03 19:12:27 +08:00
|
|
|
|
|
|
|
bool FeatureEvaluator::read(const FileNode&, Size _origWinSize)
|
|
|
|
{
|
|
|
|
origWinSize = _origWinSize;
|
|
|
|
localSize = lbufSize = Size(0, 0);
|
|
|
|
if (scaleData.empty())
|
|
|
|
scaleData = makePtr<std::vector<ScaleData> >();
|
|
|
|
else
|
|
|
|
scaleData->clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
Ptr<FeatureEvaluator> FeatureEvaluator::clone() const { return Ptr<FeatureEvaluator>(); }
|
|
|
|
int FeatureEvaluator::getFeatureType() const {return -1;}
|
2014-02-03 19:12:27 +08:00
|
|
|
bool FeatureEvaluator::setWindow(Point, int) { return true; }
|
|
|
|
void FeatureEvaluator::getUMats(std::vector<UMat>& bufs)
|
|
|
|
{
|
|
|
|
if (!(sbufFlag & USBUF_VALID))
|
|
|
|
{
|
|
|
|
sbuf.copyTo(usbuf);
|
|
|
|
sbufFlag |= USBUF_VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufs.clear();
|
|
|
|
bufs.push_back(uscaleData);
|
|
|
|
bufs.push_back(usbuf);
|
|
|
|
bufs.push_back(ufbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureEvaluator::getMats()
|
|
|
|
{
|
|
|
|
if (!(sbufFlag & SBUF_VALID))
|
|
|
|
{
|
|
|
|
usbuf.copyTo(sbuf);
|
|
|
|
sbufFlag |= SBUF_VALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float FeatureEvaluator::calcOrd(int) const { return 0.; }
|
2010-05-12 01:44:00 +08:00
|
|
|
int FeatureEvaluator::calcCat(int) const { return 0; }
|
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
bool FeatureEvaluator::updateScaleData( Size imgsz, const std::vector<float>& _scales )
|
|
|
|
{
|
|
|
|
if( scaleData.empty() )
|
|
|
|
scaleData = makePtr<std::vector<ScaleData> >();
|
|
|
|
|
|
|
|
size_t i, nscales = _scales.size();
|
|
|
|
bool recalcOptFeatures = nscales != scaleData->size();
|
|
|
|
scaleData->resize(nscales);
|
|
|
|
|
|
|
|
int layer_dy = 0;
|
|
|
|
Point layer_ofs(0,0);
|
|
|
|
Size prevBufSize = sbufSize;
|
|
|
|
sbufSize.width = std::max(sbufSize.width, (int)alignSize(cvRound(imgsz.width/_scales[0]) + 31, 32));
|
|
|
|
recalcOptFeatures = recalcOptFeatures || sbufSize.width != prevBufSize.width;
|
|
|
|
|
|
|
|
for( i = 0; i < nscales; i++ )
|
|
|
|
{
|
|
|
|
FeatureEvaluator::ScaleData& s = scaleData->at(i);
|
|
|
|
if( !recalcOptFeatures && fabs(s.scale - _scales[i]) > FLT_EPSILON*100*_scales[i] )
|
|
|
|
recalcOptFeatures = true;
|
|
|
|
float sc = _scales[i];
|
|
|
|
Size sz;
|
|
|
|
sz.width = cvRound(imgsz.width/sc);
|
|
|
|
sz.height = cvRound(imgsz.height/sc);
|
|
|
|
s.ystep = sc >= 2 ? 1 : 2;
|
|
|
|
s.scale = sc;
|
|
|
|
s.szi = Size(sz.width+1, sz.height+1);
|
|
|
|
if( layer_ofs.x + s.szi.width > sbufSize.width )
|
|
|
|
{
|
|
|
|
layer_ofs = Point(0, layer_ofs.y + layer_dy);
|
|
|
|
layer_dy = s.szi.height;
|
|
|
|
}
|
|
|
|
s.layer_ofs = layer_ofs.y*sbufSize.width + layer_ofs.x;
|
|
|
|
layer_ofs.x += s.szi.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
layer_ofs.y += layer_dy;
|
|
|
|
sbufSize.height = std::max(sbufSize.height, layer_ofs.y);
|
|
|
|
recalcOptFeatures = recalcOptFeatures || sbufSize.height != prevBufSize.height;
|
|
|
|
return recalcOptFeatures;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FeatureEvaluator::setImage( InputArray _image, const std::vector<float>& _scales )
|
|
|
|
{
|
|
|
|
Size imgsz = _image.size();
|
|
|
|
bool recalcOptFeatures = updateScaleData(imgsz, _scales);
|
2014-02-03 20:36:36 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
size_t i, nscales = scaleData->size();
|
|
|
|
Size sz0 = scaleData->at(0).szi;
|
|
|
|
sz0 = Size(std::max(rbuf.cols, (int)alignSize(sz0.width, 16)), std::max(rbuf.rows, sz0.height));
|
|
|
|
|
|
|
|
if (recalcOptFeatures)
|
|
|
|
{
|
|
|
|
computeOptFeatures();
|
|
|
|
copyVectorToUMat(*scaleData, uscaleData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_image.isUMat() && localSize.area() > 0)
|
|
|
|
{
|
|
|
|
usbuf.create(sbufSize.height*nchannels, sbufSize.width, CV_32S);
|
|
|
|
urbuf.create(sz0, CV_8U);
|
|
|
|
|
|
|
|
for (i = 0; i < nscales; i++)
|
|
|
|
{
|
|
|
|
const ScaleData& s = scaleData->at(i);
|
|
|
|
UMat dst(urbuf, Rect(0, 0, s.szi.width - 1, s.szi.height - 1));
|
|
|
|
resize(_image, dst, dst.size(), 1. / s.scale, 1. / s.scale, INTER_LINEAR);
|
|
|
|
computeChannels((int)i, dst);
|
|
|
|
}
|
|
|
|
sbufFlag = USBUF_VALID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Mat image = _image.getMat();
|
|
|
|
sbuf.create(sbufSize.height*nchannels, sbufSize.width, CV_32S);
|
|
|
|
rbuf.create(sz0, CV_8U);
|
|
|
|
|
|
|
|
for (i = 0; i < nscales; i++)
|
|
|
|
{
|
|
|
|
const ScaleData& s = scaleData->at(i);
|
|
|
|
Mat dst(s.szi.height - 1, s.szi.width - 1, CV_8U, rbuf.data);
|
|
|
|
resize(image, dst, dst.size(), 1. / s.scale, 1. / s.scale, INTER_LINEAR);
|
|
|
|
computeChannels((int)i, dst);
|
|
|
|
}
|
|
|
|
sbufFlag = SBUF_VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//---------------------------------------------- HaarEvaluator ---------------------------------------
|
|
|
|
|
|
|
|
bool HaarEvaluator::Feature :: read( const FileNode& node )
|
|
|
|
{
|
|
|
|
FileNode rnode = node[CC_RECTS];
|
|
|
|
FileNodeIterator it = rnode.begin(), it_end = rnode.end();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int ri;
|
|
|
|
for( ri = 0; ri < RECT_NUM; ri++ )
|
|
|
|
{
|
|
|
|
rect[ri].r = Rect();
|
|
|
|
rect[ri].weight = 0.f;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for(ri = 0; it != it_end; ++it, ri++)
|
|
|
|
{
|
|
|
|
FileNodeIterator it2 = (*it).begin();
|
|
|
|
it2 >> rect[ri].r.x >> rect[ri].r.y >>
|
|
|
|
rect[ri].r.width >> rect[ri].r.height >> rect[ri].weight;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
tilted = (int)node[CC_TILTED] != 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
HaarEvaluator::HaarEvaluator()
|
|
|
|
{
|
2013-12-13 01:58:42 +08:00
|
|
|
optfeaturesPtr = 0;
|
|
|
|
pwin = 0;
|
2014-02-03 19:12:27 +08:00
|
|
|
localSize = Size(4, 2);
|
|
|
|
lbufSize = Size(0, 0);
|
|
|
|
nchannels = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-02-03 19:12:27 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
HaarEvaluator::~HaarEvaluator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
bool HaarEvaluator::read(const FileNode& node, Size _origWinSize)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
if (!FeatureEvaluator::read(node, _origWinSize))
|
|
|
|
return false;
|
2013-12-11 02:54:37 +08:00
|
|
|
size_t i, n = node.size();
|
|
|
|
CV_Assert(n > 0);
|
2013-12-17 18:29:30 +08:00
|
|
|
if(features.empty())
|
|
|
|
features = makePtr<std::vector<Feature> >();
|
|
|
|
if(optfeatures.empty())
|
|
|
|
optfeatures = makePtr<std::vector<OptFeature> >();
|
2014-02-03 19:12:27 +08:00
|
|
|
if (optfeatures_lbuf.empty())
|
|
|
|
optfeatures_lbuf = makePtr<std::vector<OptFeature> >();
|
2013-12-13 01:58:42 +08:00
|
|
|
features->resize(n);
|
2013-12-11 02:54:37 +08:00
|
|
|
FileNodeIterator it = node.begin();
|
2010-05-12 01:44:00 +08:00
|
|
|
hasTiltedFeatures = false;
|
2013-12-18 00:55:49 +08:00
|
|
|
std::vector<Feature>& ff = *features;
|
2014-02-03 19:12:27 +08:00
|
|
|
sbufSize = Size();
|
2013-12-17 18:29:30 +08:00
|
|
|
ufbuf.release();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-12-11 02:54:37 +08:00
|
|
|
for(i = 0; i < n; i++, ++it)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-12-13 01:58:42 +08:00
|
|
|
if(!ff[i].read(*it))
|
2010-05-12 01:44:00 +08:00
|
|
|
return false;
|
2013-12-13 01:58:42 +08:00
|
|
|
if( ff[i].tilted )
|
2010-05-12 01:44:00 +08:00
|
|
|
hasTiltedFeatures = true;
|
|
|
|
}
|
2014-02-03 19:12:27 +08:00
|
|
|
nchannels = hasTiltedFeatures ? 3 : 2;
|
|
|
|
normrect = Rect(1, 1, origWinSize.width - 2, origWinSize.height - 2);
|
|
|
|
|
2014-02-12 21:42:09 +08:00
|
|
|
localSize = lbufSize = Size(0, 0);
|
2014-02-03 19:12:27 +08:00
|
|
|
if (ocl::haveOpenCL())
|
|
|
|
{
|
|
|
|
String vname = ocl::Device::getDefault().vendor();
|
|
|
|
if (vname == "Advanced Micro Devices, Inc." ||
|
|
|
|
vname == "AMD")
|
2014-02-12 21:42:09 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
localSize = Size(8, 8);
|
2014-02-12 21:42:09 +08:00
|
|
|
lbufSize = Size(origWinSize.width + localSize.width,
|
|
|
|
origWinSize.height + localSize.height);
|
|
|
|
if (lbufSize.area() > 1024)
|
|
|
|
lbufSize = Size(0, 0);
|
|
|
|
}
|
2014-02-03 19:12:27 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
Ptr<FeatureEvaluator> HaarEvaluator::clone() const
|
|
|
|
{
|
2013-08-13 21:45:29 +08:00
|
|
|
Ptr<HaarEvaluator> ret = makePtr<HaarEvaluator>();
|
2014-02-03 19:12:27 +08:00
|
|
|
*ret = *this;
|
2010-05-12 01:44:00 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
void HaarEvaluator::computeChannels(int scaleIdx, InputArray img)
|
|
|
|
{
|
|
|
|
const ScaleData& s = scaleData->at(scaleIdx);
|
|
|
|
tofs = (int)sbufSize.area();
|
|
|
|
sqofs = hasTiltedFeatures ? tofs*2 : tofs;
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
if (img.isUMat())
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
int sx = s.layer_ofs % sbufSize.width;
|
|
|
|
int sy = s.layer_ofs / sbufSize.width;
|
|
|
|
int sqy = sy + (sqofs / sbufSize.width);
|
|
|
|
UMat sum(usbuf, Rect(sx, sy, s.szi.width, s.szi.height));
|
|
|
|
UMat sqsum(usbuf, Rect(sx, sqy, s.szi.width, s.szi.height));
|
|
|
|
sqsum.flags = (sqsum.flags & ~UMat::DEPTH_MASK) | CV_32F;
|
|
|
|
|
|
|
|
if (hasTiltedFeatures)
|
2013-12-17 18:29:30 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
int sty = sy + (tofs / sbufSize.width);
|
|
|
|
UMat tilted(usbuf, Rect(sx, sty, s.szi.width, s.szi.height));
|
|
|
|
integral(img, sum, sqsum, tilted, CV_32S, CV_32F);
|
2013-12-17 18:29:30 +08:00
|
|
|
}
|
|
|
|
else
|
2013-12-18 00:55:49 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
UMatData* u = sqsum.u;
|
|
|
|
integral(img, sum, sqsum, noArray(), CV_32S, CV_32F);
|
|
|
|
CV_Assert(sqsum.u == u && sqsum.size() == s.szi && sqsum.type()==CV_32F);
|
2013-12-18 00:55:49 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2013-12-17 18:29:30 +08:00
|
|
|
else
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
Mat sum(s.szi, CV_32S, sbuf.ptr<int>() + s.layer_ofs, sbuf.step);
|
|
|
|
Mat sqsum(s.szi, CV_32F, sum.ptr<int>() + sqofs, sbuf.step);
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
if (hasTiltedFeatures)
|
2013-12-17 18:29:30 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
Mat tilted(s.szi, CV_32S, sum.ptr<int>() + tofs, sbuf.step);
|
|
|
|
integral(img, sum, sqsum, tilted, CV_32S, CV_32F);
|
2013-12-17 18:29:30 +08:00
|
|
|
}
|
|
|
|
else
|
2014-02-03 19:12:27 +08:00
|
|
|
integral(img, sum, sqsum, noArray(), CV_32S, CV_32F);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-02-03 19:12:27 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
void HaarEvaluator::computeOptFeatures()
|
|
|
|
{
|
|
|
|
int sstep = sbufSize.width;
|
|
|
|
CV_SUM_OFS( nofs[0], nofs[1], nofs[2], nofs[3], 0, normrect, sstep );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-12-13 01:58:42 +08:00
|
|
|
size_t fi, nfeatures = features->size();
|
|
|
|
const std::vector<Feature>& ff = *features;
|
2014-02-03 19:12:27 +08:00
|
|
|
optfeatures->resize(nfeatures);
|
|
|
|
optfeaturesPtr = &(*optfeatures)[0];
|
|
|
|
for( fi = 0; fi < nfeatures; fi++ )
|
|
|
|
optfeaturesPtr[fi].setOffsets( ff[fi], sstep, tofs );
|
|
|
|
optfeatures_lbuf->resize(nfeatures);
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
for( fi = 0; fi < nfeatures; fi++ )
|
|
|
|
optfeatures_lbuf->at(fi).setOffsets(ff[fi], lbufSize.width > 0 ? lbufSize.width : sstep, tofs);
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
copyVectorToUMat(*optfeatures_lbuf, ufbuf);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
bool HaarEvaluator::setWindow( Point pt, int scaleIdx )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
const ScaleData& s = getScaleData(scaleIdx);
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( pt.x < 0 || pt.y < 0 ||
|
2014-02-03 19:12:27 +08:00
|
|
|
pt.x + origWinSize.width >= s.szi.width ||
|
|
|
|
pt.y + origWinSize.height >= s.szi.height )
|
2010-05-12 01:44:00 +08:00
|
|
|
return false;
|
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
pwin = &sbuf.at<int>(pt) + s.layer_ofs;
|
|
|
|
const float* pq = (const float*)(pwin + sqofs);
|
|
|
|
int valsum = CALC_SUM_OFS(nofs, pwin);
|
|
|
|
float valsqsum = CALC_SUM_OFS(nofs, pq);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
double nf = (double)normrect.area() * valsqsum - (double)valsum * valsum;
|
|
|
|
if( nf > 0. )
|
2013-02-25 00:14:01 +08:00
|
|
|
nf = std::sqrt(nf);
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
|
|
|
nf = 1.;
|
2014-02-03 19:12:27 +08:00
|
|
|
varianceNormFactor = (float)(1./nf);
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
|
|
|
|
void HaarEvaluator::OptFeature::setOffsets( const Feature& _f, int step, int _tofs )
|
|
|
|
{
|
|
|
|
weight[0] = _f.rect[0].weight;
|
|
|
|
weight[1] = _f.rect[1].weight;
|
|
|
|
weight[2] = _f.rect[2].weight;
|
|
|
|
|
|
|
|
if( _f.tilted )
|
|
|
|
{
|
|
|
|
CV_TILTED_OFS( ofs[0][0], ofs[0][1], ofs[0][2], ofs[0][3], _tofs, _f.rect[0].r, step );
|
|
|
|
CV_TILTED_OFS( ofs[1][0], ofs[1][1], ofs[1][2], ofs[1][3], _tofs, _f.rect[1].r, step );
|
|
|
|
CV_TILTED_OFS( ofs[2][0], ofs[2][1], ofs[2][2], ofs[2][3], _tofs, _f.rect[2].r, step );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CV_SUM_OFS( ofs[0][0], ofs[0][1], ofs[0][2], ofs[0][3], 0, _f.rect[0].r, step );
|
|
|
|
CV_SUM_OFS( ofs[1][0], ofs[1][1], ofs[1][2], ofs[1][3], 0, _f.rect[1].r, step );
|
|
|
|
CV_SUM_OFS( ofs[2][0], ofs[2][1], ofs[2][2], ofs[2][3], 0, _f.rect[2].r, step );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:29:30 +08:00
|
|
|
Rect HaarEvaluator::getNormRect() const
|
|
|
|
{
|
|
|
|
return normrect;
|
|
|
|
}
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
int HaarEvaluator::getSquaresOffset() const
|
2013-12-17 18:29:30 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
return sqofs;
|
2013-12-17 18:29:30 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
//---------------------------------------------- LBPEvaluator -------------------------------------
|
|
|
|
bool LBPEvaluator::Feature :: read(const FileNode& node )
|
|
|
|
{
|
|
|
|
FileNode rnode = node[CC_RECT];
|
|
|
|
FileNodeIterator it = rnode.begin();
|
|
|
|
it >> rect.x >> rect.y >> rect.width >> rect.height;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LBPEvaluator::LBPEvaluator()
|
|
|
|
{
|
2013-08-13 21:45:29 +08:00
|
|
|
features = makePtr<std::vector<Feature> >();
|
2013-12-20 22:39:35 +08:00
|
|
|
optfeatures = makePtr<std::vector<OptFeature> >();
|
2014-02-03 19:12:27 +08:00
|
|
|
scaleData = makePtr<std::vector<ScaleData> >();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-02-03 19:12:27 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
LBPEvaluator::~LBPEvaluator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
bool LBPEvaluator::read( const FileNode& node, Size _origWinSize )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
if (!FeatureEvaluator::read(node, _origWinSize))
|
|
|
|
return false;
|
|
|
|
if(features.empty())
|
|
|
|
features = makePtr<std::vector<Feature> >();
|
|
|
|
if(optfeatures.empty())
|
|
|
|
optfeatures = makePtr<std::vector<OptFeature> >();
|
|
|
|
if (optfeatures_lbuf.empty())
|
|
|
|
optfeatures_lbuf = makePtr<std::vector<OptFeature> >();
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
features->resize(node.size());
|
2014-02-03 19:12:27 +08:00
|
|
|
optfeaturesPtr = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
FileNodeIterator it = node.begin(), it_end = node.end();
|
2013-12-20 22:39:35 +08:00
|
|
|
std::vector<Feature>& ff = *features;
|
2010-05-12 01:44:00 +08:00
|
|
|
for(int i = 0; it != it_end; ++it, i++)
|
|
|
|
{
|
2013-12-20 22:39:35 +08:00
|
|
|
if(!ff[i].read(*it))
|
2010-05-12 01:44:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-03 19:12:27 +08:00
|
|
|
nchannels = 1;
|
2014-02-12 21:42:09 +08:00
|
|
|
localSize = lbufSize = Size(0, 0);
|
2014-02-03 19:12:27 +08:00
|
|
|
if (ocl::haveOpenCL())
|
|
|
|
{
|
|
|
|
const ocl::Device& device = ocl::Device::getDefault();
|
|
|
|
String vname = device.vendor();
|
|
|
|
if ((vname == "Advanced Micro Devices, Inc." ||
|
|
|
|
vname == "AMD") && !device.hostUnifiedMemory())
|
|
|
|
localSize = Size(8, 8);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ptr<FeatureEvaluator> LBPEvaluator::clone() const
|
|
|
|
{
|
2013-08-13 21:45:29 +08:00
|
|
|
Ptr<LBPEvaluator> ret = makePtr<LBPEvaluator>();
|
2014-02-03 19:12:27 +08:00
|
|
|
*ret = *this;
|
2010-05-12 01:44:00 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
void LBPEvaluator::computeChannels(int scaleIdx, InputArray _img)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
const ScaleData& s = scaleData->at(scaleIdx);
|
2013-12-23 22:41:54 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
if (_img.isUMat())
|
2013-12-20 22:39:35 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
int sx = s.layer_ofs % sbufSize.width;
|
|
|
|
int sy = s.layer_ofs / sbufSize.width;
|
|
|
|
UMat sum(usbuf, Rect(sx, sy, s.szi.width, s.szi.height));
|
|
|
|
integral(_img, sum, noArray(), noArray(), CV_32S);
|
2013-12-20 22:39:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
Mat sum(s.szi, CV_32S, sbuf.ptr<int>() + s.layer_ofs, sbuf.step);
|
|
|
|
integral(_img, sum, noArray(), noArray(), CV_32S);
|
2013-12-20 22:39:35 +08:00
|
|
|
}
|
2013-12-23 19:28:50 +08:00
|
|
|
}
|
2011-09-22 02:03:53 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
void LBPEvaluator::computeOptFeatures()
|
2011-09-22 02:03:53 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
int sstep = sbufSize.width;
|
2011-09-22 02:03:53 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
size_t fi, nfeatures = features->size();
|
|
|
|
const std::vector<Feature>& ff = *features;
|
|
|
|
optfeatures->resize(nfeatures);
|
|
|
|
optfeaturesPtr = &(*optfeatures)[0];
|
|
|
|
for( fi = 0; fi < nfeatures; fi++ )
|
2014-02-03 20:36:36 +08:00
|
|
|
optfeaturesPtr[fi].setOffsets( ff[fi], sstep );
|
2014-02-03 19:12:27 +08:00
|
|
|
copyVectorToUMat(*optfeatures, ufbuf);
|
2011-09-22 02:03:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
void LBPEvaluator::OptFeature::setOffsets( const Feature& _f, int step )
|
2011-09-22 02:03:53 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
Rect tr = _f.rect;
|
|
|
|
int w0 = tr.width;
|
|
|
|
int h0 = tr.height;
|
|
|
|
|
|
|
|
CV_SUM_OFS( ofs[0], ofs[1], ofs[4], ofs[5], 0, tr, step );
|
|
|
|
tr.x += 2*w0;
|
|
|
|
CV_SUM_OFS( ofs[2], ofs[3], ofs[6], ofs[7], 0, tr, step );
|
|
|
|
tr.y += 2*h0;
|
|
|
|
CV_SUM_OFS( ofs[10], ofs[11], ofs[14], ofs[15], 0, tr, step );
|
|
|
|
tr.x -= 2*w0;
|
|
|
|
CV_SUM_OFS( ofs[8], ofs[9], ofs[12], ofs[13], 0, tr, step );
|
2011-09-22 02:03:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
bool LBPEvaluator::setWindow( Point pt, int scaleIdx )
|
2011-09-22 02:03:53 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
CV_Assert(0 <= scaleIdx && scaleIdx < (int)scaleData->size());
|
|
|
|
const ScaleData& s = scaleData->at(scaleIdx);
|
2011-09-22 02:03:53 +08:00
|
|
|
|
|
|
|
if( pt.x < 0 || pt.y < 0 ||
|
2014-02-03 19:12:27 +08:00
|
|
|
pt.x + origWinSize.width >= s.szi.width ||
|
|
|
|
pt.y + origWinSize.height >= s.szi.height )
|
2011-09-22 02:03:53 +08:00
|
|
|
return false;
|
2014-02-03 19:12:27 +08:00
|
|
|
|
|
|
|
pwin = &sbuf.at<int>(pt) + s.layer_ofs;
|
2011-09-22 02:03:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ptr<FeatureEvaluator> FeatureEvaluator::create( int featureType )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
return featureType == HAAR ? Ptr<FeatureEvaluator>(new HaarEvaluator) :
|
2012-06-09 23:00:04 +08:00
|
|
|
featureType == LBP ? Ptr<FeatureEvaluator>(new LBPEvaluator) :
|
2011-09-22 02:03:53 +08:00
|
|
|
Ptr<FeatureEvaluator>();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2011-09-22 02:03:53 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//---------------------------------------- Classifier Cascade --------------------------------------------
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
CascadeClassifierImpl::CascadeClassifierImpl()
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
CascadeClassifierImpl::~CascadeClassifierImpl()
|
2012-06-09 23:00:04 +08:00
|
|
|
{
|
2011-10-05 21:21:28 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
bool CascadeClassifierImpl::empty() const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-08-13 21:45:29 +08:00
|
|
|
return !oldCascade && data.stages.empty();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
bool CascadeClassifierImpl::load(const String& filename)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
oldCascade.release();
|
2011-07-07 19:43:21 +08:00
|
|
|
data = Data();
|
|
|
|
featureEvaluator.release();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
FileStorage fs(filename, FileStorage::READ);
|
|
|
|
if( !fs.isOpened() )
|
|
|
|
return false;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
if( read_(fs.getFirstTopLevelNode()) )
|
2010-05-12 01:44:00 +08:00
|
|
|
return true;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
fs.release();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-08-13 21:45:29 +08:00
|
|
|
oldCascade.reset((CvHaarClassifierCascade*)cvLoad(filename.c_str(), 0, 0, 0));
|
2010-05-12 01:44:00 +08:00
|
|
|
return !oldCascade.empty();
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
void CascadeClassifierImpl::read(const FileNode& node)
|
|
|
|
{
|
|
|
|
read_(node);
|
|
|
|
}
|
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
int CascadeClassifierImpl::runAt( Ptr<FeatureEvaluator>& evaluator, Point pt, int scaleIdx, double& weight )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
assert( !oldCascade &&
|
|
|
|
(data.featureType == FeatureEvaluator::HAAR ||
|
2011-09-22 02:03:53 +08:00
|
|
|
data.featureType == FeatureEvaluator::LBP ||
|
2014-02-03 19:12:27 +08:00
|
|
|
data.featureType == FeatureEvaluator::HOG) );
|
2010-12-09 23:09:34 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
if( !evaluator->setWindow(pt, scaleIdx) )
|
2011-09-22 02:03:53 +08:00
|
|
|
return -1;
|
2014-02-03 19:12:27 +08:00
|
|
|
if( data.maxNodesPerTree == 1 )
|
2011-09-22 02:03:53 +08:00
|
|
|
{
|
|
|
|
if( data.featureType == FeatureEvaluator::HAAR )
|
2012-06-09 23:00:04 +08:00
|
|
|
return predictOrderedStump<HaarEvaluator>( *this, evaluator, weight );
|
2011-09-22 02:03:53 +08:00
|
|
|
else if( data.featureType == FeatureEvaluator::LBP )
|
2012-06-09 23:00:04 +08:00
|
|
|
return predictCategoricalStump<LBPEvaluator>( *this, evaluator, weight );
|
2011-09-22 02:03:53 +08:00
|
|
|
else
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( data.featureType == FeatureEvaluator::HAAR )
|
2012-06-09 23:00:04 +08:00
|
|
|
return predictOrdered<HaarEvaluator>( *this, evaluator, weight );
|
2011-09-22 02:03:53 +08:00
|
|
|
else if( data.featureType == FeatureEvaluator::LBP )
|
2012-06-09 23:00:04 +08:00
|
|
|
return predictCategorical<LBPEvaluator>( *this, evaluator, weight );
|
2011-09-22 02:03:53 +08:00
|
|
|
else
|
|
|
|
return -2;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
void CascadeClassifierImpl::setMaskGenerator(const Ptr<MaskGenerator>& _maskGenerator)
|
2011-10-05 21:21:28 +08:00
|
|
|
{
|
|
|
|
maskGenerator=_maskGenerator;
|
|
|
|
}
|
2013-12-04 23:00:39 +08:00
|
|
|
Ptr<CascadeClassifierImpl::MaskGenerator> CascadeClassifierImpl::getMaskGenerator()
|
2011-10-05 21:21:28 +08:00
|
|
|
{
|
|
|
|
return maskGenerator;
|
|
|
|
}
|
|
|
|
|
2013-12-05 01:56:35 +08:00
|
|
|
Ptr<BaseCascadeClassifier::MaskGenerator> createFaceDetectionMaskGenerator()
|
2011-10-05 21:21:28 +08:00
|
|
|
{
|
|
|
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
2013-12-04 23:00:39 +08:00
|
|
|
return tegra::getCascadeClassifierMaskGenerator(*this);
|
2011-10-05 21:21:28 +08:00
|
|
|
#else
|
2013-12-05 01:56:35 +08:00
|
|
|
return Ptr<BaseCascadeClassifier::MaskGenerator>();
|
2011-10-05 21:21:28 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:18:33 +08:00
|
|
|
class CascadeClassifierInvoker : public ParallelLoopBody
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-09-25 16:18:33 +08:00
|
|
|
public:
|
2014-02-03 19:12:27 +08:00
|
|
|
CascadeClassifierInvoker( CascadeClassifierImpl& _cc, int _nscales, int _nstripes,
|
|
|
|
const FeatureEvaluator::ScaleData* _scaleData,
|
|
|
|
const int* _stripeSizes, std::vector<Rect>& _vec,
|
|
|
|
std::vector<int>& _levels, std::vector<double>& _weights,
|
|
|
|
bool outputLevels, const Mat& _mask, Mutex* _mtx)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-09 23:09:34 +08:00
|
|
|
classifier = &_cc;
|
2014-02-03 19:12:27 +08:00
|
|
|
nscales = _nscales;
|
|
|
|
nstripes = _nstripes;
|
|
|
|
scaleData = _scaleData;
|
|
|
|
stripeSizes = _stripeSizes;
|
2010-12-09 23:09:34 +08:00
|
|
|
rectangles = &_vec;
|
2012-09-25 16:18:33 +08:00
|
|
|
rejectLevels = outputLevels ? &_levels : 0;
|
|
|
|
levelWeights = outputLevels ? &_weights : 0;
|
|
|
|
mask = _mask;
|
|
|
|
mtx = _mtx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2012-09-25 16:18:33 +08:00
|
|
|
void operator()(const Range& range) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-14 18:17:45 +08:00
|
|
|
Ptr<FeatureEvaluator> evaluator = classifier->featureEvaluator->clone();
|
2014-02-03 19:12:27 +08:00
|
|
|
double gypWeight = 0.;
|
|
|
|
Size origWinSize = classifier->data.origWinSize;
|
2011-09-29 05:14:20 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
for( int scaleIdx = 0; scaleIdx < nscales; scaleIdx++ )
|
2010-12-09 23:09:34 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
const FeatureEvaluator::ScaleData& s = scaleData[scaleIdx];
|
|
|
|
float scalingFactor = s.scale;
|
|
|
|
int yStep = s.ystep;
|
|
|
|
int stripeSize = stripeSizes[scaleIdx];
|
|
|
|
int y0 = range.start*stripeSize;
|
|
|
|
Size szw = s.getWorkingSize(origWinSize);
|
|
|
|
int y1 = std::min(range.end*stripeSize, szw.height);
|
|
|
|
Size winSize(cvRound(origWinSize.width * scalingFactor),
|
|
|
|
cvRound(origWinSize.height * scalingFactor));
|
|
|
|
|
|
|
|
for( int y = y0; y < y1; y += yStep )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
for( int x = 0; x < szw.width; x += yStep )
|
2011-04-19 17:05:15 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
int result = classifier->runAt(evaluator, Point(x, y), scaleIdx, gypWeight);
|
|
|
|
if( rejectLevels )
|
|
|
|
{
|
|
|
|
if( result == 1 )
|
|
|
|
result = -(int)classifier->data.stages.size();
|
|
|
|
if( classifier->data.stages.size() + result == 0 )
|
|
|
|
{
|
|
|
|
mtx->lock();
|
|
|
|
rectangles->push_back(Rect(cvRound(x*scalingFactor),
|
|
|
|
cvRound(y*scalingFactor),
|
|
|
|
winSize.width, winSize.height));
|
|
|
|
rejectLevels->push_back(-result);
|
|
|
|
levelWeights->push_back(gypWeight);
|
|
|
|
mtx->unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( result > 0 )
|
2011-04-19 17:05:15 +08:00
|
|
|
{
|
2012-09-25 16:18:33 +08:00
|
|
|
mtx->lock();
|
2014-02-03 19:12:27 +08:00
|
|
|
rectangles->push_back(Rect(cvRound(x*scalingFactor),
|
|
|
|
cvRound(y*scalingFactor),
|
|
|
|
winSize.width, winSize.height));
|
2012-12-27 15:43:16 +08:00
|
|
|
mtx->unlock();
|
2011-04-19 17:05:15 +08:00
|
|
|
}
|
2014-02-03 19:12:27 +08:00
|
|
|
if( result == 0 )
|
|
|
|
x += yStep;
|
2012-06-09 23:00:04 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-12-09 23:09:34 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
CascadeClassifierImpl* classifier;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Rect>* rectangles;
|
2014-02-03 19:12:27 +08:00
|
|
|
int nscales, nstripes;
|
|
|
|
const FeatureEvaluator::ScaleData* scaleData;
|
|
|
|
const int* stripeSizes;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<int> *rejectLevels;
|
|
|
|
std::vector<double> *levelWeights;
|
2014-02-03 19:12:27 +08:00
|
|
|
std::vector<float> scales;
|
2011-10-05 21:21:28 +08:00
|
|
|
Mat mask;
|
2012-09-25 16:18:33 +08:00
|
|
|
Mutex* mtx;
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
struct getRect { Rect operator ()(const CvAvgComp& e) const { return e.rect; } };
|
2013-06-14 08:25:17 +08:00
|
|
|
struct getNeighbors { int operator ()(const CvAvgComp& e) const { return e.neighbors; } };
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-07-13 23:47:09 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
bool CascadeClassifierImpl::ocl_detectMultiScaleNoGrouping( const std::vector<float>& scales,
|
|
|
|
std::vector<Rect>& candidates )
|
2013-12-13 01:58:42 +08:00
|
|
|
{
|
2013-12-23 19:28:50 +08:00
|
|
|
int featureType = getFeatureType();
|
|
|
|
std::vector<UMat> bufs;
|
2014-02-03 19:12:27 +08:00
|
|
|
featureEvaluator->getUMats(bufs);
|
|
|
|
Size localsz = featureEvaluator->getLocalSize();
|
|
|
|
if( localsz.area() == 0 )
|
|
|
|
return false;
|
|
|
|
Size lbufSize = featureEvaluator->getLocalBufSize();
|
|
|
|
size_t localsize[] = { localsz.width, localsz.height };
|
|
|
|
const int grp_per_CU = 12;
|
|
|
|
size_t globalsize[] = { grp_per_CU*ocl::Device::getDefault().maxComputeUnits()*localsize[0], localsize[1] };
|
2013-12-23 19:28:50 +08:00
|
|
|
bool ok = false;
|
2013-12-23 22:41:54 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
ufacepos.create(1, MAX_FACES*3+1, CV_32S);
|
|
|
|
UMat ufacepos_count(ufacepos, Rect(0, 0, 1, 1));
|
|
|
|
ufacepos_count.setTo(Scalar::all(0));
|
|
|
|
|
2013-12-13 01:58:42 +08:00
|
|
|
if( ustages.empty() )
|
|
|
|
{
|
2013-12-19 20:31:30 +08:00
|
|
|
copyVectorToUMat(data.stages, ustages);
|
2014-02-03 19:12:27 +08:00
|
|
|
if (!data.stumps.empty())
|
|
|
|
copyVectorToUMat(data.stumps, unodes);
|
|
|
|
else
|
|
|
|
copyVectorToUMat(data.nodes, unodes);
|
|
|
|
copyVectorToUMat(data.leaves, uleaves);
|
2013-12-23 19:28:50 +08:00
|
|
|
if( !data.subsets.empty() )
|
|
|
|
copyVectorToUMat(data.subsets, usubsets);
|
2013-12-13 01:58:42 +08:00
|
|
|
}
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
int nstages = (int)data.stages.size();
|
|
|
|
|
2013-12-23 19:28:50 +08:00
|
|
|
if( featureType == FeatureEvaluator::HAAR )
|
|
|
|
{
|
|
|
|
Ptr<HaarEvaluator> haar = featureEvaluator.dynamicCast<HaarEvaluator>();
|
|
|
|
if( haar.empty() )
|
|
|
|
return false;
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2013-12-23 19:28:50 +08:00
|
|
|
if( haarKernel.empty() )
|
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
String opts;
|
|
|
|
if (lbufSize.area())
|
|
|
|
opts = format("-D LOCAL_SIZE_X=%d -D LOCAL_SIZE_Y=%d -D SUM_BUF_SIZE=%d -D SUM_BUF_STEP=%d -D NODE_COUNT=%d",
|
|
|
|
localsz.width, localsz.height, lbufSize.area(), lbufSize.width, data.maxNodesPerTree);
|
|
|
|
else
|
|
|
|
opts = format("-D LOCAL_SIZE_X=%d -D LOCAL_SIZE_Y=%d -D NODE_COUNT=%d",
|
|
|
|
localsz.width, localsz.height, data.maxNodesPerTree);
|
|
|
|
haarKernel.create("runHaarClassifier", ocl::objdetect::cascadedetect_oclsrc, opts);
|
2013-12-23 19:28:50 +08:00
|
|
|
if( haarKernel.empty() )
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-23 22:41:54 +08:00
|
|
|
|
2013-12-23 19:28:50 +08:00
|
|
|
Rect normrect = haar->getNormRect();
|
2014-02-03 19:12:27 +08:00
|
|
|
int sqofs = haar->getSquaresOffset();
|
|
|
|
int splitstage_ocl = 1;
|
2013-12-23 19:28:50 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
haarKernel.args((int)scales.size(),
|
|
|
|
ocl::KernelArg::PtrReadOnly(bufs[0]), // scaleData
|
|
|
|
ocl::KernelArg::ReadOnlyNoSize(bufs[1]), // sum
|
2013-12-23 19:28:50 +08:00
|
|
|
ocl::KernelArg::PtrReadOnly(bufs[2]), // optfeatures
|
|
|
|
|
|
|
|
// cascade classifier
|
2014-02-03 19:12:27 +08:00
|
|
|
splitstage_ocl, nstages,
|
2013-12-23 19:28:50 +08:00
|
|
|
ocl::KernelArg::PtrReadOnly(ustages),
|
2014-02-03 19:12:27 +08:00
|
|
|
ocl::KernelArg::PtrReadOnly(unodes),
|
|
|
|
ocl::KernelArg::PtrReadOnly(uleaves),
|
2013-12-23 19:28:50 +08:00
|
|
|
|
|
|
|
ocl::KernelArg::PtrWriteOnly(ufacepos), // positions
|
2014-02-03 19:12:27 +08:00
|
|
|
normrect, sqofs, data.origWinSize, (int)MAX_FACES);
|
|
|
|
ok = haarKernel.run(2, globalsize, localsize, true);
|
2013-12-23 19:28:50 +08:00
|
|
|
}
|
|
|
|
else if( featureType == FeatureEvaluator::LBP )
|
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
if (data.maxNodesPerTree > 1)
|
|
|
|
return false;
|
2014-02-03 20:36:36 +08:00
|
|
|
|
2013-12-23 19:28:50 +08:00
|
|
|
Ptr<LBPEvaluator> lbp = featureEvaluator.dynamicCast<LBPEvaluator>();
|
|
|
|
if( lbp.empty() )
|
|
|
|
return false;
|
2013-12-23 22:41:54 +08:00
|
|
|
|
2013-12-23 19:28:50 +08:00
|
|
|
if( lbpKernel.empty() )
|
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
String opts;
|
|
|
|
if (lbufSize.area())
|
|
|
|
opts = format("-D LOCAL_SIZE_X=%d -D LOCAL_SIZE_Y=%d -D SUM_BUF_SIZE=%d -D SUM_BUF_STEP=%d",
|
|
|
|
localsz.width, localsz.height, lbufSize.area(), lbufSize.width);
|
|
|
|
else
|
|
|
|
opts = format("-D LOCAL_SIZE_X=%d -D LOCAL_SIZE_Y=%d", localsz.width, localsz.height);
|
|
|
|
lbpKernel.create("runLBPClassifierStumpSimple", ocl::objdetect::cascadedetect_oclsrc, opts);
|
2013-12-23 19:28:50 +08:00
|
|
|
if( lbpKernel.empty() )
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-23 22:41:54 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
int splitstage_ocl = 1;
|
2013-12-23 19:28:50 +08:00
|
|
|
int subsetSize = (data.ncategories + 31)/32;
|
2014-02-03 19:12:27 +08:00
|
|
|
lbpKernel.args((int)scales.size(),
|
|
|
|
ocl::KernelArg::PtrReadOnly(bufs[0]), // scaleData
|
|
|
|
ocl::KernelArg::ReadOnlyNoSize(bufs[1]), // sum
|
|
|
|
ocl::KernelArg::PtrReadOnly(bufs[2]), // optfeatures
|
|
|
|
|
|
|
|
// cascade classifier
|
|
|
|
splitstage_ocl, nstages,
|
|
|
|
ocl::KernelArg::PtrReadOnly(ustages),
|
|
|
|
ocl::KernelArg::PtrReadOnly(unodes),
|
|
|
|
ocl::KernelArg::PtrReadOnly(usubsets),
|
|
|
|
subsetSize,
|
|
|
|
|
|
|
|
ocl::KernelArg::PtrWriteOnly(ufacepos), // positions
|
|
|
|
data.origWinSize, (int)MAX_FACES);
|
|
|
|
|
|
|
|
ok = lbpKernel.run(2, globalsize, localsize, true);
|
|
|
|
}
|
2013-12-23 22:41:54 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
if( ok )
|
|
|
|
{
|
|
|
|
Mat facepos = ufacepos.getMat(ACCESS_READ);
|
|
|
|
const int* fptr = facepos.ptr<int>();
|
|
|
|
int nfaces = fptr[0];
|
|
|
|
nfaces = std::min(nfaces, (int)MAX_FACES);
|
2013-12-23 22:41:54 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
for( int i = 0; i < nfaces; i++ )
|
|
|
|
{
|
|
|
|
const FeatureEvaluator::ScaleData& s = featureEvaluator->getScaleData(fptr[i*3 + 1]);
|
|
|
|
candidates.push_back(Rect(cvRound(fptr[i*3 + 2]*s.scale),
|
|
|
|
cvRound(fptr[i*3 + 3]*s.scale),
|
|
|
|
cvRound(data.origWinSize.width*s.scale),
|
|
|
|
cvRound(data.origWinSize.height*s.scale)));
|
|
|
|
}
|
2013-12-23 19:28:50 +08:00
|
|
|
}
|
2013-12-18 00:55:49 +08:00
|
|
|
return ok;
|
2013-12-13 01:58:42 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
bool CascadeClassifierImpl::isOldFormatCascade() const
|
2010-12-14 18:17:45 +08:00
|
|
|
{
|
|
|
|
return !oldCascade.empty();
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
int CascadeClassifierImpl::getFeatureType() const
|
2010-12-14 18:17:45 +08:00
|
|
|
{
|
|
|
|
return featureEvaluator->getFeatureType();
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
Size CascadeClassifierImpl::getOriginalWindowSize() const
|
2010-12-14 18:17:45 +08:00
|
|
|
{
|
|
|
|
return data.origWinSize;
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
void* CascadeClassifierImpl::getOldCascade()
|
|
|
|
{
|
|
|
|
return oldCascade;
|
|
|
|
}
|
|
|
|
|
2013-06-14 08:25:17 +08:00
|
|
|
static void detectMultiScaleOldFormat( const Mat& image, Ptr<CvHaarClassifierCascade> oldCascade,
|
|
|
|
std::vector<Rect>& objects,
|
|
|
|
std::vector<int>& rejectLevels,
|
|
|
|
std::vector<double>& levelWeights,
|
|
|
|
std::vector<CvAvgComp>& vecAvgComp,
|
|
|
|
double scaleFactor, int minNeighbors,
|
|
|
|
int flags, Size minObjectSize, Size maxObjectSize,
|
|
|
|
bool outputRejectLevels = false )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-06-14 08:25:17 +08:00
|
|
|
MemStorage storage(cvCreateMemStorage(0));
|
|
|
|
CvMat _image = image;
|
|
|
|
CvSeq* _objects = cvHaarDetectObjectsForROC( &_image, oldCascade, storage, rejectLevels, levelWeights, scaleFactor,
|
|
|
|
minNeighbors, flags, minObjectSize, maxObjectSize, outputRejectLevels );
|
|
|
|
Seq<CvAvgComp>(_objects).copyTo(vecAvgComp);
|
|
|
|
objects.resize(vecAvgComp.size());
|
|
|
|
std::transform(vecAvgComp.begin(), vecAvgComp.end(), objects.begin(), getRect());
|
|
|
|
}
|
2010-11-18 17:10:47 +08:00
|
|
|
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2013-12-13 01:58:42 +08:00
|
|
|
void CascadeClassifierImpl::detectMultiScaleNoGrouping( InputArray _image, std::vector<Rect>& candidates,
|
2013-06-14 08:25:17 +08:00
|
|
|
std::vector<int>& rejectLevels, std::vector<double>& levelWeights,
|
|
|
|
double scaleFactor, Size minObjectSize, Size maxObjectSize,
|
|
|
|
bool outputRejectLevels )
|
|
|
|
{
|
2013-12-13 01:58:42 +08:00
|
|
|
Size imgsz = _image.size();
|
2014-02-03 20:36:36 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
Mat grayImage;
|
|
|
|
_InputArray gray;
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2013-06-14 08:25:17 +08:00
|
|
|
candidates.clear();
|
2013-12-13 01:58:42 +08:00
|
|
|
rejectLevels.clear();
|
|
|
|
levelWeights.clear();
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2010-12-09 23:09:34 +08:00
|
|
|
if( maxObjectSize.height == 0 || maxObjectSize.width == 0 )
|
2013-12-13 01:58:42 +08:00
|
|
|
maxObjectSize = imgsz;
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
bool use_ocl = tryOpenCL && ocl::useOpenCL() &&
|
|
|
|
featureEvaluator->getLocalSize().area() > 0 &&
|
|
|
|
ocl::Device::getDefault().type() != ocl::Device::TYPE_CPU &&
|
|
|
|
(data.minNodesPerTree == data.maxNodesPerTree) &&
|
|
|
|
!isOldFormatCascade() &&
|
|
|
|
maskGenerator.empty() &&
|
|
|
|
!outputRejectLevels;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
/*if( use_ocl )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
if (_image.channels() > 1)
|
|
|
|
cvtColor(_image, ugrayImage, COLOR_BGR2GRAY);
|
|
|
|
else if (_image.isUMat())
|
|
|
|
ugrayImage = _image.getUMat();
|
2013-12-13 01:58:42 +08:00
|
|
|
else
|
2014-02-03 19:12:27 +08:00
|
|
|
_image.copyTo(ugrayImage);
|
|
|
|
gray = ugrayImage;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-02-03 19:12:27 +08:00
|
|
|
else*/
|
2013-12-18 00:55:49 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
if (_image.channels() > 1)
|
|
|
|
cvtColor(_image, grayImage, COLOR_BGR2GRAY);
|
|
|
|
else if (_image.isMat())
|
|
|
|
grayImage = _image.getMat();
|
|
|
|
else
|
|
|
|
_image.copyTo(grayImage);
|
|
|
|
gray = grayImage;
|
2013-12-18 00:55:49 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
std::vector<float> scales;
|
|
|
|
scales.reserve(1024);
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( double factor = 1; ; factor *= scaleFactor )
|
|
|
|
{
|
2010-12-14 18:17:45 +08:00
|
|
|
Size originalWindowSize = getOriginalWindowSize();
|
2010-12-09 23:09:34 +08:00
|
|
|
|
2010-12-14 18:17:45 +08:00
|
|
|
Size windowSize( cvRound(originalWindowSize.width*factor), cvRound(originalWindowSize.height*factor) );
|
2014-02-03 19:12:27 +08:00
|
|
|
if( windowSize.width > maxObjectSize.width || windowSize.height > maxObjectSize.height ||
|
|
|
|
windowSize.width > imgsz.width || windowSize.height > imgsz.height )
|
2010-11-18 17:10:47 +08:00
|
|
|
break;
|
2010-12-09 23:09:34 +08:00
|
|
|
if( windowSize.width < minObjectSize.width || windowSize.height < minObjectSize.height )
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
2014-02-03 19:12:27 +08:00
|
|
|
scales.push_back((float)factor);
|
|
|
|
}
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
if( !featureEvaluator->setImage(gray, scales) )
|
|
|
|
return;
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
// OpenCL code
|
|
|
|
if( use_ocl && ocl_detectMultiScaleNoGrouping( scales, candidates ))
|
|
|
|
return;
|
|
|
|
tryOpenCL = false;
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
// CPU code
|
|
|
|
featureEvaluator->getMats();
|
2013-12-17 18:29:30 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
Mat currentMask;
|
|
|
|
if (maskGenerator)
|
|
|
|
currentMask = maskGenerator->generateMask(gray.getMat());
|
|
|
|
|
|
|
|
size_t i, nscales = scales.size();
|
|
|
|
cv::AutoBuffer<int> stripeSizeBuf(nscales);
|
|
|
|
int* stripeSizes = stripeSizeBuf;
|
|
|
|
const FeatureEvaluator::ScaleData* s = &featureEvaluator->getScaleData(0);
|
|
|
|
Size szw = s->getWorkingSize(data.origWinSize);
|
|
|
|
int nstripes = cvCeil(szw.width/32.);
|
|
|
|
for( i = 0; i < nscales; i++ )
|
2013-12-17 18:29:30 +08:00
|
|
|
{
|
2014-02-03 19:12:27 +08:00
|
|
|
szw = s[i].getWorkingSize(data.origWinSize);
|
|
|
|
stripeSizes[i] = std::max((szw.height/s[i].ystep + nstripes-1)/nstripes, 1)*s[i].ystep;
|
2013-12-17 18:29:30 +08:00
|
|
|
}
|
2014-02-03 19:12:27 +08:00
|
|
|
|
|
|
|
CascadeClassifierInvoker invoker(*this, (int)nscales, nstripes, s, stripeSizes,
|
|
|
|
candidates, rejectLevels, levelWeights,
|
|
|
|
outputRejectLevels, currentMask, &mtx);
|
|
|
|
parallel_for_(Range(0, nstripes), invoker);
|
2013-12-17 18:29:30 +08:00
|
|
|
}
|
2013-06-14 08:25:17 +08:00
|
|
|
}
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
void CascadeClassifierImpl::detectMultiScale( InputArray _image, std::vector<Rect>& objects,
|
2013-06-14 08:25:17 +08:00
|
|
|
std::vector<int>& rejectLevels,
|
|
|
|
std::vector<double>& levelWeights,
|
|
|
|
double scaleFactor, int minNeighbors,
|
|
|
|
int flags, Size minObjectSize, Size maxObjectSize,
|
|
|
|
bool outputRejectLevels )
|
|
|
|
{
|
2013-12-13 01:58:42 +08:00
|
|
|
CV_Assert( scaleFactor > 1 && _image.depth() == CV_8U );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-06-14 08:25:17 +08:00
|
|
|
if( empty() )
|
|
|
|
return;
|
2010-12-09 23:09:34 +08:00
|
|
|
|
2013-06-14 08:25:17 +08:00
|
|
|
if( isOldFormatCascade() )
|
2011-04-22 18:03:05 +08:00
|
|
|
{
|
2013-12-13 01:58:42 +08:00
|
|
|
Mat image = _image.getMat();
|
2013-06-14 08:25:17 +08:00
|
|
|
std::vector<CvAvgComp> fakeVecAvgComp;
|
|
|
|
detectMultiScaleOldFormat( image, oldCascade, objects, rejectLevels, levelWeights, fakeVecAvgComp, scaleFactor,
|
|
|
|
minNeighbors, flags, minObjectSize, maxObjectSize, outputRejectLevels );
|
2011-04-22 18:03:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-13 01:58:42 +08:00
|
|
|
detectMultiScaleNoGrouping( _image, objects, rejectLevels, levelWeights, scaleFactor, minObjectSize, maxObjectSize,
|
2013-06-14 08:25:17 +08:00
|
|
|
outputRejectLevels );
|
|
|
|
const double GROUP_EPS = 0.2;
|
|
|
|
if( outputRejectLevels )
|
|
|
|
{
|
|
|
|
groupRectangles( objects, rejectLevels, levelWeights, minNeighbors, GROUP_EPS );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
groupRectangles( objects, minNeighbors, GROUP_EPS );
|
|
|
|
}
|
2011-04-22 18:03:05 +08:00
|
|
|
}
|
2011-04-19 20:31:35 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
void CascadeClassifierImpl::detectMultiScale( InputArray _image, std::vector<Rect>& objects,
|
2011-04-19 20:31:35 +08:00
|
|
|
double scaleFactor, int minNeighbors,
|
|
|
|
int flags, Size minObjectSize, Size maxObjectSize)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<int> fakeLevels;
|
|
|
|
std::vector<double> fakeWeights;
|
2014-02-03 19:12:27 +08:00
|
|
|
detectMultiScale( _image, objects, fakeLevels, fakeWeights, scaleFactor,
|
2013-06-14 08:25:17 +08:00
|
|
|
minNeighbors, flags, minObjectSize, maxObjectSize );
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
void CascadeClassifierImpl::detectMultiScale( InputArray _image, std::vector<Rect>& objects,
|
2013-06-14 08:25:17 +08:00
|
|
|
std::vector<int>& numDetections, double scaleFactor,
|
|
|
|
int minNeighbors, int flags, Size minObjectSize,
|
|
|
|
Size maxObjectSize )
|
|
|
|
{
|
2013-11-19 00:48:00 +08:00
|
|
|
Mat image = _image.getMat();
|
2013-06-14 08:25:17 +08:00
|
|
|
CV_Assert( scaleFactor > 1 && image.depth() == CV_8U );
|
|
|
|
|
|
|
|
if( empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<int> fakeLevels;
|
|
|
|
std::vector<double> fakeWeights;
|
|
|
|
if( isOldFormatCascade() )
|
|
|
|
{
|
|
|
|
std::vector<CvAvgComp> vecAvgComp;
|
|
|
|
detectMultiScaleOldFormat( image, oldCascade, objects, fakeLevels, fakeWeights, vecAvgComp, scaleFactor,
|
|
|
|
minNeighbors, flags, minObjectSize, maxObjectSize );
|
|
|
|
numDetections.resize(vecAvgComp.size());
|
|
|
|
std::transform(vecAvgComp.begin(), vecAvgComp.end(), numDetections.begin(), getNeighbors());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
detectMultiScaleNoGrouping( image, objects, fakeLevels, fakeWeights, scaleFactor, minObjectSize, maxObjectSize );
|
|
|
|
const double GROUP_EPS = 0.2;
|
|
|
|
groupRectangles( objects, numDetections, minNeighbors, GROUP_EPS );
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2013-12-19 18:09:44 +08:00
|
|
|
CascadeClassifierImpl::Data::Data()
|
|
|
|
{
|
|
|
|
stageType = featureType = ncategories = maxNodesPerTree = 0;
|
|
|
|
}
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
bool CascadeClassifierImpl::Data::read(const FileNode &root)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-05-19 20:41:48 +08:00
|
|
|
static const float THRESHOLD_EPS = 1e-5f;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// load stage params
|
2013-03-23 00:37:49 +08:00
|
|
|
String stageTypeStr = (String)root[CC_STAGE_TYPE];
|
2010-05-12 01:44:00 +08:00
|
|
|
if( stageTypeStr == CC_BOOST )
|
|
|
|
stageType = BOOST;
|
|
|
|
else
|
|
|
|
return false;
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String featureTypeStr = (String)root[CC_FEATURE_TYPE];
|
2010-05-12 01:44:00 +08:00
|
|
|
if( featureTypeStr == CC_HAAR )
|
|
|
|
featureType = FeatureEvaluator::HAAR;
|
|
|
|
else if( featureTypeStr == CC_LBP )
|
|
|
|
featureType = FeatureEvaluator::LBP;
|
2011-09-22 02:03:53 +08:00
|
|
|
else if( featureTypeStr == CC_HOG )
|
|
|
|
featureType = FeatureEvaluator::HOG;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
|
|
|
return false;
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
origWinSize.width = (int)root[CC_WIDTH];
|
|
|
|
origWinSize.height = (int)root[CC_HEIGHT];
|
|
|
|
CV_Assert( origWinSize.height > 0 && origWinSize.width > 0 );
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// load feature params
|
|
|
|
FileNode fn = root[CC_FEATURE_PARAMS];
|
|
|
|
if( fn.empty() )
|
|
|
|
return false;
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
ncategories = fn[CC_MAX_CAT_COUNT];
|
|
|
|
int subsetSize = (ncategories + 31)/32,
|
|
|
|
nodeStep = 3 + ( ncategories>0 ? subsetSize : 1 );
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// load stages
|
|
|
|
fn = root[CC_STAGES];
|
|
|
|
if( fn.empty() )
|
|
|
|
return false;
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
stages.reserve(fn.size());
|
|
|
|
classifiers.clear();
|
|
|
|
nodes.clear();
|
2013-12-19 18:09:44 +08:00
|
|
|
stumps.clear();
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
FileNodeIterator it = fn.begin(), it_end = fn.end();
|
2014-02-03 19:12:27 +08:00
|
|
|
minNodesPerTree = INT_MAX;
|
2013-12-19 18:09:44 +08:00
|
|
|
maxNodesPerTree = 0;
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( int si = 0; it != it_end; si++, ++it )
|
|
|
|
{
|
|
|
|
FileNode fns = *it;
|
|
|
|
Stage stage;
|
2011-05-19 20:41:48 +08:00
|
|
|
stage.threshold = (float)fns[CC_STAGE_THRESHOLD] - THRESHOLD_EPS;
|
2010-05-12 01:44:00 +08:00
|
|
|
fns = fns[CC_WEAK_CLASSIFIERS];
|
|
|
|
if(fns.empty())
|
|
|
|
return false;
|
|
|
|
stage.ntrees = (int)fns.size();
|
|
|
|
stage.first = (int)classifiers.size();
|
|
|
|
stages.push_back(stage);
|
|
|
|
classifiers.reserve(stages[si].first + stages[si].ntrees);
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
FileNodeIterator it1 = fns.begin(), it1_end = fns.end();
|
|
|
|
for( ; it1 != it1_end; ++it1 ) // weak trees
|
|
|
|
{
|
|
|
|
FileNode fnw = *it1;
|
|
|
|
FileNode internalNodes = fnw[CC_INTERNAL_NODES];
|
|
|
|
FileNode leafValues = fnw[CC_LEAF_VALUES];
|
|
|
|
if( internalNodes.empty() || leafValues.empty() )
|
|
|
|
return false;
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
DTree tree;
|
|
|
|
tree.nodeCount = (int)internalNodes.size()/nodeStep;
|
2014-02-03 19:12:27 +08:00
|
|
|
minNodesPerTree = std::min(minNodesPerTree, tree.nodeCount);
|
2013-12-19 18:09:44 +08:00
|
|
|
maxNodesPerTree = std::max(maxNodesPerTree, tree.nodeCount);
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
classifiers.push_back(tree);
|
2010-12-14 18:17:45 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
nodes.reserve(nodes.size() + tree.nodeCount);
|
|
|
|
leaves.reserve(leaves.size() + leafValues.size());
|
|
|
|
if( subsetSize > 0 )
|
|
|
|
subsets.reserve(subsets.size() + tree.nodeCount*subsetSize);
|
2010-12-14 18:17:45 +08:00
|
|
|
|
|
|
|
FileNodeIterator internalNodesIter = internalNodes.begin(), internalNodesEnd = internalNodes.end();
|
|
|
|
|
|
|
|
for( ; internalNodesIter != internalNodesEnd; ) // nodes
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
DTreeNode node;
|
2010-12-14 18:17:45 +08:00
|
|
|
node.left = (int)*internalNodesIter; ++internalNodesIter;
|
|
|
|
node.right = (int)*internalNodesIter; ++internalNodesIter;
|
|
|
|
node.featureIdx = (int)*internalNodesIter; ++internalNodesIter;
|
2010-05-12 01:44:00 +08:00
|
|
|
if( subsetSize > 0 )
|
|
|
|
{
|
2010-12-14 18:17:45 +08:00
|
|
|
for( int j = 0; j < subsetSize; j++, ++internalNodesIter )
|
|
|
|
subsets.push_back((int)*internalNodesIter);
|
2010-05-12 01:44:00 +08:00
|
|
|
node.threshold = 0.f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-14 18:17:45 +08:00
|
|
|
node.threshold = (float)*internalNodesIter; ++internalNodesIter;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
nodes.push_back(node);
|
|
|
|
}
|
2010-12-14 18:17:45 +08:00
|
|
|
|
|
|
|
internalNodesIter = leafValues.begin(), internalNodesEnd = leafValues.end();
|
|
|
|
|
|
|
|
for( ; internalNodesIter != internalNodesEnd; ++internalNodesIter ) // leaves
|
|
|
|
leaves.push_back((float)*internalNodesIter);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
if( maxNodesPerTree == 1 )
|
2013-12-19 18:09:44 +08:00
|
|
|
{
|
|
|
|
int nodeOfs = 0, leafOfs = 0;
|
|
|
|
size_t nstages = stages.size();
|
|
|
|
for( size_t stageIdx = 0; stageIdx < nstages; stageIdx++ )
|
|
|
|
{
|
|
|
|
const Stage& stage = stages[stageIdx];
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2013-12-19 18:09:44 +08:00
|
|
|
int ntrees = stage.ntrees;
|
|
|
|
for( int i = 0; i < ntrees; i++, nodeOfs++, leafOfs+= 2 )
|
|
|
|
{
|
|
|
|
const DTreeNode& node = nodes[nodeOfs];
|
|
|
|
stumps.push_back(Stump(node.featureIdx, node.threshold,
|
|
|
|
leaves[leafOfs], leaves[leafOfs+1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-12-14 18:17:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:31:30 +08:00
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
bool CascadeClassifierImpl::read_(const FileNode& root)
|
2010-12-14 18:17:45 +08:00
|
|
|
{
|
2013-12-13 01:58:42 +08:00
|
|
|
tryOpenCL = true;
|
2013-12-23 19:28:50 +08:00
|
|
|
haarKernel = ocl::Kernel();
|
|
|
|
lbpKernel = ocl::Kernel();
|
2013-12-13 01:58:42 +08:00
|
|
|
ustages.release();
|
2014-02-03 19:12:27 +08:00
|
|
|
unodes.release();
|
|
|
|
uleaves.release();
|
2010-12-14 18:17:45 +08:00
|
|
|
if( !data.read(root) )
|
|
|
|
return false;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// load features
|
2010-12-14 18:17:45 +08:00
|
|
|
featureEvaluator = FeatureEvaluator::create(data.featureType);
|
|
|
|
FileNode fn = root[CC_FEATURES];
|
2010-05-12 01:44:00 +08:00
|
|
|
if( fn.empty() )
|
|
|
|
return false;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-02-03 19:12:27 +08:00
|
|
|
return featureEvaluator->read(fn, data.origWinSize);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-08-13 21:45:29 +08:00
|
|
|
template<> void DefaultDeleter<CvHaarClassifierCascade>::operator ()(CvHaarClassifierCascade* obj) const
|
2012-06-09 23:00:04 +08:00
|
|
|
{ cvReleaseHaarClassifierCascade(&obj); }
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
|
|
|
|
BaseCascadeClassifier::~BaseCascadeClassifier()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CascadeClassifier::CascadeClassifier() {}
|
|
|
|
CascadeClassifier::CascadeClassifier(const String& filename)
|
|
|
|
{
|
|
|
|
load(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
CascadeClassifier::~CascadeClassifier()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CascadeClassifier::empty() const
|
|
|
|
{
|
|
|
|
return cc.empty() || cc->empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CascadeClassifier::load( const String& filename )
|
|
|
|
{
|
|
|
|
cc = makePtr<CascadeClassifierImpl>();
|
|
|
|
if(!cc->load(filename))
|
|
|
|
cc.release();
|
|
|
|
return !empty();
|
|
|
|
}
|
|
|
|
|
2013-12-05 01:56:35 +08:00
|
|
|
bool CascadeClassifier::read(const FileNode &root)
|
|
|
|
{
|
2014-02-08 21:42:09 +08:00
|
|
|
Ptr<CascadeClassifierImpl> ccimpl = makePtr<CascadeClassifierImpl>();
|
2013-12-05 01:56:35 +08:00
|
|
|
bool ok = ccimpl->read_(root);
|
|
|
|
if( ok )
|
|
|
|
cc = ccimpl.staticCast<BaseCascadeClassifier>();
|
|
|
|
else
|
|
|
|
cc.release();
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:00:39 +08:00
|
|
|
void CascadeClassifier::detectMultiScale( InputArray image,
|
|
|
|
CV_OUT std::vector<Rect>& objects,
|
|
|
|
double scaleFactor,
|
|
|
|
int minNeighbors, int flags,
|
|
|
|
Size minSize,
|
|
|
|
Size maxSize )
|
|
|
|
{
|
|
|
|
CV_Assert(!empty());
|
|
|
|
cc->detectMultiScale(image, objects, scaleFactor, minNeighbors, flags, minSize, maxSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CascadeClassifier::detectMultiScale( InputArray image,
|
|
|
|
CV_OUT std::vector<Rect>& objects,
|
|
|
|
CV_OUT std::vector<int>& numDetections,
|
|
|
|
double scaleFactor,
|
|
|
|
int minNeighbors, int flags,
|
|
|
|
Size minSize, Size maxSize )
|
|
|
|
{
|
|
|
|
CV_Assert(!empty());
|
|
|
|
cc->detectMultiScale(image, objects, numDetections,
|
|
|
|
scaleFactor, minNeighbors, flags, minSize, maxSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CascadeClassifier::detectMultiScale( InputArray image,
|
|
|
|
CV_OUT std::vector<Rect>& objects,
|
|
|
|
CV_OUT std::vector<int>& rejectLevels,
|
|
|
|
CV_OUT std::vector<double>& levelWeights,
|
|
|
|
double scaleFactor,
|
|
|
|
int minNeighbors, int flags,
|
|
|
|
Size minSize, Size maxSize,
|
|
|
|
bool outputRejectLevels )
|
|
|
|
{
|
|
|
|
CV_Assert(!empty());
|
|
|
|
cc->detectMultiScale(image, objects, rejectLevels, levelWeights,
|
|
|
|
scaleFactor, minNeighbors, flags,
|
|
|
|
minSize, maxSize, outputRejectLevels);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CascadeClassifier::isOldFormatCascade() const
|
|
|
|
{
|
|
|
|
CV_Assert(!empty());
|
|
|
|
return cc->isOldFormatCascade();
|
|
|
|
}
|
|
|
|
|
|
|
|
Size CascadeClassifier::getOriginalWindowSize() const
|
|
|
|
{
|
|
|
|
CV_Assert(!empty());
|
|
|
|
return cc->getOriginalWindowSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CascadeClassifier::getFeatureType() const
|
|
|
|
{
|
|
|
|
CV_Assert(!empty());
|
|
|
|
return cc->getFeatureType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void* CascadeClassifier::getOldCascade()
|
|
|
|
{
|
|
|
|
CV_Assert(!empty());
|
|
|
|
return cc->getOldCascade();
|
|
|
|
}
|
|
|
|
|
2013-12-05 01:56:35 +08:00
|
|
|
void CascadeClassifier::setMaskGenerator(const Ptr<BaseCascadeClassifier::MaskGenerator>& maskGenerator)
|
2013-12-04 23:00:39 +08:00
|
|
|
{
|
|
|
|
CV_Assert(!empty());
|
|
|
|
cc->setMaskGenerator(maskGenerator);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ptr<BaseCascadeClassifier::MaskGenerator> CascadeClassifier::getMaskGenerator()
|
|
|
|
{
|
|
|
|
CV_Assert(!empty());
|
|
|
|
return cc->getMaskGenerator();
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
} // namespace cv
|