From abbb73e8942cda04d29acd0004c66207ed9e19a6 Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Tue, 2 Oct 2018 20:39:23 +0300 Subject: [PATCH] Update HOGDescriptor --- .../objdetect/include/opencv2/objdetect.hpp | 35 +++-- modules/objdetect/src/hog.cpp | 124 ++---------------- modules/objdetect/test/test_cascadeandhog.cpp | 21 +-- 3 files changed, 41 insertions(+), 139 deletions(-) diff --git a/modules/objdetect/include/opencv2/objdetect.hpp b/modules/objdetect/include/opencv2/objdetect.hpp index 59af3f919e..3ccdfe60a3 100644 --- a/modules/objdetect/include/opencv2/objdetect.hpp +++ b/modules/objdetect/include/opencv2/objdetect.hpp @@ -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& foundLocations, + CV_WRAP virtual void detect(InputArray img, CV_OUT std::vector& foundLocations, CV_OUT std::vector& 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& foundLocations, + virtual void detect(InputArray img, CV_OUT std::vector& foundLocations, double hitThreshold = 0, Size winStride = Size(), Size padding = Size(), const std::vector& searchLocations=std::vector()) 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 &locations, + virtual void detectROI(InputArray img, const std::vector &locations, CV_OUT std::vector& foundLocations, CV_OUT std::vector& 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& foundLocations, std::vector& 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. diff --git a/modules/objdetect/src/hog.cpp b/modules/objdetect/src/hog.cpp index 40839dfb3a..348a66302d 100644 --- a/modules/objdetect/src/hog.cpp +++ b/modules/objdetect/src/hog.cpp @@ -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); // - qangle.create(gradsize, CV_8UC2); // [0..nbins-1] - quantized gradient orientation + _grad.create(gradsize, CV_32FC2); // + _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& descriptors, } } -void HOGDescriptor::detect(const Mat& img, +void HOGDescriptor::detect(InputArray _img, std::vector& hits, std::vector& weights, double hitThreshold, Size winStride, Size padding, const std::vector& 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& hits, double hitThreshold, +void HOGDescriptor::detect(InputArray img, std::vector& hits, double hitThreshold, Size winStride, Size padding, const std::vector& locations) const { CV_INSTRUMENT_REGION(); @@ -3544,12 +3548,13 @@ public: Mutex* mtx; }; -void HOGDescriptor::detectROI(const cv::Mat& img, const std::vector &locations, +void HOGDescriptor::detectROI(InputArray _img, const std::vector &locations, CV_OUT std::vector& foundLocations, CV_OUT std::vector& 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 & } } -void HOGDescriptor::detectMultiScaleROI(const cv::Mat& img, +void HOGDescriptor::detectMultiScaleROI(InputArray _img, CV_OUT std::vector& foundLocations, std::vector& locations, double hitThreshold, int groupThreshold) const { CV_INSTRUMENT_REGION(); + Mat img = _img.getMat(); std::vector 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 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(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& rectList, std::vector& weights, int groupThreshold, double eps) const { CV_INSTRUMENT_REGION(); diff --git a/modules/objdetect/test/test_cascadeandhog.cpp b/modules/objdetect/test/test_cascadeandhog.cpp index 106f3341b2..f6a98cbcf8 100644 --- a/modules/objdetect/test/test_cascadeandhog.cpp +++ b/modules/objdetect/test/test_cascadeandhog.cpp @@ -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& hits, vector& weights, double hitThreshold = 0.0, Size winStride = Size(), Size padding = Size(), const vector& locations = vector()) const; - virtual void detect(const Mat& img, vector& hits, double hitThreshold = 0.0, + virtual void detect(InputArray img, vector& hits, double hitThreshold = 0.0, Size winStride = Size(), Size padding = Size(), const vector& locations = vector()) 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& hits, vector& weights, double hitThreshold, Size winStride, Size padding, const vector& 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& hits, double hitThreshold, +void HOGDescriptorTester::detect(InputArray img, vector& hits, double hitThreshold, Size winStride, Size padding, const vector& locations) const { vector weightsV; @@ -1166,15 +1167,19 @@ void HOGDescriptorTester::compute(InputArray _img, vector& 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); // - qangle.create(gradsize, CV_8UC2); // [0..nbins-1] - quantized gradient orientation + _grad.create(gradsize, CV_32FC2); // + _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);