123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- // This file is part of OpenCV project.
- // It is subject to the license terms in the LICENSE file found in the top-level directory
- // of this distribution and at http://opencv.org/license.html.
- #include "test_precomp.hpp"
- //#include "opencv2/imgproc/segmentation.hpp"
- namespace opencv_test { namespace {
- Mat getTestImageGray()
- {
- static Mat m;
- if (m.empty())
- {
- m = imread(findDataFile("shared/lena.png"), IMREAD_GRAYSCALE);
- }
- return m.clone();
- }
- Mat getTestImageColor()
- {
- static Mat m;
- if (m.empty())
- {
- m = imread(findDataFile("shared/lena.png"), IMREAD_COLOR);
- }
- return m.clone();
- }
- Mat getTestImage1()
- {
- static Mat m;
- if (m.empty())
- {
- m.create(Size(200, 100), CV_8UC1);
- m.setTo(Scalar::all(128));
- Rect roi(50, 30, 100, 40);
- m(roi).setTo(Scalar::all(0));
- #if 0
- imshow("image", m);
- waitKey();
- #endif
- }
- return m.clone();
- }
- Mat getTestImage2()
- {
- static Mat m;
- if (m.empty())
- {
- m.create(Size(200, 100), CV_8UC1);
- m.setTo(Scalar::all(128));
- Rect roi(40, 30, 100, 40);
- m(roi).setTo(Scalar::all(255));
- #if 0
- imshow("image", m);
- waitKey();
- #endif
- }
- return m.clone();
- }
- Mat getTestImage3()
- {
- static Mat m;
- if (m.empty())
- {
- m.create(Size(200, 100), CV_8UC1);
- m.setTo(Scalar::all(128));
- Scalar color(0,0,0,0);
- line(m, Point(30, 50), Point(50, 50), color, 1);
- line(m, Point(50, 50), Point(80, 30), color, 1);
- line(m, Point(150, 50), Point(80, 30), color, 1);
- line(m, Point(150, 50), Point(180, 50), color, 1);
- line(m, Point(80, 10), Point(80, 90), Scalar::all(200), 1);
- line(m, Point(100, 10), Point(100, 90), Scalar::all(200), 1);
- line(m, Point(120, 10), Point(120, 90), Scalar::all(200), 1);
- #if 0
- imshow("image", m);
- waitKey();
- #endif
- }
- return m.clone();
- }
- Mat getTestImage4()
- {
- static Mat m;
- if (m.empty())
- {
- m.create(Size(200, 100), CV_8UC1);
- for (int y = 0; y < m.rows; y++)
- {
- for (int x = 0; x < m.cols; x++)
- {
- float dx = (float)(x - 100);
- float dy = (float)(y - 100);
- float d = sqrtf(dx * dx + dy * dy);
- m.at<uchar>(y, x) = saturate_cast<uchar>(100 + 100 * sin(d / 10 * CV_PI));
- }
- }
- #if 0
- imshow("image", m);
- waitKey();
- #endif
- }
- return m.clone();
- }
- Mat getTestImage5()
- {
- static Mat m;
- if (m.empty())
- {
- m.create(Size(200, 100), CV_8UC1);
- for (int y = 0; y < m.rows; y++)
- {
- for (int x = 0; x < m.cols; x++)
- {
- float dx = (float)(x - 100);
- float dy = (float)(y - 100);
- float d = sqrtf(dx * dx + dy * dy);
- m.at<uchar>(y, x) = saturate_cast<uchar>(x / 2 + 100 * sin(d / 10 * CV_PI));
- }
- }
- #if 0
- imshow("image", m);
- waitKey();
- #endif
- }
- return m.clone();
- }
- void show(const Mat& img, const std::vector<Point> pts)
- {
- if (cvtest::debugLevel >= 10)
- {
- Mat dst = img.clone();
- std::vector< std::vector<Point> > contours;
- contours.push_back(pts);
- polylines(dst, contours, false, Scalar::all(255));
- imshow("dst", dst);
- waitKey();
- }
- }
- TEST(Imgproc_IntelligentScissorsMB, rect)
- {
- segmentation::IntelligentScissorsMB tool;
- tool.applyImage(getTestImage1());
- Point source_point(50, 30);
- tool.buildMap(source_point);
- Point target_point(100, 30);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- tool.applyImage(getTestImage2());
- tool.buildMap(source_point);
- std::vector<Point> pts2;
- tool.getContour(target_point, pts2, true/*backward*/);
- EXPECT_EQ(pts.size(), pts2.size());
- }
- TEST(Imgproc_IntelligentScissorsMB, lines)
- {
- segmentation::IntelligentScissorsMB tool;
- Mat image = getTestImage3();
- tool.applyImage(image);
- Point source_point(30, 50);
- tool.buildMap(source_point);
- Point target_point(150, 50);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- EXPECT_EQ((size_t)121, pts.size());
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, circles)
- {
- segmentation::IntelligentScissorsMB tool;
- tool.setGradientMagnitudeMaxLimit(10);
- Mat image = getTestImage4();
- tool.applyImage(image);
- Point source_point(50, 50);
- tool.buildMap(source_point);
- Point target_point(150, 50);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- EXPECT_EQ((size_t)101, pts.size());
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, circles_gradient)
- {
- segmentation::IntelligentScissorsMB tool;
- Mat image = getTestImage5();
- tool.applyImage(image);
- Point source_point(50, 50);
- tool.buildMap(source_point);
- Point target_point(150, 50);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- EXPECT_EQ((size_t)101, pts.size());
- show(image, pts);
- }
- #define PTS_SIZE_EPS 2
- TEST(Imgproc_IntelligentScissorsMB, grayscale)
- {
- segmentation::IntelligentScissorsMB tool;
- Mat image = getTestImageGray();
- tool.applyImage(image);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 206;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, check_features_grayscale_1_0_0_zerro_crossing_with_limit)
- {
- segmentation::IntelligentScissorsMB tool;
- tool.setEdgeFeatureZeroCrossingParameters(64);
- tool.setWeights(1.0f, 0.0f, 0.0f);
- Mat image = getTestImageGray();
- tool.applyImage(image);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 207;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, check_features_grayscale_1_0_0_canny)
- {
- segmentation::IntelligentScissorsMB tool;
- tool.setEdgeFeatureCannyParameters(50, 100);
- tool.setWeights(1.0f, 0.0f, 0.0f);
- Mat image = getTestImageGray();
- tool.applyImage(image);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 201;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, check_features_grayscale_0_1_0)
- {
- segmentation::IntelligentScissorsMB tool;
- tool.setWeights(0.0f, 1.0f, 0.0f);
- Mat image = getTestImageGray();
- tool.applyImage(image);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 166;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, check_features_grayscale_0_0_1)
- {
- segmentation::IntelligentScissorsMB tool;
- tool.setWeights(0.0f, 0.0f, 1.0f);
- Mat image = getTestImageGray();
- tool.applyImage(image);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 197;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, color)
- {
- segmentation::IntelligentScissorsMB tool;
- Mat image = getTestImageColor();
- tool.applyImage(image);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 205;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, color_canny)
- {
- segmentation::IntelligentScissorsMB tool;
- tool.setEdgeFeatureCannyParameters(32, 100);
- Mat image = getTestImageColor();
- tool.applyImage(image);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 200;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, color_custom_features_invalid)
- {
- segmentation::IntelligentScissorsMB tool;
- ASSERT_ANY_THROW(tool.applyImageFeatures(noArray(), noArray(), noArray()));
- }
- TEST(Imgproc_IntelligentScissorsMB, color_custom_features_edge)
- {
- segmentation::IntelligentScissorsMB tool;
- Mat image = getTestImageColor();
- Mat canny_edges;
- Canny(image, canny_edges, 32, 100, 5);
- Mat binary_edge_feature;
- cv::threshold(canny_edges, binary_edge_feature, 254, 1, THRESH_BINARY_INV);
- tool.applyImageFeatures(binary_edge_feature, noArray(), noArray(), image);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 201;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, color_custom_features_all)
- {
- segmentation::IntelligentScissorsMB tool;
- tool.setWeights(0.9f, 0.0f, 0.1f);
- Mat image = getTestImageColor();
- Mat canny_edges;
- Canny(image, canny_edges, 50, 100, 5);
- Mat binary_edge_feature; // 0, 1 values
- cv::threshold(canny_edges, binary_edge_feature, 254, 1, THRESH_BINARY_INV);
- Mat_<Point2f> gradient_direction(image.size(), Point2f(0, 0)); // normalized
- Mat_<float> gradient_magnitude(image.size(), 0); // cost function
- tool.applyImageFeatures(binary_edge_feature, gradient_direction, gradient_magnitude);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 201;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- TEST(Imgproc_IntelligentScissorsMB, color_custom_features_edge_magnitude)
- {
- segmentation::IntelligentScissorsMB tool;
- tool.setWeights(0.9f, 0.0f, 0.1f);
- Mat image = getTestImageColor();
- Mat canny_edges;
- Canny(image, canny_edges, 50, 100, 5);
- Mat binary_edge_feature; // 0, 1 values
- cv::threshold(canny_edges, binary_edge_feature, 254, 1, THRESH_BINARY_INV);
- Mat_<float> gradient_magnitude(image.size(), 0); // cost function
- tool.applyImageFeatures(binary_edge_feature, noArray(), gradient_magnitude);
- Point source_point(275, 63);
- tool.buildMap(source_point);
- Point target_point(413, 155);
- std::vector<Point> pts;
- tool.getContour(target_point, pts);
- size_t gold = 201;
- EXPECT_GE(pts.size(), gold - PTS_SIZE_EPS);
- EXPECT_LE(pts.size(), gold + PTS_SIZE_EPS);
- show(image, pts);
- }
- }} // namespace
|