mirror of
https://github.com/opencv/opencv.git
synced 2026-11-09 18:04:29 -06:00
Merge pull request #30690 from vrabaud:imgwarp
imgproc: handle empty input image in findContours and findTRUContours - #30690 Since cv::copyMakeBorder now asserts that the source matrix is non-empty, calling cv::findContours or cv::findTRUContours on an empty image threw an exception instead of returning an empty contour list. Guard against empty input images by clearing the output contours (and hierarchy) and returning early, matching cv::findContoursLinkRuns. ### 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. - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -700,7 +700,10 @@ void cv::findContours(InputArray _image,
|
||||
|
||||
// preprocess
|
||||
Mat image;
|
||||
copyMakeBorder(_image, image, 1, 1, 1, 1, BORDER_CONSTANT | BORDER_ISOLATED, Scalar(0));
|
||||
if (_image.empty())
|
||||
image = Mat::zeros(2, 2, _image.type());
|
||||
else
|
||||
copyMakeBorder(_image, image, 1, 1, 1, 1, BORDER_CONSTANT | BORDER_ISOLATED, Scalar(0));
|
||||
if (image.type() != CV_32SC1)
|
||||
threshold(image, image, 0, 1, THRESH_BINARY);
|
||||
|
||||
|
||||
@@ -648,6 +648,11 @@ void findTRUContours(InputArray _src, OutputArrayOfArrays _contours, int minSize
|
||||
CV_INSTRUMENT_REGION();
|
||||
Mat src = _src.getMat();
|
||||
CV_Assert(src.type() == CV_8UC1);
|
||||
if (src.empty())
|
||||
{
|
||||
_contours.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// Buffer handling
|
||||
cv::Mat padded;
|
||||
|
||||
@@ -125,6 +125,35 @@ TEST(Imgproc_FindContours, border)
|
||||
ASSERT_EQ(0, cvtest::norm(img, img_draw_contours, NORM_INF));
|
||||
}
|
||||
|
||||
TEST(Imgproc_FindContours, empty_input)
|
||||
{
|
||||
Mat empty;
|
||||
std::vector<std::vector<Point> > contours(1, std::vector<Point>(1, Point(1, 2)));
|
||||
std::vector<Vec4i> hierarchy(1, Vec4i(0, 0, 0, 0));
|
||||
|
||||
// General path (with hierarchy)
|
||||
findContours(empty, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
|
||||
EXPECT_TRUE(contours.empty());
|
||||
EXPECT_TRUE(hierarchy.empty());
|
||||
|
||||
// Fast path (RETR_LIST without hierarchy -> findTRUContours)
|
||||
contours.assign(1, std::vector<Point>(1, Point(1, 2)));
|
||||
findContours(empty, contours, RETR_LIST, CHAIN_APPROX_SIMPLE, Point(3, 4));
|
||||
EXPECT_TRUE(contours.empty());
|
||||
|
||||
// RETR_FLOODFILL with empty CV_32SC1
|
||||
Mat empty32s(0, 0, CV_32SC1);
|
||||
contours.assign(1, std::vector<Point>(1, Point(1, 2)));
|
||||
hierarchy.assign(1, Vec4i(0, 0, 0, 0));
|
||||
findContours(empty32s, contours, hierarchy, RETR_FLOODFILL, CHAIN_APPROX_SIMPLE);
|
||||
EXPECT_TRUE(contours.empty());
|
||||
EXPECT_TRUE(hierarchy.empty());
|
||||
|
||||
// Empty input still goes through type/mode validation
|
||||
EXPECT_THROW(findContours(Mat(0, 0, CV_32FC1), contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE), cv::Exception);
|
||||
EXPECT_THROW(findContours(empty, contours, hierarchy, RETR_FLOODFILL, CHAIN_APPROX_SIMPLE), cv::Exception);
|
||||
}
|
||||
|
||||
TEST(Imgproc_FindContours, regression_4363_shared_nbd)
|
||||
{
|
||||
// Create specific test image
|
||||
|
||||
Reference in New Issue
Block a user