|
| 1 | +#include "xpano/algorithm/auto_crop.h" |
| 2 | + |
| 3 | +#include <catch2/catch_test_macros.hpp> |
| 4 | +#include <opencv2/core.hpp> |
| 5 | + |
| 6 | +#include "xpano/utils/vec.h" |
| 7 | + |
| 8 | +using xpano::algorithm::crop::FindLargestCrop; |
| 9 | +using xpano::algorithm::crop::kMaskValueOn; |
| 10 | +using xpano::utils::Point2i; |
| 11 | + |
| 12 | +// NOLINTBEGIN(readability-magic-numbers) |
| 13 | + |
| 14 | +TEST_CASE("Auto crop empty mask") { |
| 15 | + cv::Mat mask(10, 20, CV_8U, cv::Scalar(0)); |
| 16 | + auto result = FindLargestCrop(mask); |
| 17 | + CHECK(!result.has_value()); |
| 18 | +} |
| 19 | + |
| 20 | +TEST_CASE("Auto crop full mask / even size") { |
| 21 | + cv::Mat mask(10, 20, CV_8U, cv::Scalar(kMaskValueOn)); |
| 22 | + auto result = FindLargestCrop(mask); |
| 23 | + REQUIRE(result.has_value()); |
| 24 | + CHECK(result->start == Point2i{0, 0}); |
| 25 | + CHECK(result->end == Point2i{19, 9}); |
| 26 | +} |
| 27 | + |
| 28 | +TEST_CASE("Auto crop full mask / odd size") { |
| 29 | + cv::Mat mask(10, 21, CV_8U, cv::Scalar(kMaskValueOn)); |
| 30 | + auto result = FindLargestCrop(mask); |
| 31 | + REQUIRE(result.has_value()); |
| 32 | + CHECK(result->start == Point2i{0, 0}); |
| 33 | + CHECK(result->end == Point2i{20, 9}); |
| 34 | +} |
| 35 | + |
| 36 | +TEST_CASE("Auto crop single column mask") { |
| 37 | + cv::Mat mask(10, 1, CV_8U, cv::Scalar(kMaskValueOn)); |
| 38 | + auto result = FindLargestCrop(mask); |
| 39 | + REQUIRE(result.has_value()); |
| 40 | + CHECK(result->start == Point2i{0, 0}); |
| 41 | + CHECK(result->end == Point2i{0, 9}); |
| 42 | +} |
| 43 | + |
| 44 | +TEST_CASE("Auto crop two columns mask") { |
| 45 | + cv::Mat mask(10, 2, CV_8U, cv::Scalar(kMaskValueOn)); |
| 46 | + auto result = FindLargestCrop(mask); |
| 47 | + REQUIRE(result.has_value()); |
| 48 | + CHECK(result->start == Point2i{0, 0}); |
| 49 | + CHECK(result->end == Point2i{1, 9}); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_CASE("Auto crop single row mask") { |
| 53 | + cv::Mat mask(1, 20, CV_8U, cv::Scalar(kMaskValueOn)); |
| 54 | + auto result = FindLargestCrop(mask); |
| 55 | + REQUIRE(result.has_value()); |
| 56 | + CHECK(result->start == Point2i{0, 0}); |
| 57 | + CHECK(result->end == Point2i{19, 0}); |
| 58 | +} |
| 59 | + |
| 60 | +TEST_CASE("Auto crop two rows mask") { |
| 61 | + cv::Mat mask(2, 20, CV_8U, cv::Scalar(kMaskValueOn)); |
| 62 | + auto result = FindLargestCrop(mask); |
| 63 | + REQUIRE(result.has_value()); |
| 64 | + CHECK(result->start == Point2i{0, 0}); |
| 65 | + CHECK(result->end == Point2i{19, 1}); |
| 66 | +} |
| 67 | + |
| 68 | +TEST_CASE("Auto crop mask with rows set") { |
| 69 | + cv::Mat mask(10, 20, CV_8U, cv::Scalar(0)); |
| 70 | + |
| 71 | + SECTION("single row") { |
| 72 | + mask.row(5) = kMaskValueOn; |
| 73 | + auto result = FindLargestCrop(mask); |
| 74 | + REQUIRE(result.has_value()); |
| 75 | + CHECK(result->start == Point2i{0, 5}); |
| 76 | + CHECK(result->end == Point2i{19, 5}); |
| 77 | + } |
| 78 | + |
| 79 | + SECTION("two rows") { |
| 80 | + mask.row(5) = kMaskValueOn; |
| 81 | + mask.row(6) = kMaskValueOn; |
| 82 | + auto result = FindLargestCrop(mask); |
| 83 | + REQUIRE(result.has_value()); |
| 84 | + CHECK(result->start == Point2i{0, 5}); |
| 85 | + CHECK(result->end == Point2i{19, 6}); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +TEST_CASE("Auto crop mask with empty column") { |
| 90 | + cv::Mat mask(10, 20, CV_8U, cv::Scalar(kMaskValueOn)); |
| 91 | + mask.col(5) = 0; |
| 92 | + auto result = FindLargestCrop(mask); |
| 93 | + REQUIRE(result.has_value()); |
| 94 | + // Algorithm will stop when encountering empty column 5 |
| 95 | + // this is to simplify the implementation |
| 96 | + CHECK(result->start == Point2i{6, 0}); |
| 97 | + CHECK(result->end == Point2i{13, 9}); |
| 98 | +} |
| 99 | + |
| 100 | +TEST_CASE("Auto crop empty matrix") { |
| 101 | + cv::Mat mask; |
| 102 | + auto result = FindLargestCrop(mask); |
| 103 | + REQUIRE(!result.has_value()); |
| 104 | +} |
| 105 | + |
| 106 | +// Example from https://stackoverflow.com/questions/2478447 |
| 107 | +/* |
| 108 | + I |
| 109 | + 1 1 1 1 0 1 |
| 110 | + 1 1 0 1 1 0 |
| 111 | +II->1 1 1 1 1 1 |
| 112 | + 0 1 1 1 1 1 |
| 113 | + 1 1 1 1 1 0 <--IV |
| 114 | + 1 1 0 1 1 1 |
| 115 | + IV |
| 116 | +*/ |
| 117 | +TEST_CASE("Auto crop complex case") { |
| 118 | + cv::Mat mask(6, 6, CV_8U, cv::Scalar(kMaskValueOn)); |
| 119 | + mask.at<unsigned char>(0, 4) = 0; |
| 120 | + mask.at<unsigned char>(1, 2) = 0; |
| 121 | + mask.at<unsigned char>(1, 5) = 0; |
| 122 | + mask.at<unsigned char>(3, 0) = 0; |
| 123 | + mask.at<unsigned char>(4, 5) = 0; |
| 124 | + mask.at<unsigned char>(5, 2) = 0; |
| 125 | + |
| 126 | + auto result = FindLargestCrop(mask); |
| 127 | + REQUIRE(result.has_value()); |
| 128 | + CHECK(result->start == Point2i{1, 2}); |
| 129 | + CHECK(result->end == Point2i{4, 4}); |
| 130 | +} |
| 131 | + |
| 132 | +// NOLINTEND(readability-magic-numbers) |
0 commit comments