From da3debda6d233af90e421e95700c63fc08b83b75 Mon Sep 17 00:00:00 2001 From: Kumataro Date: Fri, 9 Aug 2024 20:22:02 +0900 Subject: [PATCH] Merge pull request #25981 from Kumataro:fix25971 imgproc: add specific error code when cvtColor is used on an image with an invalid number of channels #25981 close #25971 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/core/include/opencv2/core/check.hpp | 3 +++ modules/core/src/check.cpp | 7 ++++++- modules/imgproc/src/color.hpp | 8 ++++---- modules/imgproc/src/color.simd_helpers.hpp | 4 ++-- modules/imgproc/test/test_color.cpp | 16 ++++++++++++++++ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/modules/core/include/opencv2/core/check.hpp b/modules/core/include/opencv2/core/check.hpp index c9ce97b6ae..aa1a839f70 100644 --- a/modules/core/include/opencv2/core/check.hpp +++ b/modules/core/include/opencv2/core/check.hpp @@ -135,6 +135,9 @@ CV_EXPORTS void CV_NORETURN check_failed_MatChannels(const int v, const CheckCon /// Example: depth == CV_32F || depth == CV_64F #define CV_CheckDepth(t, test_expr, msg) CV__CHECK_CUSTOM_TEST(_, MatDepth, t, (test_expr), #t, #test_expr, msg) +/// Example: channel == 1 || channel == 3 +#define CV_CheckChannels(t, test_expr, msg) CV__CHECK_CUSTOM_TEST(_, MatChannels, t, (test_expr), #t, #test_expr, msg) + /// Example: v == A || v == B #define CV_Check(v, test_expr, msg) CV__CHECK_CUSTOM_TEST(_, auto, v, (test_expr), #v, #test_expr, msg) diff --git a/modules/core/src/check.cpp b/modules/core/src/check.cpp index 2891f3a2e3..8a1fb37085 100644 --- a/modules/core/src/check.cpp +++ b/modules/core/src/check.cpp @@ -155,7 +155,12 @@ void check_failed_MatType(const int v, const CheckContext& ctx) } void check_failed_MatChannels(const int v, const CheckContext& ctx) { - check_failed_auto_(v, ctx); + std::stringstream ss; + ss << ctx.message << ":" << std::endl + << " '" << ctx.p2_str << "'" << std::endl + << "where" << std::endl + << " '" << ctx.p1_str << "' is " << v; + cv::error(cv::Error::BadNumChannels, ss.str(), ctx.func, ctx.file, ctx.line); } void check_failed_true(const bool v, const CheckContext& ctx) { diff --git a/modules/imgproc/src/color.hpp b/modules/imgproc/src/color.hpp index 883c9ccab4..3c3fa42972 100644 --- a/modules/imgproc/src/color.hpp +++ b/modules/imgproc/src/color.hpp @@ -202,8 +202,8 @@ struct CvtHelper int stype = _src.type(); scn = CV_MAT_CN(stype), depth = CV_MAT_DEPTH(stype); - CV_Check(scn, VScn::contains(scn), "Invalid number of channels in input image"); - CV_Check(dcn, VDcn::contains(dcn), "Invalid number of channels in output image"); + CV_CheckChannels(scn, VScn::contains(scn), "Invalid number of channels in input image"); + CV_CheckChannels(dcn, VDcn::contains(dcn), "Invalid number of channels in output image"); CV_CheckDepth(depth, VDepth::contains(depth), "Unsupported depth of input image"); if (_src.getObj() == _dst.getObj()) // inplace processing (#6653) @@ -247,8 +247,8 @@ struct OclHelper int scn = src.channels(); int depth = src.depth(); - CV_Check(scn, VScn::contains(scn), "Invalid number of channels in input image"); - CV_Check(dcn, VDcn::contains(dcn), "Invalid number of channels in output image"); + CV_CheckChannels(scn, VScn::contains(scn), "Invalid number of channels in input image"); + CV_CheckChannels(dcn, VDcn::contains(dcn), "Invalid number of channels in output image"); CV_CheckDepth(depth, VDepth::contains(depth), "Unsupported depth of input image"); switch (sizePolicy) diff --git a/modules/imgproc/src/color.simd_helpers.hpp b/modules/imgproc/src/color.simd_helpers.hpp index 06b9ba3d06..ef675d129a 100644 --- a/modules/imgproc/src/color.simd_helpers.hpp +++ b/modules/imgproc/src/color.simd_helpers.hpp @@ -89,8 +89,8 @@ struct CvtHelper int stype = _src.type(); scn = CV_MAT_CN(stype), depth = CV_MAT_DEPTH(stype); - CV_Check(scn, VScn::contains(scn), "Invalid number of channels in input image"); - CV_Check(dcn, VDcn::contains(dcn), "Invalid number of channels in output image"); + CV_CheckChannels(scn, VScn::contains(scn), "Invalid number of channels in input image"); + CV_CheckChannels(dcn, VDcn::contains(dcn), "Invalid number of channels in output image"); CV_CheckDepth(depth, VDepth::contains(depth), "Unsupported depth of input image"); if (_src.getObj() == _dst.getObj()) // inplace processing (#6653) diff --git a/modules/imgproc/test/test_color.cpp b/modules/imgproc/test/test_color.cpp index 1229a468eb..3e63874436 100644 --- a/modules/imgproc/test/test_color.cpp +++ b/modules/imgproc/test/test_color.cpp @@ -3203,4 +3203,20 @@ TEST(ImgProc_RGB2Lab, NaN_21111) #endif } +// See https://github.com/opencv/opencv/issues/25971 +// If num of channels is not suitable for selected cv::ColorConversionCodes, +// e.code must be cv::Error::BadNumChannels. +TEST(ImgProc_cvtColor_InvalidNumOfChannels, regression_25971) +{ + try { + cv::Mat src = cv::Mat::zeros(100, 100, CV_8UC1); + cv::Mat dst; + EXPECT_THROW(cv::cvtColor(src, dst, COLOR_RGB2GRAY), cv::Exception); + }catch(const cv::Exception& e) { + EXPECT_EQ(e.code, cv::Error::BadNumChannels); + }catch(...) { + FAIL() << "Unexpected exception is happened."; + } +} + }} // namespace