farm-ng-core
image_types.h
Go to the documentation of this file.
1 // Copyright (c) 2011, Hauke Strasdat
2 // Copyright (c) 2012, Steven Lovegrove
3 // Copyright (c) 2021, farm-ng, inc.
4 //
5 // Use of this source code is governed by an MIT-style
6 // license that can be found in the LICENSE file or at
7 // https://opensource.org/licenses/MIT.
8 
9 #pragma once
10 
11 #include "sophus/common/enum.h"
12 #include "sophus/image/image.h"
13 
14 namespace sophus {
15 
16 // Pixel type defs:
17 template <class TChannel>
18 using Pixel2 = Eigen::Matrix<TChannel, 2, 1>;
22 
23 template <class TChannel>
24 using Pixel3 = Eigen::Matrix<TChannel, 3, 1>;
28 
29 template <class TChannel>
30 using Pixel4 = Eigen::Matrix<TChannel, 4, 1>;
34 
35 // Image view type defs:
38 
42 
43 static_assert(concepts::ImageView<ImageViewF32>);
44 
48 
49 static_assert(concepts::ImageView<MutImageViewF32>);
50 
51 template <class TChannel>
56 
57 static_assert(concepts::ImageView<ImageView3F32>);
58 
59 template <class TChannel>
64 
65 static_assert(concepts::ImageView<MutImageView3F32>);
66 
67 template <class TChannel>
72 
73 template <class TChannel>
78 
79 // Image type defs:
82 
86 
90 
91 template <class TChannel>
96 
97 template <class TChannel>
102 
103 template <class TChannel>
108 
109 template <class TChannel>
114 
115 /// Number type.
116 
117 template <class TT>
118 struct ImageTraits {
119  static int const kNumChannels = 1;
120  using TPixel = TT;
121  using ChannelT = TPixel;
122  static_assert(
123  std::is_floating_point_v<ChannelT> || std::is_unsigned_v<ChannelT>);
124 };
125 
126 template <class TT, int kNumChannelsT>
127 struct ImageTraits<Eigen::Matrix<TT, kNumChannelsT, 1>> {
128  static int const kNumChannels = kNumChannelsT;
129  using TPixel = Eigen::Matrix<TT, kNumChannels, 1>;
130  using ChannelT = TT;
131  static_assert(
132  std::is_floating_point_v<ChannelT> || std::is_unsigned_v<ChannelT>);
133 };
134 
135 /// Returns boolean image with the result per pixel:
136 ///
137 /// mask(..) = lhs(..) == rhs (..)
138 template <class TPixel>
141  lhs, rhs, [](TPixel lhs, TPixel rhs) { return lhs == rhs; });
142 }
143 
144 /// Returns boolean image with the result per pixel:
145 ///
146 /// mask(..) = lhs(..) < rhs (..)
147 template <class TPixel>
150  lhs, rhs, [](TPixel lhs, TPixel rhs) { return lhs < rhs; });
151 }
152 
153 /// Returns boolean image with the result per pixel:
154 ///
155 /// mask(..) = lhs(..) > rhs (..)
156 template <class TPixel>
158  -> MutImageBool {
160  lhs, rhs, [](TPixel lhs, TPixel rhs) { return lhs > rhs; });
161 }
162 
163 /// Returns boolean image with the result per pixel:
164 ///
165 /// mask(..) = ||lhs(..), rhs (..)|| <= thr
166 template <class TPixel>
168  ImageView<TPixel> lhs,
169  ImageView<TPixel> rhs,
172  lhs, rhs, [thr](TPixel lhs, TPixel rhs) {
173  return MaxMetric(lhs, rhs) <= thr;
174  });
175 }
176 
177 /// Returns number of pixels equal `truth_value` in mask.
178 auto count(ImageViewBool mask, bool truth_value) -> int;
179 
180 /// Returns number of true pixels in mask.
181 auto countTrue(ImageViewBool mask) -> int;
182 
183 /// Returns number of false pixels in mask.
184 auto countFalse(ImageViewBool mask) -> int;
185 
186 /// Returns true if all pixels are true.
187 auto isAllTrue(ImageViewBool mask) -> bool;
188 
189 /// Returns true if at least one pixel is true.
190 auto isAnyTrue(ImageViewBool mask) -> bool;
191 
192 /// Returns boolean image with the result per pixel:
193 ///
194 /// out_mask(..) = !mask(..)
195 [[nodiscard]] auto neg(ImageViewBool mask) -> MutImageBool;
196 
197 /// Returns first pixel of mask which equals `truth_value`, nullopt otherwise.
198 auto firstPixel(ImageViewBool mask, bool truth_value)
199  -> std::optional<Eigen::Vector2i>;
200 
201 /// Returns first true pixel, nullopt otherwise.
202 auto firstTruePixel(ImageViewBool mask) -> std::optional<Eigen::Vector2i>;
203 
204 /// Returns first false pixel, nullopt otherwise.
205 auto firstFalsePixel(ImageViewBool mask) -> std::optional<Eigen::Vector2i>;
206 
207 /// If it is false that `left_image` == `right_image`, print formatted error
208 /// message and then panic.
209 #define SOPHUS_ASSERT_IMAGE_EQ(left_image, right_image, ...) \
210  SOPHUS_ASSERT_EQ( \
211  (left_image).imageSize(), \
212  (right_image).imageSize(), \
213  "Inside: SOPHUS_ASSERT_IMAGE_EQ."); \
214  do { \
215  if (!(left_image).hasSameData(right_image)) { \
216  ::sophus::MutImageBool mask = isEqualMask((left_image), (right_image)); \
217  FARM_IMPL_LOG_HEADER("SOPHUS_ASSERT_IMAGE_EQ failed"); \
218  FARM_IMPL_LOG_PRINTLN( \
219  "Number of pixel failing: {} / {}", \
220  countFalse(mask), \
221  mask.imageSize().area()); \
222  auto maybe_uv = firstFalsePixel(mask); \
223  ::Eigen::Vector2i uv = SOPHUS_UNWRAP(maybe_uv); \
224  int u = uv[0]; \
225  int v = uv[1]; \
226  FARM_IMPL_LOG_PRINTLN( \
227  "First failed pixel: ({},{}).\nLeft:\n{}\nRigth:\n{}", \
228  u, \
229  v, \
230  left_image(u, v), \
231  right_image(u, v)); \
232  FARM_IMPL_LOG_PRINTLN(__VA_ARGS__); \
233  FARM_IMPL_ABORT(); \
234  } \
235  } while (false)
236 
237 } // namespace sophus
Eigen
Definition: params.h:72
sophus::Pixel2U16
Pixel2< uint16_t > Pixel2U16
Definition: image_types.h:20
sophus::ImageTraits< Eigen::Matrix< TT, kNumChannelsT, 1 > >::ChannelT
TT ChannelT
Definition: image_types.h:130
sophus::isGreaterMask
auto isGreaterMask(ImageView< TPixel > lhs, ImageView< TPixel > rhs) -> MutImageBool
Returns boolean image with the result per pixel:
Definition: image_types.h:157
sophus::isAllTrue
auto isAllTrue(ImageViewBool mask) -> bool
Returns true if all pixels are true.
Definition: image_types.cpp:25
sophus::ImageViewBool
ImageView< bool > ImageViewBool
Definition: image_types.h:36
sophus::Image
Image read-only access to pixels and shared ownership, hence cheap to copy. Type is nullable.
Definition: image.h:31
image.h
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::ImageTraits< Eigen::Matrix< TT, kNumChannelsT, 1 > >::TPixel
Eigen::Matrix< TT, kNumChannels, 1 > TPixel
Definition: image_types.h:129
sophus::firstPixel
auto firstPixel(ImageViewBool mask, bool truth_value) -> std::optional< Eigen::Vector2i >
Returns first pixel of mask which equals truth_value, nullopt otherwise.
Definition: image_types.cpp:53
sophus::ImageView
A view of an (immutable) image, which does not own the data.
Definition: image_view.h:55
sophus::Pixel3
Eigen::Matrix< TChannel, 3, 1 > Pixel3
Definition: image_types.h:24
sophus::Pixel2
Eigen::Matrix< TChannel, 2, 1 > Pixel2
Definition: image_types.h:18
sophus::MutImage::makeFromTransform
static auto makeFromTransform(ImageView< TOtherPixel > view, TUnaryOperation const &unary_op) -> MutImage
Creates new MutImage given view and unary transform function.
Definition: mut_image.h:135
sophus::isLessMask
auto isLessMask(ImageView< TPixel > lhs, ImageView< TPixel > rhs) -> MutImageBool
Returns boolean image with the result per pixel:
Definition: image_types.h:148
sophus::isNearMask
auto isNearMask(ImageView< TPixel > lhs, ImageView< TPixel > rhs, typename ImageTraits< TPixel >::ChannelT thr) -> MutImageBool
Returns boolean image with the result per pixel:
Definition: image_types.h:167
sophus::Pixel3U8
Pixel3< uint8_t > Pixel3U8
Definition: image_types.h:25
sophus::Pixel4
Eigen::Matrix< TChannel, 4, 1 > Pixel4
Definition: image_types.h:30
sophus::Pixel3F32
Pixel3< float > Pixel3F32
Definition: image_types.h:27
sophus::ImageTraits::ChannelT
TPixel ChannelT
Definition: image_types.h:121
sophus::isEqualMask
auto isEqualMask(ImageView< TPixel > lhs, ImageView< TPixel > rhs) -> MutImageBool
Returns boolean image with the result per pixel:
Definition: image_types.h:139
sophus::Pixel4F32
Pixel4< float > Pixel4F32
Definition: image_types.h:33
sophus::ImageTraits
Number type.
Definition: image_types.h:118
sophus::MutImage
A image with write access to pixels and exclusive ownership. There is no copy constr / copy assignmen...
Definition: image_view.h:32
sophus::MutImageBool
MutImage< bool > MutImageBool
Definition: image_types.h:81
sophus::neg
auto neg(ImageViewBool mask) -> MutImageBool
Returns boolean image with the result per pixel:
Definition: image_types.cpp:49
sophus::count
auto count(ImageViewBool mask, bool truth_value) -> int
Returns number of pixels equal truth_value in mask.
Definition: image_types.cpp:13
sophus::Pixel2F32
Pixel2< float > Pixel2F32
Definition: image_types.h:21
sophus::firstTruePixel
auto firstTruePixel(ImageViewBool mask) -> std::optional< Eigen::Vector2i >
Returns first true pixel, nullopt otherwise.
Definition: image_types.cpp:66
sophus::isAnyTrue
auto isAnyTrue(ImageViewBool mask) -> bool
Returns true if at least one pixel is true.
Definition: image_types.cpp:37
sophus::ImageTraits::TPixel
TT TPixel
Definition: image_types.h:120
sophus::countTrue
auto countTrue(ImageViewBool mask) -> int
Returns number of true pixels in mask.
Definition: image_types.cpp:21
sophus::Pixel2U8
Pixel2< uint8_t > Pixel2U8
Definition: image_types.h:19
sophus::countFalse
auto countFalse(ImageViewBool mask) -> int
Returns number of false pixels in mask.
Definition: image_types.cpp:23
sophus::MutImageView
View of a mutable image, which does not own the data.
Definition: mut_image_view.h:61
sophus::Pixel3U16
Pixel3< uint16_t > Pixel3U16
Definition: image_types.h:26
sophus::Pixel4U8
Pixel4< uint8_t > Pixel4U8
Definition: image_types.h:31
sophus::firstFalsePixel
auto firstFalsePixel(ImageViewBool mask) -> std::optional< Eigen::Vector2i >
Returns first false pixel, nullopt otherwise.
Definition: image_types.cpp:70
enum.h
sophus::ImageTraits::kNumChannels
static const int kNumChannels
Definition: image_types.h:119
sophus::Pixel4U16
Pixel4< uint16_t > Pixel4U16
Definition: image_types.h:32