From 9db93d773dec95e2b6cf34fe8eb67983a1f8f24a Mon Sep 17 00:00:00 2001 From: Vitaly Tuzov Date: Thu, 11 Aug 2016 20:20:36 +0300 Subject: [PATCH 1/2] Fix for MSER::detectRegions crash on images with either dimension less than 3 --- modules/features2d/include/opencv2/features2d.hpp | 2 +- modules/features2d/src/mser.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/features2d/include/opencv2/features2d.hpp b/modules/features2d/include/opencv2/features2d.hpp index 32fdabd8a4..4d362923ce 100644 --- a/modules/features2d/include/opencv2/features2d.hpp +++ b/modules/features2d/include/opencv2/features2d.hpp @@ -355,7 +355,7 @@ public: /** @brief Detect %MSER regions - @param image input image (8UC1, 8UC3 or 8UC4) + @param image input image (8UC1, 8UC3 or 8UC4, must be greater or equal than 3x3) @param msers resulting list of point sets @param bboxes resulting bounding boxes */ diff --git a/modules/features2d/src/mser.cpp b/modules/features2d/src/mser.cpp index 1143addc12..72b2314d9c 100644 --- a/modules/features2d/src/mser.cpp +++ b/modules/features2d/src/mser.cpp @@ -1020,12 +1020,11 @@ extractMSER_8uC3( const Mat& src, void MSER_Impl::detectRegions( InputArray _src, vector >& msers, vector& bboxes ) { Mat src = _src.getMat(); - size_t npix = src.total(); msers.clear(); bboxes.clear(); - if( npix == 0 ) + if( src.rows < 3 || src.cols < 3 ) return; Size size = src.size(); From 62c1a5a15232ddc9cc97b46d0d3c07a4c5b35d9e Mon Sep 17 00:00:00 2001 From: Vitaly Tuzov Date: Mon, 15 Aug 2016 11:28:52 +0300 Subject: [PATCH 2/2] Replaced quiet return from MSER:detectRegion function with exeption throwing to notify user in case small image is processed --- modules/features2d/src/mser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/features2d/src/mser.cpp b/modules/features2d/src/mser.cpp index 72b2314d9c..4f2cab1dc0 100644 --- a/modules/features2d/src/mser.cpp +++ b/modules/features2d/src/mser.cpp @@ -1025,7 +1025,7 @@ void MSER_Impl::detectRegions( InputArray _src, vector >& msers, v bboxes.clear(); if( src.rows < 3 || src.cols < 3 ) - return; + CV_Error(Error::StsBadArg, "Input image is too small. Expected at least 3x3"); Size size = src.size();