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
This commit is contained in:
Kumataro 2024-08-09 20:22:02 +09:00 committed by GitHub
parent 6cd730a02c
commit da3debda6d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 31 additions and 7 deletions

View File

@ -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)

View File

@ -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_<int>(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)
{

View File

@ -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)

View File

@ -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)

View File

@ -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