blob: 96a2a224850fdfc7579a4ca7193a7daec09681eb [file] [log] [blame]
Brian Silverman5eec8b92019-03-10 15:14:31 -07001#include "aos/vision/blob/threshold.h"
2
3#include <vector>
4
5#include "aos/vision/blob/range_image.h"
6#include "aos/vision/image/image_types.h"
7#include "gmock/gmock.h"
8#include "gtest/gtest.h"
9
10namespace aos {
11namespace vision {
12namespace testing {
13
14class YuyvYThresholdTest : public ::testing::Test {
15};
16
17// Verifies that a simple image is thresholded correctly.
18//
19// Specifically, we want to get this result from the thresholding:
20// --+--
21// +---+
22// -+++-
23// +++-+
24// -----
25// ++-++
26// +++++
27// +-+-+
28TEST_F(YuyvYThresholdTest, SimpleImage) {
29 ImageFormat format;
30 format.w = 5;
31 format.h = 8;
32
33 std::vector<std::vector<ImageRange>> expected_ranges;
34 std::vector<char> image;
35 image.resize(5 * 8 * 2);
36 // --+--
37 image[0 * 2 + 0 * 10] = 0;
38 image[1 * 2 + 0 * 10] = 0;
39 image[2 * 2 + 0 * 10] = 128;
40 image[3 * 2 + 0 * 10] = 127;
41 image[4 * 2 + 0 * 10] = 0;
42 expected_ranges.push_back({{{2, 3}}});
43 // +---+
44 image[0 * 2 + 1 * 10] = 128;
45 image[1 * 2 + 1 * 10] = 0;
46 image[2 * 2 + 1 * 10] = 0;
47 image[3 * 2 + 1 * 10] = 10;
48 image[4 * 2 + 1 * 10] = 255;
49 expected_ranges.push_back({{{0, 1}, {4, 5}}});
50 // -+++-
51 image[0 * 2 + 2 * 10] = 73;
52 image[1 * 2 + 2 * 10] = 250;
53 image[2 * 2 + 2 * 10] = 251;
54 image[3 * 2 + 2 * 10] = 252;
55 image[4 * 2 + 2 * 10] = 45;
56 expected_ranges.push_back({{{1, 4}}});
57 // +++-+
58 image[0 * 2 + 3 * 10] = 128;
59 image[1 * 2 + 3 * 10] = 134;
60 image[2 * 2 + 3 * 10] = 250;
61 image[3 * 2 + 3 * 10] = 0;
62 image[4 * 2 + 3 * 10] = 230;
63 expected_ranges.push_back({{{0, 3}, {4, 5}}});
64 // -----
65 image[0 * 2 + 4 * 10] = 7;
66 image[1 * 2 + 4 * 10] = 120;
67 image[2 * 2 + 4 * 10] = 127;
68 image[3 * 2 + 4 * 10] = 0;
69 image[4 * 2 + 4 * 10] = 50;
70 expected_ranges.push_back({{}});
71 // ++-++
72 image[0 * 2 + 5 * 10] = 140;
73 image[1 * 2 + 5 * 10] = 140;
74 image[2 * 2 + 5 * 10] = 0;
75 image[3 * 2 + 5 * 10] = 140;
76 image[4 * 2 + 5 * 10] = 140;
77 expected_ranges.push_back({{{0, 2}, {3, 5}}});
78 // +++++
79 image[0 * 2 + 6 * 10] = 128;
80 image[1 * 2 + 6 * 10] = 128;
81 image[2 * 2 + 6 * 10] = 128;
82 image[3 * 2 + 6 * 10] = 128;
83 image[4 * 2 + 6 * 10] = 128;
84 expected_ranges.push_back({{{0, 5}}});
85 // +-+-+
86 image[0 * 2 + 7 * 10] = 200;
87 image[1 * 2 + 7 * 10] = 0;
88 image[2 * 2 + 7 * 10] = 200;
89 image[3 * 2 + 7 * 10] = 0;
90 image[4 * 2 + 7 * 10] = 200;
91 expected_ranges.push_back({{{0, 1}, {2, 3}, {4, 5}}});
92 const RangeImage expected_result(0, std::move(expected_ranges));
93
94 const auto slow_result = SlowYuyvYThreshold(format, image.data(), 127);
95 ASSERT_EQ(expected_result, slow_result);
96}
97
98} // namespace testing
99} // namespace vision
100} // namespace aos