diff --git a/modules/imgproc/src/deriv.cpp b/modules/imgproc/src/deriv.cpp index b909080e6e..e3b4a6d3fa 100644 --- a/modules/imgproc/src/deriv.cpp +++ b/modules/imgproc/src/deriv.cpp @@ -619,7 +619,9 @@ void cv::Laplacian( InputArray _src, OutputArray _dst, int ddepth, int ksize, ddepth = sdepth; _dst.create( _src.size(), CV_MAKETYPE(ddepth, cn) ); - int ktype = std::max(CV_32F, std::max(ddepth, sdepth)); + // the kernel is filled as float or double below, so it must be CV_32F or CV_64F + // (std::max over the depths picked CV_16F, since CV_16F > CV_64F) + int ktype = sdepth == CV_64F || ddepth == CV_64F ? CV_64F : CV_32F; Mat kernel; if( ksize == 1 || ksize == 3 ) diff --git a/modules/imgproc/test/test_filter.cpp b/modules/imgproc/test/test_filter.cpp index 1bba815a3b..73788527bb 100644 --- a/modules/imgproc/test/test_filter.cpp +++ b/modules/imgproc/test/test_filter.cpp @@ -2566,6 +2566,15 @@ TEST(Imgproc, filter_empty_src_16857) EXPECT_TRUE(dst2.empty()); } +TEST(Imgproc_Laplacian, regression_16F_kernel_overflow) +{ + // the 3x3 kernel used to be created as CV_16F and then filled with 9 doubles, + // writing past its end; now it is CV_32F and filter2D rejects the CV_16F input + Mat src(8, 8, CV_16FC1, Scalar(1)), dst; + EXPECT_THROW(Laplacian(src, dst, -1, 1), cv::Exception); + EXPECT_THROW(Laplacian(src, dst, -1, 3), cv::Exception); +} + TEST(Imgproc_GaussianBlur, regression_11303) { cv::Mat dst;