-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_processing_toolbox_test.cc
More file actions
105 lines (92 loc) · 3.67 KB
/
image_processing_toolbox_test.cc
File metadata and controls
105 lines (92 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "image_processing_toolbox.h"
#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"
#include "tools/cpp/runfiles/runfiles.h"
#include <filesystem>
#include "opencv2/opencv.hpp"
namespace sfm {
namespace {
using ::absl_testing::IsOk;
using ::absl_testing::IsOkAndHolds;
using ::testing::SizeIs;
using ::bazel::tools::cpp::runfiles::Runfiles;
TEST(SaveFrames, Works) {
const char* tmp_dir = std::getenv("TEST_TMPDIR");
ASSERT_NE(tmp_dir, nullptr) << "TEST_TMPDIR not set";
const std::string output_path = std::filesystem::path(tmp_dir) / "frames";
Runfiles* files = Runfiles::CreateForTest();
ASSERT_THAT(SaveFrames(files->Rlocation("_main/testdata/video_10s.mp4"), 10,
output_path),
IsOk());
int32_t count = 0;
for (const auto& entry : std::filesystem::directory_iterator(output_path)) {
if (entry.is_regular_file()) {
++count;
}
}
EXPECT_EQ(count, 10);
}
TEST(MatchFeatures, TwoValidImages) {
Runfiles* files = Runfiles::CreateForTest();
const cv::Mat image = cv::imread(
files->Rlocation(
"_main/testdata/reconstruction/kleenex/two_frames/frame_1.jpg"));
ASSERT_FALSE(image.empty());
const cv::Mat previous_image = cv::imread(
files->Rlocation(
"_main/testdata/reconstruction/kleenex/two_frames/frame_35.jpg"));
ASSERT_FALSE(previous_image.empty());
cv::Ptr<cv::Feature2D> detector = cv::SIFT::create();
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(
cv::DescriptorMatcher::FLANNBASED);
cv::Mat descriptor_a = ComputeDescriptor(detector, image);
cv::Mat descriptor_b = ComputeDescriptor(detector, previous_image);
auto matches = MatchFeatures(matcher, descriptor_a, descriptor_b);
EXPECT_THAT(matches, IsOkAndHolds(SizeIs(26)));
}
TEST(MatchFeatures, SameImage) {
Runfiles* files = Runfiles::CreateForTest();
const cv::Mat image = cv::imread(
files->Rlocation(
"_main/testdata/reconstruction/kleenex/two_frames/frame_1.jpg"));
ASSERT_FALSE(image.empty());
cv::Ptr<cv::Feature2D> detector = cv::SIFT::create();
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(
cv::DescriptorMatcher::FLANNBASED);
cv::Mat descriptor_a = ComputeDescriptor(detector, image);
auto matches = MatchFeatures(matcher, descriptor_a, descriptor_a);
EXPECT_THAT(matches, IsOkAndHolds(SizeIs(0)));
}
TEST(MatchFeatures, BogusImage) {
Runfiles* files = Runfiles::CreateForTest();
const cv::Mat image = cv::imread(
files->Rlocation(
"_main/testdata/reconstruction/kleenex/two_frames/frame_1.jpg"));
ASSERT_FALSE(image.empty());
const cv::Mat previous_image = cv::imread(
files->Rlocation(
"_main/testdata/reconstruction/frame_0.jpg"));
ASSERT_FALSE(previous_image.empty());
cv::Ptr<cv::Feature2D> detector = cv::SIFT::create();
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(
cv::DescriptorMatcher::FLANNBASED);
cv::Mat descriptor_a = ComputeDescriptor(detector, image);
cv::Mat descriptor_b = ComputeDescriptor(detector, previous_image);
auto matches = MatchFeatures(matcher, descriptor_a, descriptor_b);
// Not even close
EXPECT_THAT(matches, IsOkAndHolds(SizeIs(8)));
}
TEST(SaveRelatedFrames, Works) {
const char* tmp_dir = std::getenv("TEST_TMPDIR");
ASSERT_NE(tmp_dir, nullptr) << "TEST_TMPDIR not set";
const std::string output_path = std::filesystem::path(tmp_dir) / "frames";
Runfiles* files = Runfiles::CreateForTest();
ASSERT_THAT(
SaveRelatedFrames(files->Rlocation("_main/testdata/video_10s.mp4"),
output_path),
IsOk());
}
} // namespace
} // namespace sfm