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