diff --git a/modules/objdetect/src/cascadedetect.cpp b/modules/objdetect/src/cascadedetect.cpp index 86a36bf8f3..20800ae525 100644 --- a/modules/objdetect/src/cascadedetect.cpp +++ b/modules/objdetect/src/cascadedetect.cpp @@ -1587,9 +1587,8 @@ bool CascadeClassifier::read(const FileNode &root) return ok; } -static void clipObjects(Size sz, std::vector& objects, - std::vector* a, - std::vector* b) +void clipObjects(Size sz, std::vector& objects, + std::vector* a, std::vector* b) { size_t i, j = 0, n = objects.size(); Rect win0 = Rect(0, 0, sz.width, sz.height); diff --git a/modules/objdetect/src/cascadedetect.hpp b/modules/objdetect/src/cascadedetect.hpp index 4cbf3e9bf0..696ab40d05 100644 --- a/modules/objdetect/src/cascadedetect.hpp +++ b/modules/objdetect/src/cascadedetect.hpp @@ -5,6 +5,9 @@ namespace cv { +void clipObjects(Size sz, std::vector& objects, + std::vector* a, std::vector* b); + class FeatureEvaluator { public: diff --git a/modules/objdetect/src/hog.cpp b/modules/objdetect/src/hog.cpp index 4697a01057..710c285174 100644 --- a/modules/objdetect/src/hog.cpp +++ b/modules/objdetect/src/hog.cpp @@ -41,6 +41,7 @@ //M*/ #include "precomp.hpp" +#include "cascadedetect.hpp" #include "opencv2/core/core_c.h" #include "opencl_kernels_objdetect.hpp" @@ -1822,7 +1823,9 @@ static bool ocl_detectMultiScale(InputArray _img, std::vector &found_locat all_candidates.push_back(Rect(Point2d(locations[j]) * scale, scaled_win_size)); } found_locations.assign(all_candidates.begin(), all_candidates.end()); - cv::groupRectangles(found_locations, (int)group_threshold, 0.2); + groupRectangles(found_locations, (int)group_threshold, 0.2); + clipObjects(imgSize, found_locations, 0, 0); + return true; } #endif //HAVE_OPENCL @@ -1878,6 +1881,7 @@ void HOGDescriptor::detectMultiScale( groupRectangles_meanshift(foundLocations, foundWeights, foundScales, finalThreshold, winSize); else groupRectangles(foundLocations, foundWeights, (int)finalThreshold, 0.2); + clipObjects(imgSize, foundLocations, 0, &foundWeights); } void HOGDescriptor::detectMultiScale(InputArray img, std::vector& foundLocations, diff --git a/modules/objdetect/test/test_cascadeandhog.cpp b/modules/objdetect/test/test_cascadeandhog.cpp index df33ffe933..31afbe6dac 100644 --- a/modules/objdetect/test/test_cascadeandhog.cpp +++ b/modules/objdetect/test/test_cascadeandhog.cpp @@ -1360,4 +1360,32 @@ TEST(Objdetect_HOGDetector_Strict, accuracy) std::vector descriptors; reference_hog.compute(image, descriptors); } - } +} + +TEST(Objdetect_CascadeDetector, small_img) +{ + String root = cvtest::TS::ptr()->get_data_path() + "cascadeandhog/cascades/"; + String cascades[] = + { + root + "haarcascade_frontalface_alt.xml", + root + "lbpcascade_frontalface.xml", + String() + }; + + vector objects; + RNG rng((uint64)-1); + + for( int i = 0; !cascades[i].empty(); i++ ) + { + printf("%d. %s\n", i, cascades[i].c_str()); + CascadeClassifier cascade(cascades[i]); + for( int j = 0; j < 100; j++ ) + { + int width = rng.uniform(1, 100); + int height = rng.uniform(1, 100); + Mat img(height, width, CV_8U); + randu(img, 0, 256); + cascade.detectMultiScale(img, objects); + } + } +}