Merge pull request #23688 from cpoerschke:4.x-pr-21959-prep

imgproc: add contour values check to IntelligentScissorsMB tests

Preparation for the #21959 changes as per @asmorkalov's https://github.com/opencv/opencv/pull/21959#issuecomment-1560511500 suggestion.

### 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
- [ ] 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
pull/23762/head
Christine Poerschke 2 years ago committed by GitHub
parent b9ce87e8e2
commit d3e7968927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 135
      modules/imgproc/test/test_intelligent_scissors.cpp

@ -147,11 +147,72 @@ void show(const Mat& img, const std::vector<Point> pts)
} }
} }
Size estimateContourSize(const std::vector<Point>& pts)
{
Size s(0,0);
for (size_t i = 0; i < pts.size(); i++)
{
if (s.width < pts[i].x)
s.width = pts[i].x;
if (s.height < pts[i].y)
s.height = pts[i].y;
}
return s;
}
int contoursAreaPixelsMismatch(const std::vector<Point>& pts, const std::vector<Point>& gt)
{
Size ptsSize = estimateContourSize(pts);
Size gtSize = estimateContourSize(gt);
Size imgSize(std::max(ptsSize.width, gtSize.width)+1, std::max(ptsSize.height, gtSize.height)+1);
Mat ptsArea = Mat::zeros(imgSize, CV_8UC1);
Mat gtArea = Mat::zeros(imgSize, CV_8UC1);
std::vector<std::vector<Point>> pts_wrapped = {pts};
std::vector<std::vector<Point>> gt_wrapped = {gt};
drawContours(ptsArea, pts_wrapped, -1, Scalar(255), FILLED);
drawContours(gtArea, gt_wrapped, -1, Scalar(255), FILLED);
Mat uni = ptsArea | gtArea;
Mat intersection = ptsArea & gtArea;
bitwise_not(intersection, intersection);
Mat delta = uni & intersection;
return countNonZero(delta);
}
void checkContour(std::vector<Point>& pts,
const bool backward = false,
int allowed_mismatch = 0)
{
const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
CV_Assert(test_info);
const std::string name = std::string(cvtest::TS::ptr()->get_data_path() + "imgproc/" + test_info->test_case_name() + "-" + test_info->name() + (backward ? "-backward" : "") + ".xml");
std::vector<Point> reference_pts;
#ifdef GENERATE_TEST_DATA
{
cv::FileStorage fs(name, cv::FileStorage::WRITE);
fs << "pts" << pts;
}
reference_pts = pts;
#else
FileStorage fs(name, FileStorage::READ);
read(fs["pts"], reference_pts, std::vector<Point>());
#endif
if (!allowed_mismatch)
EXPECT_EQ(pts, reference_pts);
else
EXPECT_LE(contoursAreaPixelsMismatch(pts, reference_pts), allowed_mismatch);
}
TEST(Imgproc_IntelligentScissorsMB, rect) TEST(Imgproc_IntelligentScissorsMB, rect)
{ {
segmentation::IntelligentScissorsMB tool; segmentation::IntelligentScissorsMB tool;
Mat image = getTestImage1();
tool.applyImage(getTestImage1()); tool.applyImage(image);
Point source_point(50, 30); Point source_point(50, 30);
tool.buildMap(source_point); tool.buildMap(source_point);
@ -159,15 +220,18 @@ TEST(Imgproc_IntelligentScissorsMB, rect)
Point target_point(100, 30); Point target_point(100, 30);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts);
show(image, pts);
tool.applyImage(getTestImage2()); Mat image2 = getTestImage2();
tool.applyImage(image2);
tool.buildMap(source_point); tool.buildMap(source_point);
std::vector<Point> pts2; std::vector<Point> pts2;
tool.getContour(target_point, pts2, true/*backward*/); tool.getContour(target_point, pts2, true/*backward*/);
checkContour(pts2, true/*backward*/);
EXPECT_EQ(pts.size(), pts2.size()); show(image2, pts2);
} }
TEST(Imgproc_IntelligentScissorsMB, lines) TEST(Imgproc_IntelligentScissorsMB, lines)
@ -182,8 +246,7 @@ TEST(Imgproc_IntelligentScissorsMB, lines)
Point target_point(150, 50); Point target_point(150, 50);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts);
EXPECT_EQ((size_t)121, pts.size());
show(image, pts); show(image, pts);
} }
@ -201,8 +264,7 @@ TEST(Imgproc_IntelligentScissorsMB, circles)
Point target_point(150, 50); Point target_point(150, 50);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts);
EXPECT_EQ((size_t)101, pts.size());
show(image, pts); show(image, pts);
} }
@ -218,13 +280,10 @@ TEST(Imgproc_IntelligentScissorsMB, circles_gradient)
Point target_point(150, 50); Point target_point(150, 50);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts);
EXPECT_EQ((size_t)101, pts.size());
show(image, pts); show(image, pts);
} }
#define PTS_SIZE_EPS 2
TEST(Imgproc_IntelligentScissorsMB, grayscale) TEST(Imgproc_IntelligentScissorsMB, grayscale)
{ {
segmentation::IntelligentScissorsMB tool; segmentation::IntelligentScissorsMB tool;
@ -238,10 +297,7 @@ TEST(Imgproc_IntelligentScissorsMB, grayscale)
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 2);
size_t gold = 206;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }
@ -260,10 +316,7 @@ TEST(Imgproc_IntelligentScissorsMB, check_features_grayscale_1_0_0_zerro_crossin
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 11);
size_t gold = 207;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }
@ -282,10 +335,7 @@ TEST(Imgproc_IntelligentScissorsMB, check_features_grayscale_1_0_0_canny)
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 6);
size_t gold = 201;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }
@ -303,10 +353,7 @@ TEST(Imgproc_IntelligentScissorsMB, check_features_grayscale_0_1_0)
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 4);
size_t gold = 166;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }
@ -324,10 +371,7 @@ TEST(Imgproc_IntelligentScissorsMB, check_features_grayscale_0_0_1)
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 2);
size_t gold = 197;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }
@ -344,10 +388,7 @@ TEST(Imgproc_IntelligentScissorsMB, color)
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 2);
size_t gold = 205;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }
@ -365,10 +406,7 @@ TEST(Imgproc_IntelligentScissorsMB, color_canny)
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 2);
size_t gold = 200;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }
@ -397,10 +435,7 @@ TEST(Imgproc_IntelligentScissorsMB, color_custom_features_edge)
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 2);
size_t gold = 201;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }
@ -427,10 +462,7 @@ TEST(Imgproc_IntelligentScissorsMB, color_custom_features_all)
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 9);
size_t gold = 201;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }
@ -456,10 +488,7 @@ TEST(Imgproc_IntelligentScissorsMB, color_custom_features_edge_magnitude)
Point target_point(413, 155); Point target_point(413, 155);
std::vector<Point> pts; std::vector<Point> pts;
tool.getContour(target_point, pts); tool.getContour(target_point, pts);
checkContour(pts, false, 9);
size_t gold = 201;
EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
show(image, pts); show(image, pts);
} }

Loading…
Cancel
Save