Merge pull request #12450 from sturkmen72:update_HOGDescriptor

This commit is contained in:
Alexander Alekhin 2018-10-11 19:23:35 +00:00
commit 0f3fe957f5
3 changed files with 41 additions and 139 deletions

View File

@ -380,7 +380,7 @@ public:
/**@brief Creates the HOG descriptor and detector with default params.
aqual to HOGDescriptor(Size(64,128), Size(16,16), Size(8,8), Size(8,8), 9, 1 )
aqual to HOGDescriptor(Size(64,128), Size(16,16), Size(8,8), Size(8,8), 9 )
*/
CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
@ -414,7 +414,7 @@ public:
{}
/** @overload
@param filename the file name containing HOGDescriptor properties and coefficients of the trained classifier
@param filename The file name containing HOGDescriptor properties and coefficients for the linear SVM classifier.
*/
CV_WRAP HOGDescriptor(const String& filename)
{
@ -448,28 +448,28 @@ public:
/**@example samples/cpp/peopledetect.cpp
*/
/**@brief Sets coefficients for the linear SVM classifier.
@param _svmdetector coefficients for the linear SVM classifier.
@param svmdetector coefficients for the linear SVM classifier.
*/
CV_WRAP virtual void setSVMDetector(InputArray _svmdetector);
CV_WRAP virtual void setSVMDetector(InputArray svmdetector);
/** @brief Reads HOGDescriptor parameters from a file node.
/** @brief Reads HOGDescriptor parameters from a cv::FileNode.
@param fn File node
*/
virtual bool read(FileNode& fn);
/** @brief Stores HOGDescriptor parameters in a file storage.
/** @brief Stores HOGDescriptor parameters in a cv::FileStorage.
@param fs File storage
@param objname Object name
*/
virtual void write(FileStorage& fs, const String& objname) const;
/** @brief loads coefficients for the linear SVM classifier from a file
@param filename Name of the file to read.
/** @brief loads HOGDescriptor parameters and coefficients for the linear SVM classifier from a file.
@param filename Path of the file to read.
@param objname The optional name of the node to read (if empty, the first top-level node will be used).
*/
CV_WRAP virtual bool load(const String& filename, const String& objname = String());
/** @brief saves coefficients for the linear SVM classifier to a file
/** @brief saves HOGDescriptor parameters and coefficients for the linear SVM classifier to a file
@param filename File name
@param objname Object name
*/
@ -505,7 +505,7 @@ public:
@param padding Padding
@param searchLocations Vector of Point includes set of requested locations to be evaluated.
*/
CV_WRAP virtual void detect(const Mat& img, CV_OUT std::vector<Point>& foundLocations,
CV_WRAP virtual void detect(InputArray img, CV_OUT std::vector<Point>& foundLocations,
CV_OUT std::vector<double>& weights,
double hitThreshold = 0, Size winStride = Size(),
Size padding = Size(),
@ -521,7 +521,7 @@ public:
@param padding Padding
@param searchLocations Vector of Point includes locations to search.
*/
virtual void detect(const Mat& img, CV_OUT std::vector<Point>& foundLocations,
virtual void detect(InputArray img, CV_OUT std::vector<Point>& foundLocations,
double hitThreshold = 0, Size winStride = Size(),
Size padding = Size(),
const std::vector<Point>& searchLocations=std::vector<Point>()) const;
@ -570,7 +570,7 @@ public:
@param paddingTL Padding from top-left
@param paddingBR Padding from bottom-right
*/
CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,
CV_WRAP virtual void computeGradient(InputArray img, InputOutputArray grad, InputOutputArray angleOfs,
Size paddingTL = Size(), Size paddingBR = Size()) const;
/** @brief Returns coefficients of the classifier trained for people detection (for 64x128 windows).
@ -639,7 +639,7 @@ public:
@param winStride winStride
@param padding padding
*/
virtual void detectROI(const cv::Mat& img, const std::vector<cv::Point> &locations,
virtual void detectROI(InputArray img, const std::vector<cv::Point> &locations,
CV_OUT std::vector<cv::Point>& foundLocations, CV_OUT std::vector<double>& confidences,
double hitThreshold = 0, cv::Size winStride = Size(),
cv::Size padding = Size()) const;
@ -652,17 +652,12 @@ public:
in the detector coefficients (as the last free coefficient). But if the free coefficient is omitted (which is allowed), you can specify it manually here.
@param groupThreshold Minimum possible number of rectangles minus 1. The threshold is used in a group of rectangles to retain it.
*/
virtual void detectMultiScaleROI(const cv::Mat& img,
virtual void detectMultiScaleROI(InputArray img,
CV_OUT std::vector<cv::Rect>& foundLocations,
std::vector<DetectionROI>& locations,
double hitThreshold = 0,
int groupThreshold = 0) const;
/** @brief read/parse Dalal's alt model file
@param modelfile Path of Dalal's alt model file.
*/
void readALTModel(String modelfile);
/** @brief Groups the object candidate rectangles.
@param rectList Input/output vector of rectangles. Output vector includes retained and grouped rectangles. (The Python list is not modified in place.)
@param weights Input/output vector of weights of rectangles. Output vector includes weights of retained and grouped rectangles. (The Python list is not modified in place.)
@ -688,7 +683,7 @@ protected:
};
/** @brief Detect QR code in image and return minimum area of quadrangle that describes QR code.
@param in Matrix of the type CV_8UC1 containing an image where QR code are detected.
@param in Matrix of the type CV_8U containing an image where QR code are detected.
@param points Output vector of vertices of a quadrangle of minimal area that describes QR code.
@param eps_x Epsilon neighborhood, which allows you to determine the horizontal pattern of the scheme 1:1:3:1:1 according to QR code standard.
@param eps_y Epsilon neighborhood, which allows you to determine the vertical pattern of the scheme 1:1:3:1:1 according to QR code standard.

View File

@ -234,17 +234,20 @@ inline float32x4_t vsetq_f32(float f0, float f1, float f2, float f3)
return a;
}
#endif
void HOGDescriptor::computeGradient(const Mat& img, Mat& grad, Mat& qangle,
void HOGDescriptor::computeGradient(InputArray _img, InputOutputArray _grad, InputOutputArray _qangle,
Size paddingTL, Size paddingBR) const
{
CV_INSTRUMENT_REGION();
Mat img = _img.getMat();
CV_Assert( img.type() == CV_8U || img.type() == CV_8UC3 );
Size gradsize(img.cols + paddingTL.width + paddingBR.width,
img.rows + paddingTL.height + paddingBR.height);
grad.create(gradsize, CV_32FC2); // <magnitude*(1-alpha), magnitude*alpha>
qangle.create(gradsize, CV_8UC2); // [0..nbins-1] - quantized gradient orientation
_grad.create(gradsize, CV_32FC2); // <magnitude*(1-alpha), magnitude*alpha>
_qangle.create(gradsize, CV_8UC2); // [0..nbins-1] - quantized gradient orientation
Mat grad = _grad.getMat();
Mat qangle = _qangle.getMat();
Size wholeSize;
Point roiofs;
@ -1650,12 +1653,13 @@ void HOGDescriptor::compute(InputArray _img, std::vector<float>& descriptors,
}
}
void HOGDescriptor::detect(const Mat& img,
void HOGDescriptor::detect(InputArray _img,
std::vector<Point>& hits, std::vector<double>& weights, double hitThreshold,
Size winStride, Size padding, const std::vector<Point>& locations) const
{
CV_INSTRUMENT_REGION();
Mat img = _img.getMat();
hits.clear();
weights.clear();
if( svmDetector.empty() )
@ -1764,7 +1768,7 @@ void HOGDescriptor::detect(const Mat& img,
}
}
void HOGDescriptor::detect(const Mat& img, std::vector<Point>& hits, double hitThreshold,
void HOGDescriptor::detect(InputArray img, std::vector<Point>& hits, double hitThreshold,
Size winStride, Size padding, const std::vector<Point>& locations) const
{
CV_INSTRUMENT_REGION();
@ -3544,12 +3548,13 @@ public:
Mutex* mtx;
};
void HOGDescriptor::detectROI(const cv::Mat& img, const std::vector<cv::Point> &locations,
void HOGDescriptor::detectROI(InputArray _img, const std::vector<cv::Point> &locations,
CV_OUT std::vector<cv::Point>& foundLocations, CV_OUT std::vector<double>& confidences,
double hitThreshold, cv::Size winStride, cv::Size padding) const
{
CV_INSTRUMENT_REGION();
Mat img = _img.getMat();
foundLocations.clear();
confidences.clear();
@ -3656,12 +3661,13 @@ void HOGDescriptor::detectROI(const cv::Mat& img, const std::vector<cv::Point> &
}
}
void HOGDescriptor::detectMultiScaleROI(const cv::Mat& img,
void HOGDescriptor::detectMultiScaleROI(InputArray _img,
CV_OUT std::vector<cv::Rect>& foundLocations, std::vector<DetectionROI>& locations,
double hitThreshold, int groupThreshold) const
{
CV_INSTRUMENT_REGION();
Mat img = _img.getMat();
std::vector<Rect> allCandidates;
Mutex mtx;
@ -3674,110 +3680,6 @@ void HOGDescriptor::detectMultiScaleROI(const cv::Mat& img,
cv::groupRectangles(foundLocations, groupThreshold, 0.2);
}
void HOGDescriptor::readALTModel(String modelfile)
{
// read model from SVMlight format..
FILE *modelfl;
if ((modelfl = fopen(modelfile.c_str(), "rb")) == NULL)
{
String eerr("file not exist");
String efile(__FILE__);
String efunc(__FUNCTION__);
CV_THROW (Exception(Error::StsError, eerr, efile, efunc, __LINE__));
}
char version_buffer[10];
if (!fread (&version_buffer,sizeof(char),10,modelfl))
{
String eerr("version?");
String efile(__FILE__);
String efunc(__FUNCTION__);
fclose(modelfl);
CV_THROW (Exception(Error::StsError, eerr, efile, efunc, __LINE__));
}
if(strcmp(version_buffer,"V6.01")) {
String eerr("version does not match");
String efile(__FILE__);
String efunc(__FUNCTION__);
fclose(modelfl);
CV_THROW (Exception(Error::StsError, eerr, efile, efunc, __LINE__));
}
/* read version number */
int version = 0;
if (!fread (&version,sizeof(int),1,modelfl))
{
fclose(modelfl);
CV_THROW (Exception());
}
if (version < 200)
{
String eerr("version does not match");
String efile(__FILE__);
String efunc(__FUNCTION__);
fclose(modelfl);
CV_THROW (Exception());
}
int kernel_type;
size_t nread;
nread=fread(&(kernel_type),sizeof(int),1,modelfl);
{// ignore these
int poly_degree;
nread=fread(&(poly_degree),sizeof(int),1,modelfl);
double rbf_gamma;
nread=fread(&(rbf_gamma),sizeof(double), 1, modelfl);
double coef_lin;
nread=fread(&(coef_lin),sizeof(double),1,modelfl);
double coef_const;
nread=fread(&(coef_const),sizeof(double),1,modelfl);
int l;
nread=fread(&l,sizeof(int),1,modelfl);
CV_Assert(l >= 0 && l < 0xFFFF);
char* custom = new char[l];
nread=fread(custom,sizeof(char),l,modelfl);
delete[] custom;
}
int totwords;
nread=fread(&(totwords),sizeof(int),1,modelfl);
{// ignore these
int totdoc;
nread=fread(&(totdoc),sizeof(int),1,modelfl);
int sv_num;
nread=fread(&(sv_num), sizeof(int),1,modelfl);
}
double linearbias;
nread=fread(&linearbias, sizeof(double), 1, modelfl);
std::vector<float> detector;
detector.clear();
if(kernel_type == 0) { /* linear kernel */
/* save linear wts also */
CV_Assert(totwords + 1 > 0 && totwords < 0xFFFF);
double *linearwt = new double[totwords+1];
int length = totwords;
nread = fread(linearwt, sizeof(double), totwords + 1, modelfl);
if(nread != static_cast<size_t>(length) + 1) {
delete [] linearwt;
fclose(modelfl);
CV_THROW (Exception());
}
for(int i = 0; i < length; i++)
detector.push_back((float)linearwt[i]);
detector.push_back((float)-linearbias);
setSVMDetector(detector);
delete [] linearwt;
} else {
fclose(modelfl);
CV_THROW (Exception());
}
fclose(modelfl);
}
void HOGDescriptor::groupRectangles(std::vector<cv::Rect>& rectList, std::vector<double>& weights, int groupThreshold, double eps) const
{
CV_INSTRUMENT_REGION();

View File

@ -553,15 +553,15 @@ public:
ts(cvtest::TS::ptr()), failed(false)
{ }
virtual void computeGradient(const Mat& img, Mat& grad, Mat& qangle,
virtual void computeGradient(InputArray img, InputOutputArray grad, InputOutputArray qangle,
Size paddingTL, Size paddingBR) const;
virtual void detect(const Mat& img,
virtual void detect(InputArray img,
vector<Point>& hits, vector<double>& weights, double hitThreshold = 0.0,
Size winStride = Size(), Size padding = Size(),
const vector<Point>& locations = vector<Point>()) const;
virtual void detect(const Mat& img, vector<Point>& hits, double hitThreshold = 0.0,
virtual void detect(InputArray img, vector<Point>& hits, double hitThreshold = 0.0,
Size winStride = Size(), Size padding = Size(),
const vector<Point>& locations = vector<Point>()) const;
@ -985,7 +985,7 @@ inline bool HOGDescriptorTester::is_failed() const
static inline int gcd(int a, int b) { return (a % b == 0) ? b : gcd (b, a % b); }
void HOGDescriptorTester::detect(const Mat& img,
void HOGDescriptorTester::detect(InputArray _img,
vector<Point>& hits, vector<double>& weights, double hitThreshold,
Size winStride, Size padding, const vector<Point>& locations) const
{
@ -996,6 +996,7 @@ void HOGDescriptorTester::detect(const Mat& img,
if( svmDetector.empty() )
return;
Mat img = _img.getMat();
if( winStride == Size() )
winStride = cellSize;
Size cacheStride(gcd(winStride.width, blockStride.width),
@ -1085,7 +1086,7 @@ void HOGDescriptorTester::detect(const Mat& img,
}
}
void HOGDescriptorTester::detect(const Mat& img, vector<Point>& hits, double hitThreshold,
void HOGDescriptorTester::detect(InputArray img, vector<Point>& hits, double hitThreshold,
Size winStride, Size padding, const vector<Point>& locations) const
{
vector<double> weightsV;
@ -1166,15 +1167,19 @@ void HOGDescriptorTester::compute(InputArray _img, vector<float>& descriptors,
}
}
void HOGDescriptorTester::computeGradient(const Mat& img, Mat& grad, Mat& qangle,
void HOGDescriptorTester::computeGradient(InputArray _img, InputOutputArray _grad, InputOutputArray _qangle,
Size paddingTL, Size paddingBR) const
{
Mat img = _img.getMat();
CV_Assert( img.type() == CV_8U || img.type() == CV_8UC3 );
Size gradsize(img.cols + paddingTL.width + paddingBR.width,
img.rows + paddingTL.height + paddingBR.height);
grad.create(gradsize, CV_32FC2); // <magnitude*(1-alpha), magnitude*alpha>
qangle.create(gradsize, CV_8UC2); // [0..nbins-1] - quantized gradient orientation
_grad.create(gradsize, CV_32FC2); // <magnitude*(1-alpha), magnitude*alpha>
_qangle.create(gradsize, CV_8UC2); // [0..nbins-1] - quantized gradient orientation
Mat grad = _grad.getMat();
Mat qangle = _qangle.getMat();
Size wholeSize;
Point roiofs;
img.locateROI(wholeSize, roiofs);