CARLA
 
载入中...
搜索中...
未找到
test_image.cpp
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "test.h"
8
10#include <carla/image/ImageIO.h>
12
13#include <memory>
14
15template <typename ViewT, typename PixelT>
16struct TestImage {
17 TestImage(TestImage &&) = default;
18 using pixel_type = PixelT;
19 std::unique_ptr<PixelT[]> data;
20 ViewT view;
21};
22
23template <typename PixelT>
24static auto MakeTestImage(size_t width, size_t height) {
25 auto data = std::make_unique<PixelT[]>(sizeof(PixelT) * width * height);
26 auto view = boost::gil::interleaved_view(
27 width,
28 height,
29 reinterpret_cast<PixelT*>(data.get()),
30 static_cast<long>(sizeof(PixelT) * width));
31 return TestImage<decltype(view), PixelT>{std::move(data), view};
32}
33
34#ifndef PLATFORM_WINDOWS
35TEST(image, support) {
36 using namespace carla::image::io;
37 carla::logging::log("PNG support =", has_png_support());
38 carla::logging::log("JPEG support =", has_jpeg_support());
39 carla::logging::log("TIFF support =", has_tiff_support());
40}
41#endif // PLATFORM_WINDOWS
42
43TEST(image, depth) {
44#ifndef NDEBUG
45 carla::log_info("This test only happens in release (too slow).");
46#else
47 using namespace boost::gil;
48 using namespace carla::image;
49
50 constexpr auto width = 256 * 256 * 256;
51 constexpr auto height = 1u;
52
53 auto img_bgra8 = MakeTestImage<bgra8_pixel_t>(width, height);
54 auto img_gray8 = MakeTestImage<gray8_pixel_t>(width, height);
55
56 auto depth_view = ImageView::MakeColorConvertedView(
57 img_bgra8.view,
59 auto ldepth_view = ImageView::MakeColorConvertedView(
60 img_bgra8.view,
62
63 auto p = *depth_view.begin();
64 static_assert(std::is_same<decltype(p), gray8_pixel_t>::value, "Not the pixel I was looking for!");
65 auto lp = *ldepth_view.begin();
66 static_assert(std::is_same<decltype(lp), gray8_pixel_t>::value, "Not the pixel I was looking for!");
67
68 ASSERT_EQ(depth_view.size(), img_bgra8.view.size());
69 ASSERT_EQ(ldepth_view.size(), img_bgra8.view.size());
70
71 {
72 auto it_bgra8 = img_bgra8.view.begin();
73 auto it_gray8 = img_gray8.view.begin();
74
75 for (auto r = 0u; r < 256u; ++r) {
76 for (auto g = 0u; g < 256u; ++g) {
77 for (auto b = 0u; b < 256u; ++b) {
78 decltype(img_bgra8)::pixel_type &p_bgra8 = *it_bgra8;
79 decltype(img_gray8)::pixel_type &p_gray8 = *it_gray8;
80 get_color(p_bgra8, red_t()) = static_cast<uint8_t>(r);
81 get_color(p_bgra8, green_t()) = static_cast<uint8_t>(g);
82 get_color(p_bgra8, blue_t()) = static_cast<uint8_t>(b);
83 const float depth = r + (g * 256) + (b * 256 * 256);
84 const float normalized = depth / static_cast<float>(256 * 256 * 256 - 1);
85 p_gray8[0] = static_cast<uint8_t>(255.0 * normalized);
86
87 ++it_bgra8;
88 ++it_gray8;
89 }
90 }
91 }
92 }
93
94 auto img_copy = MakeTestImage<bgra8_pixel_t>(width, height);
95 ImageConverter::CopyPixels(img_bgra8.view, img_copy.view);
96 ImageConverter::ConvertInPlace(img_copy.view, ColorConverter::LogarithmicDepth());
97
98 {
99 auto it_gray8 = img_gray8.view.begin();
100 auto it_depth = depth_view.begin();
101 auto it_ldepth = ldepth_view.begin();
102 auto it_copy = img_copy.view.begin();
103
104 for (auto i = 0u; i < width; ++i) {
105 auto p_gray8 = *it_gray8;
106 auto p_depth = *it_depth;
107 auto p_ldepth = *it_ldepth;
108 auto p_copy = *it_copy;
109 ASSERT_NEAR(int(p_depth[0]), int(p_gray8[0]), 1)
110 << "at XY(" << i << ",0)";
111 decltype(p_copy) ld;
112 color_convert(p_ldepth, ld);
113 ASSERT_EQ(ld, p_copy)
114 << "at XY(" << i << ",0)";
115 ++it_gray8;
116 ++it_depth;
117 ++it_ldepth;
118 ++it_copy;
119 }
120 }
121#endif // NDEBUG
122}
123
124TEST(image, semantic_segmentation) {
125 using namespace boost::gil;
126 using namespace carla::image;
127
128 constexpr auto width = CityScapesPalette::GetNumberOfTags();
129 constexpr auto height = 1u;
130
131 auto img_bgra8 = MakeTestImage<bgra8_pixel_t>(width, height);
132 auto img_ss = MakeTestImage<rgb8_pixel_t>(width, height);
133
134 auto semseg_view = ImageView::MakeColorConvertedView(
135 img_bgra8.view,
137
138 auto p = *semseg_view.begin();
139 static_assert(std::is_same<decltype(p), bgra8_pixel_t>::value, "Not the pixel I was looking for!");
140
141 ASSERT_EQ(semseg_view.size(), img_bgra8.view.size());
142
143 {
144 auto it_bgra8 = img_bgra8.view.begin();
145 auto it_ss = img_ss.view.begin();
146
147 for (auto tag = 0u; tag < width; ++tag) {
148 decltype(img_bgra8)::pixel_type &p_bgra8 = *it_bgra8;
149 get_color(p_bgra8, red_t()) = static_cast<uint8_t>(tag);
150 get_color(p_bgra8, green_t()) = 0u;
151 get_color(p_bgra8, blue_t()) = 0u;
152 decltype(img_ss)::pixel_type &p_ss = *it_ss;
153 auto color = CityScapesPalette::GetColor(static_cast<uint8_t>(tag));
154 get_color(p_ss, red_t()) = color[0u];
155 get_color(p_ss, green_t()) = color[1u];
156 get_color(p_ss, blue_t()) = color[2u];
157 ++it_bgra8;
158 ++it_ss;
159 }
160 }
161
162 auto img_copy = MakeTestImage<rgba8_pixel_t>(width, height);
163 ImageConverter::CopyPixels(img_bgra8.view, img_copy.view);
164 ImageConverter::ConvertInPlace(img_copy.view, ColorConverter::CityScapesPalette());
165
166 {
167 auto it_ss = img_ss.view.begin();
168 auto it_ssv = semseg_view.begin();
169 auto it_copy = img_copy.view.begin();
170
171 for (auto i = 0u; i < width; ++i) {
172 auto p_ssv = *it_ssv;
173 auto p_copy = *it_copy;
174 auto _p_ss = *it_ss;
175 decltype(p_ssv) p_ss;
176 color_convert(_p_ss, p_ss);
177 ASSERT_EQ(p_ssv, p_ss)
178 << "at XY(" << i << ",0)";
179 decltype(p_copy) css;
180 color_convert(p_ssv, css);
181 ASSERT_EQ(p_ssv, p_copy)
182 << "at XY(" << i << ",0)";
183 ++it_ss;
184 ++it_ssv;
185 ++it_copy;
186 }
187 }
188}
static void log(Args &&... args)
Definition Logging.h:59
static void log_info(Args &&... args)
Definition Logging.h:82
std::unique_ptr< PixelT[]> data
PixelT pixel_type
TestImage(TestImage &&)=default
TEST(image, support)
static auto MakeTestImage(size_t width, size_t height)