void CascadeClassifier::detectMultiScale( const Mat& image, std::vector<Rect>& objects, std::vector<int>& rejectLevels, std::vector<double>& levelWeights, double scaleFactor, int minNeighbors, int flags, Size minObjectSize, Size maxObjectSize, bool outputRejectLevels ) { CV_Assert( scaleFactor > 1 && image.depth() == CV_8U ); if( empty() ) return; if( isOldFormatCascade() ) { std::vector<CvAvgComp> fakeVecAvgComp; detectMultiScaleOldFormat( image, oldCascade, objects, rejectLevels, levelWeights, fakeVecAvgComp, scaleFactor, minNeighbors, flags, minObjectSize, maxObjectSize, outputRejectLevels ); } else { detectMultiScaleNoGrouping( image, objects, rejectLevels, levelWeights, scaleFactor, minObjectSize, maxObjectSize, outputRejectLevels ); const double GROUP_EPS = 0.2; if( outputRejectLevels ) { groupRectangles( objects, rejectLevels, levelWeights, minNeighbors, GROUP_EPS ); } else { groupRectangles( objects, minNeighbors, GROUP_EPS ); } } }
void CascadeClassifier::detectMultiScale( const Mat& image, std::vector<Rect>& objects, std::vector<int>& numDetections, double scaleFactor, int minNeighbors, int flags, Size minObjectSize, Size maxObjectSize ) { 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 ); } }
void WaldBoost::detect(Ptr<CvFeatureEvaluator> eval, const Mat& img, const std::vector<float>& scales, std::vector<Rect>& bboxes, std::vector<double>& confidences) { bboxes.clear(); confidences.clear(); Mat resized_img; int step = 4; float h; for (size_t i = 0; i < scales.size(); ++i) { float scale = scales[i]; resize(img, resized_img, Size(), scale, scale, INTER_LINEAR_EXACT); eval->setImage(resized_img, 0, 0, feature_indices_); int n_rows = (int)(24 / scale); int n_cols = (int)(24 / scale); for (int r = 0; r + 24 < resized_img.rows; r += step) { for (int c = 0; c + 24 < resized_img.cols; c += step) { eval->setWindow(Point(c, r)); if (predict(eval, &h) == +1) { int row = (int)(r / scale); int col = (int)(c / scale); bboxes.push_back(Rect(col, row, n_cols, n_rows)); confidences.push_back(h); } } } } std::vector<int> levels(bboxes.size(), 0); groupRectangles(bboxes, levels, confidences, 3, 0.7); }
bool SurfFaceDetection::DetectSingleScale(const Mat &_grayImg, float _scale, float _stepFactor, Size _winSize, vector<Rect> &_faceList) { if(!CalculateSurfSumImg(_grayImg)) return false; int height = (int)(_scale * _winSize.height + 0.5); int width = (int)(_scale * _winSize.width + 0.5); Size actualSize(width,height); int step = cv::min((int)(actualSize.height * _stepFactor + 0.5), (int)(actualSize.width * _stepFactor + 0.5)); DetectSingleScale(actualSize, _scale, step, _faceList); groupRectangles(_faceList, 2); return true; }
/** * @brief Identify unique faces from two sets of faces * @param frontFaceVector [const std::vector<cv::Rect>&] The first set of faces * @param secondFaceVector [const std::vector<cv::Rect>&] The second set of faces * @return [std::vector<cv::Rect>] A vector containing the unique faces. * Each face is represented by a rectangle. */ std::vector<cv::Rect> FaceDetector::identifyUniqueFaces( const std::vector<cv::Rect> firstFaceVector, const std::vector<cv::Rect> secondFaceVector) { std::vector<cv::Rect> final_faces; final_faces = firstFaceVector; final_faces.insert( final_faces.end(), secondFaceVector.begin(), secondFaceVector.end() ); int size = final_faces.size(); for( unsigned int i = 0; i < size; i++ ) { final_faces.push_back( final_faces[i] ); } groupRectangles( final_faces, 1, 0.2 ); return final_faces; }
bool SurfFaceDetection::DetectMultiScale(const Mat &_grayImg, float _scaleFactor, float _stepFactor, Size _winSize, vector<Rect> &_faceList, bool _isScore, vector<double> *_scoreList) { if(!CalculateSurfSumImg(_grayImg)) return false; Size actualSize(_winSize); //Size imgSize = _grayImg.size(); float scale = 1.0; int step = cv::min((int)(actualSize.height * _stepFactor + 0.5), (int)(actualSize.width * _stepFactor + 0.5)); while( actualSize.width <= imgSize.width && actualSize.height <= imgSize.height ) { DetectSingleScale(actualSize, scale, step, _faceList,_scoreList); scale = scale * _scaleFactor; int height = (int)(scale * _winSize.height + 0.5); int width = (int)(scale * _winSize.width + 0.5); actualSize = Size(width, height); step = cv::min((int)(actualSize.height * _stepFactor + 0.5), (int)(actualSize.width * _stepFactor + 0.5)); } vector<int> weights(_faceList.size(),2); //groupRectangles(_faceList,1,0.2,&weights,_scoreList); groupRectangles(_faceList,weights, *_scoreList,1); if( srcScale != 1.0 ) { for(int idx=0; idx<_faceList.size(); idx++) { _faceList[idx].x = (int)(_faceList[idx].x * srcScale + 0.5); _faceList[idx].y = (int)(_faceList[idx].y * srcScale + 0.5); _faceList[idx].width = (int)(_faceList[idx].width * srcScale + 0.5); _faceList[idx].height = (int)(_faceList[idx].height * srcScale + 0.5); } } return true; }
void DetBody::runDetection(Mat image , int frameNumber) { Mat detImage; image.copyTo(detImage); Size Big = detImage.size(); // TMP resize(detImage, detImage, Size(640, 480)); // TMP Size Small = detImage.size(); // TMP double xFactor = (double)Big.width / (double)Small.width; // TMP double yFactor = (double)Big.height / (double)Small.height; // TMP this->detections.clear(); this->pos.clear(); this->FinalDetections.clear(); this->FinalTorsoDetections.clear(); this->FinalUpperBodyDetections.clear(); PersonDetection(frameNumber, mixtureBody, FinalDetections, FinalUpperBodyDetections, FinalTorsoDetections, detImage, TH); // Put detections in detection vectors for(unsigned int k = 0; k < FinalTorsoDetections.size(); k++) { this->pos.push_back(FinalTorsoDetections[k].detect); } for(unsigned int k = 0; k < FinalUpperBodyDetections.size(); k++) { this->pos.push_back(FinalUpperBodyDetections[k].detect); } for(unsigned int k = 0; k < FinalDetections.size(); k++) { this->pos.push_back(FinalDetections[k].detect); } groupRectangles(this->pos, 1, 1000); cout << " detections: " << this->pos.size() << endl; for(unsigned int n = 0; n < this->pos.size(); n++) { // Stretching bounding box this->pos[n].y = (this->pos[n].y * yFactor) - ( this->pos[n].height * yFactor * .2 ); this->pos[n].x = (this->pos[n].x * xFactor) - ( this->pos[n].width * xFactor * .9); this->pos[n].width = (this->pos[n].width * xFactor) * (1 + .9 + .9); this->pos[n].height = (this->pos[n].height * yFactor) * 4; // Secure an in image coutout if(this->pos[n].x < 0) { this->pos[n].width = this->pos[n].width + this->pos[n].x; this->pos[n].x = 0; } if(this->pos[n].x + this->pos[n].width > image.cols) { this->pos[n].width = image.cols - this->pos[n].x; } if(this->pos[n].y + this->pos[n].height > image.rows) { this->pos[n].height = image.rows - this->pos[n].y; } // Cutout this->detections.push_back(image(this->pos[n]).clone()); } }
//used for cascade detection algorithm for ROC-curve calculating void groupRectangles(std::vector<Rect>& rectList, std::vector<int>& rejectLevels, std::vector<double>& levelWeights, int groupThreshold, double eps) { groupRectangles(rectList, groupThreshold, eps, &rejectLevels, &levelWeights); }
void groupRectangles(std::vector<Rect>& rectList, std::vector<int>& weights, int groupThreshold, double eps) { groupRectangles(rectList, groupThreshold, eps, &weights, 0); }