From 481f786fe7750ebf139aeb96045147d47d6182dc Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 28 May 2015 17:05:43 +0300 Subject: [PATCH 1/6] added simple HAL test; added RHO homography test --- modules/calib3d/test/test_homography.cpp | 35 +++- modules/core/test/test_hal_core.cpp | 195 +++++++++++++++++++++++ 2 files changed, 222 insertions(+), 8 deletions(-) create mode 100644 modules/core/test/test_hal_core.cpp diff --git a/modules/calib3d/test/test_homography.cpp b/modules/calib3d/test/test_homography.cpp index 1076a7c42..0d4fae50b 100644 --- a/modules/calib3d/test/test_homography.cpp +++ b/modules/calib3d/test/test_homography.cpp @@ -662,7 +662,7 @@ TEST(Calib3d_Homography, fromImages) std::vector< DMatch > good_matches; for( int i = 0; i < descriptors_1.rows; i++ ) { - if( matches[i].distance <= 42 ) + if( matches[i].distance <= 100 ) good_matches.push_back( matches[i]); } @@ -676,13 +676,32 @@ TEST(Calib3d_Homography, fromImages) pointframe2.push_back( keypoints_2[ good_matches[i].trainIdx ].pt ); } - Mat inliers; - Mat H = findHomography( pointframe1, pointframe2, RANSAC,3.0,inliers); - int ninliers = countNonZero(inliers); - printf("nfeatures1 = %d, nfeatures2=%d, good matches=%d, ninliers=%d\n", + Mat H0, H1, inliers0, inliers1; + double min_t0 = DBL_MAX, min_t1 = DBL_MAX; + for( int i = 0; i < 10; i++ ) + { + double t = (double)getTickCount(); + H0 = findHomography( pointframe1, pointframe2, RANSAC, 3.0, inliers0 ); + t = (double)getTickCount() - t; + min_t0 = std::min(min_t0, t); + } + int ninliers0 = countNonZero(inliers0); + for( int i = 0; i < 10; i++ ) + { + double t = (double)getTickCount(); + H1 = findHomography( pointframe1, pointframe2, RHO, 3.0, inliers1 ); + t = (double)getTickCount() - t; + min_t1 = std::min(min_t1, t); + } + int ninliers1 = countNonZero(inliers1); + double freq = getTickFrequency(); + printf("nfeatures1 = %d, nfeatures2=%d, matches=%d, ninliers(RANSAC)=%d, " + "time(RANSAC)=%.2fmsec, ninliers(RHO)=%d, time(RHO)=%.2fmsec\n", (int)keypoints_1.size(), (int)keypoints_2.size(), - (int)good_matches.size(), ninliers); + (int)good_matches.size(), ninliers0, min_t0*1000./freq, ninliers1, min_t1*1000./freq); - ASSERT_TRUE(!H.empty()); - ASSERT_GE(ninliers, 80); + ASSERT_TRUE(!H0.empty()); + ASSERT_GE(ninliers0, 80); + ASSERT_TRUE(!H1.empty()); + ASSERT_GE(ninliers1, 80); } diff --git a/modules/core/test/test_hal_core.cpp b/modules/core/test/test_hal_core.cpp new file mode 100644 index 000000000..3dd0a4c43 --- /dev/null +++ b/modules/core/test/test_hal_core.cpp @@ -0,0 +1,195 @@ +/*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. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// 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. +// +// * The name of the copyright holders may not be used to endorse or promote products +// 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. +// In no event shall the OpenCV Foundation or contributors be liable for any direct, +// 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 "test_precomp.hpp" +#include "opencv2/hal.hpp" + +using namespace cv; + +enum +{ + HAL_EXP = 0, + HAL_LOG = 1, + HAL_SQRT = 2 +}; + +TEST(Core_HAL, mathfuncs) +{ + for( int hcase = 0; hcase < 6; hcase++ ) + { + int depth = hcase % 2 == 0 ? CV_32F : CV_64F; + double eps = depth == CV_32F ? 1e-5 : 1e-10; + int nfunc = hcase / 2; + int n = 100; + + Mat src(1, n, depth), dst(1, n, depth), dst0(1, n, depth); + randu(src, 1, 10); + + double min_hal_t = DBL_MAX, min_ocv_t = DBL_MAX; + + for( int iter = 0; iter < 10; iter++ ) + { + double t = (double)getTickCount(); + switch (nfunc) + { + case HAL_EXP: + if( depth == CV_32F ) + hal::exp(src.ptr(), dst.ptr(), n); + else + hal::exp(src.ptr(), dst.ptr(), n); + break; + case HAL_LOG: + if( depth == CV_32F ) + hal::log(src.ptr(), dst.ptr(), n); + else + hal::log(src.ptr(), dst.ptr(), n); + break; + case HAL_SQRT: + if( depth == CV_32F ) + hal::sqrt(src.ptr(), dst.ptr(), n); + else + hal::sqrt(src.ptr(), dst.ptr(), n); + break; + default: + CV_Error(Error::StsBadArg, "unknown function"); + } + t = (double)getTickCount() - t; + min_hal_t = std::min(min_hal_t, t); + + t = (double)getTickCount(); + switch (nfunc) + { + case HAL_EXP: + exp(src, dst0); + break; + case HAL_LOG: + log(src, dst0); + break; + case HAL_SQRT: + pow(src, 0.5, dst0); + break; + default: + CV_Error(Error::StsBadArg, "unknown function"); + } + t = (double)getTickCount() - t; + min_ocv_t = std::min(min_ocv_t, t); + } + EXPECT_LE(norm(dst, dst0, NORM_INF | NORM_RELATIVE), eps); + + double freq = getTickFrequency(); + printf("%s (N=%d, %s): hal time=%.2fusec, ocv time=%.2fusec\n", + (nfunc == HAL_EXP ? "exp" : nfunc == HAL_LOG ? "log" : nfunc == HAL_SQRT ? "sqrt" : "???"), + n, (depth == CV_32F ? "f32" : "f64"), min_hal_t*1e6/freq, min_ocv_t*1e6/freq); + } +} + +enum +{ + HAL_LU = 0, + HAL_CHOL = 1 +}; + +TEST(Core_HAL, mat_decomp) +{ + for( int hcase = 0; hcase < 16; hcase++ ) + { + int depth = hcase % 2 == 0 ? CV_32F : CV_64F; + int size = (hcase / 2) % 4; + size = size == 0 ? 3 : size == 1 ? 4 : size == 2 ? 6 : 15; + int nfunc = (hcase / 8); + double eps = depth == CV_32F ? 1e-5 : 1e-10; + + if( size == 3 ) + continue; + + Mat a0(size, size, depth), a(size, size, depth), b(size, 1, depth), x(size, 1, depth), x0(size, 1, depth); + randu(a0, -1, 1); + a0 = a0*a0.t(); + randu(b, -1, 1); + + double min_hal_t = DBL_MAX, min_ocv_t = DBL_MAX; + size_t asize = size*size*a.elemSize(); + size_t bsize = size*b.elemSize(); + + for( int iter = 0; iter < 10; iter++ ) + { + memcpy(x.ptr(), b.ptr(), bsize); + memcpy(a.ptr(), a0.ptr(), asize); + + double t = (double)getTickCount(); + switch (nfunc) + { + case HAL_LU: + if( depth == CV_32F ) + hal::LU(a.ptr(), a.step, size, x.ptr(), x.step, 1); + else + hal::LU(a.ptr(), a.step, size, x.ptr(), x.step, 1); + break; + case HAL_CHOL: + if( depth == CV_32F ) + hal::Cholesky(a.ptr(), a.step, size, x.ptr(), x.step, 1); + else + hal::Cholesky(a.ptr(), a.step, size, x.ptr(), x.step, 1); + break; + default: + CV_Error(Error::StsBadArg, "unknown function"); + } + t = (double)getTickCount() - t; + min_hal_t = std::min(min_hal_t, t); + + t = (double)getTickCount(); + solve(a0, b, x0, (nfunc == HAL_LU ? DECOMP_LU : DECOMP_CHOLESKY)); + t = (double)getTickCount() - t; + min_ocv_t = std::min(min_ocv_t, t); + } + //std::cout << "x: " << Mat(x.t()) << std::endl; + //std::cout << "x0: " << Mat(x0.t()) << std::endl; + + EXPECT_LE(norm(x, x0, NORM_INF | NORM_RELATIVE), eps); + + double freq = getTickFrequency(); + printf("%s (%d x %d, %s): hal time=%.2fusec, ocv time=%.2fusec\n", + (nfunc == HAL_LU ? "LU" : nfunc == HAL_CHOL ? "Cholesky" : "???"), + size, size, + (depth == CV_32F ? "f32" : "f64"), + min_hal_t*1e6/freq, min_ocv_t*1e6/freq); + } +} From 5f8f56ea0e7266089bab12d500100d9ca57af366 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 28 May 2015 17:24:22 +0300 Subject: [PATCH 2/6] report an error when trying to load HOG cascade --- modules/objdetect/src/cascadedetect.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/objdetect/src/cascadedetect.cpp b/modules/objdetect/src/cascadedetect.cpp index 63edd0d8c..5cc861e0f 100644 --- a/modules/objdetect/src/cascadedetect.cpp +++ b/modules/objdetect/src/cascadedetect.cpp @@ -1402,8 +1402,10 @@ bool CascadeClassifierImpl::Data::read(const FileNode &root) else if( featureTypeStr == CC_LBP ) featureType = FeatureEvaluator::LBP; else if( featureTypeStr == CC_HOG ) + { featureType = FeatureEvaluator::HOG; - + CV_Error(Error::StsNotImplemented, "HOG cascade is not supported in 3.0"); + } else return false; From 036c4389040c086645e2f4d1fcd221eabfacb6f7 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 28 May 2015 19:01:56 +0300 Subject: [PATCH 3/6] make facedetect.py work when the nested cascade is not specified --- samples/python2/facedetect.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/samples/python2/facedetect.py b/samples/python2/facedetect.py index 72b8561c3..8c6b27ab1 100755 --- a/samples/python2/facedetect.py +++ b/samples/python2/facedetect.py @@ -49,11 +49,12 @@ if __name__ == '__main__': rects = detect(gray, cascade) vis = img.copy() draw_rects(vis, rects, (0, 255, 0)) - for x1, y1, x2, y2 in rects: - roi = gray[y1:y2, x1:x2] - vis_roi = vis[y1:y2, x1:x2] - subrects = detect(roi.copy(), nested) - draw_rects(vis_roi, subrects, (255, 0, 0)) + if not nested.empty(): + for x1, y1, x2, y2 in rects: + roi = gray[y1:y2, x1:x2] + vis_roi = vis[y1:y2, x1:x2] + subrects = detect(roi.copy(), nested) + draw_rects(vis_roi, subrects, (255, 0, 0)) dt = clock() - t draw_str(vis, (20, 20), 'time: %.1f ms' % (dt*1000)) From 5a94a95fbfd41e12e694d0527c0e37fb85c8d0dc Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 28 May 2015 19:33:21 +0300 Subject: [PATCH 4/6] improvements in Haar CascadeClassifier: 1) use CV_32S instead of CV_32F for the integral of squares (which is more accurate and more efficient); 2) skip the window if its contrast is too low --- modules/imgproc/src/sumpixels.cpp | 3 ++ modules/objdetect/src/cascadedetect.cpp | 33 +++++++++++-------- modules/objdetect/src/opencl/cascadedetect.cl | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/modules/imgproc/src/sumpixels.cpp b/modules/imgproc/src/sumpixels.cpp index 16c7c7ef2..ce6aa794c 100755 --- a/modules/imgproc/src/sumpixels.cpp +++ b/modules/imgproc/src/sumpixels.cpp @@ -318,6 +318,7 @@ static void integral_##suffix( T* src, size_t srcstep, ST* sum, size_t sumstep, { integral_(src, srcstep, sum, sumstep, sqsum, sqsumstep, tilted, tiltedstep, size, cn); } DEF_INTEGRAL_FUNC(8u32s, uchar, int, double) +DEF_INTEGRAL_FUNC(8u32s32s, uchar, int, int) DEF_INTEGRAL_FUNC(8u32f64f, uchar, float, double) DEF_INTEGRAL_FUNC(8u64f64f, uchar, double, double) DEF_INTEGRAL_FUNC(16u64f64f, ushort, double, double) @@ -505,6 +506,8 @@ void cv::integral( InputArray _src, OutputArray _sum, OutputArray _sqsum, Output func = (IntegralFunc)GET_OPTIMIZED(integral_8u32s); else if( depth == CV_8U && sdepth == CV_32S && sqdepth == CV_32F ) func = (IntegralFunc)integral_8u32s32f; + else if( depth == CV_8U && sdepth == CV_32S && sqdepth == CV_32S ) + func = (IntegralFunc)integral_8u32s32s; else if( depth == CV_8U && sdepth == CV_32F && sqdepth == CV_64F ) func = (IntegralFunc)integral_8u32f64f; else if( depth == CV_8U && sdepth == CV_32F && sqdepth == CV_32F ) diff --git a/modules/objdetect/src/cascadedetect.cpp b/modules/objdetect/src/cascadedetect.cpp index 5cc861e0f..056abecc8 100644 --- a/modules/objdetect/src/cascadedetect.cpp +++ b/modules/objdetect/src/cascadedetect.cpp @@ -627,33 +627,33 @@ void HaarEvaluator::computeChannels(int scaleIdx, InputArray img) 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; + sqsum.flags = (sqsum.flags & ~UMat::DEPTH_MASK) | CV_32S; if (hasTiltedFeatures) { 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); + integral(img, sum, sqsum, tilted, CV_32S, CV_32S); } else { 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); + integral(img, sum, sqsum, noArray(), CV_32S, CV_32S); + CV_Assert(sqsum.u == u && sqsum.size() == s.szi && sqsum.type()==CV_32S); } } else { Mat sum(s.szi, CV_32S, sbuf.ptr() + s.layer_ofs, sbuf.step); - Mat sqsum(s.szi, CV_32F, sum.ptr() + sqofs, sbuf.step); + Mat sqsum(s.szi, CV_32S, sum.ptr() + sqofs, sbuf.step); if (hasTiltedFeatures) { Mat tilted(s.szi, CV_32S, sum.ptr() + tofs, sbuf.step); - integral(img, sum, sqsum, tilted, CV_32S, CV_32F); + integral(img, sum, sqsum, tilted, CV_32S, CV_32S); } else - integral(img, sum, sqsum, noArray(), CV_32S, CV_32F); + integral(img, sum, sqsum, noArray(), CV_32S, CV_32S); } } @@ -689,18 +689,23 @@ bool HaarEvaluator::setWindow( Point pt, int scaleIdx ) return false; pwin = &sbuf.at(pt) + s.layer_ofs; - const float* pq = (const float*)(pwin + sqofs); + const int* pq = (const int*)(pwin + sqofs); int valsum = CALC_SUM_OFS(nofs, pwin); - float valsqsum = CALC_SUM_OFS(nofs, pq); + unsigned valsqsum = (unsigned)(CALC_SUM_OFS(nofs, pq)); - double nf = (double)normrect.area() * valsqsum - (double)valsum * valsum; + double area = normrect.area(); + double nf = area * valsqsum - (double)valsum * valsum; if( nf > 0. ) + { nf = std::sqrt(nf); + varianceNormFactor = (float)(1./nf); + return area*varianceNormFactor < 1e-1; + } else - nf = 1.; - varianceNormFactor = (float)(1./nf); - - return true; + { + varianceNormFactor = 1.f; + return false; + } } diff --git a/modules/objdetect/src/opencl/cascadedetect.cl b/modules/objdetect/src/opencl/cascadedetect.cl index 7ab581a28..ccc9c6d68 100644 --- a/modules/objdetect/src/opencl/cascadedetect.cl +++ b/modules/objdetect/src/opencl/cascadedetect.cl @@ -160,7 +160,7 @@ void runHaarClassifier( __global const int* psum = psum1; #endif - __global const float* psqsum = (__global const float*)(psum1 + sqofs); + __global const int* psqsum = (__global const int*)(psum1 + sqofs); float sval = (psum[nofs.x] - psum[nofs.y] - psum[nofs.z] + psum[nofs.w])*invarea; float sqval = (psqsum[nofs0.x] - psqsum[nofs0.y] - psqsum[nofs0.z] + psqsum[nofs0.w])*invarea; float nf = (float)normarea * sqrt(max(sqval - sval * sval, 0.f)); From 8c3c6b31fc9eece0ddb62477b3809c03c07326cb Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 28 May 2015 20:15:22 +0300 Subject: [PATCH 5/6] make sure the returned rectangles are inside the image (http://code.opencv.org/issues/3136) --- modules/objdetect/src/cascadedetect.cpp | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/modules/objdetect/src/cascadedetect.cpp b/modules/objdetect/src/cascadedetect.cpp index 056abecc8..86a36bf8f 100644 --- a/modules/objdetect/src/cascadedetect.cpp +++ b/modules/objdetect/src/cascadedetect.cpp @@ -1587,6 +1587,44 @@ bool CascadeClassifier::read(const FileNode &root) return ok; } +static 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); + if(a) + { + CV_Assert(a->size() == n); + } + if(b) + { + CV_Assert(b->size() == n); + } + + for( i = 0; i < n; i++ ) + { + Rect r = win0 & objects[i]; + if( r.area() > 0 ) + { + objects[j] = r; + if( i > j ) + { + if(a) a->at(j) = a->at(i); + if(b) b->at(j) = b->at(i); + } + j++; + } + } + + if( j < n ) + { + objects.resize(j); + if(a) a->resize(j); + if(b) b->resize(j); + } +} + void CascadeClassifier::detectMultiScale( InputArray image, CV_OUT std::vector& objects, double scaleFactor, @@ -1596,6 +1634,7 @@ void CascadeClassifier::detectMultiScale( InputArray image, { CV_Assert(!empty()); cc->detectMultiScale(image, objects, scaleFactor, minNeighbors, flags, minSize, maxSize); + clipObjects(image.size(), objects, 0, 0); } void CascadeClassifier::detectMultiScale( InputArray image, @@ -1608,6 +1647,7 @@ void CascadeClassifier::detectMultiScale( InputArray image, CV_Assert(!empty()); cc->detectMultiScale(image, objects, numDetections, scaleFactor, minNeighbors, flags, minSize, maxSize); + clipObjects(image.size(), objects, &numDetections, 0); } void CascadeClassifier::detectMultiScale( InputArray image, @@ -1623,6 +1663,7 @@ void CascadeClassifier::detectMultiScale( InputArray image, cc->detectMultiScale(image, objects, rejectLevels, levelWeights, scaleFactor, minNeighbors, flags, minSize, maxSize, outputRejectLevels); + clipObjects(image.size(), objects, &rejectLevels, &levelWeights); } bool CascadeClassifier::isOldFormatCascade() const From 882c0321f43f4f24cb83ae0865ee92f2ea931128 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 28 May 2015 21:02:27 +0300 Subject: [PATCH 6/6] clip the found objects in HOG as well (http://code.opencv.org/issues/3825); added test to check CascadeClassifier on small images (http://code.opencv.org/issues/3710) --- modules/objdetect/src/cascadedetect.cpp | 5 ++-- modules/objdetect/src/cascadedetect.hpp | 3 ++ modules/objdetect/src/hog.cpp | 6 +++- modules/objdetect/test/test_cascadeandhog.cpp | 30 ++++++++++++++++++- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/modules/objdetect/src/cascadedetect.cpp b/modules/objdetect/src/cascadedetect.cpp index 86a36bf8f..20800ae52 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 4cbf3e9bf..696ab40d0 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 4697a0105..710c28517 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 df33ffe93..31afbe6da 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); + } + } +}