farm-ng-core
image_size.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/calculus/region.h"
12 #include "sophus/common/common.h"
13 
14 #include <Eigen/Dense>
15 
16 #include <iostream>
17 
18 namespace sophus {
19 
20 /// Image size, hence its width and height.
21 struct ImageSize {
22  ImageSize() = default;
24 
25  static auto from(Eigen::Array2<int> const& arr) -> ImageSize {
26  return {arr[0], arr[1]};
27  }
28 
29  [[nodiscard]] auto area() const -> size_t { return width * height; }
30 
31  /// Returns true if obs is within image.
32  ///
33  /// Positive border makes the image frame smaller.
34  [[nodiscard]] auto contains(Eigen::Vector2i const& obs, int border = 0) const
35  -> bool;
36  [[nodiscard]] auto contains(
37  Eigen::Vector2f const& obs, float border = 0.f) const -> bool;
38  [[nodiscard]] auto contains(
39  Eigen::Vector2d const& obs, double border = 0.0) const -> bool;
40 
41  [[nodiscard]] auto isEmpty() const -> bool {
42  return width == 0 && height == 0;
43  }
44 
45  [[nodiscard]] auto array() const -> Eigen::Array2<int> {
46  return Eigen::Array2<int>(width, height);
47  }
48 
49  [[nodiscard]] auto iwidth() const -> int { return this->width; }
50  [[nodiscard]] auto iheight() const -> int { return this->height; }
51 
52  /// Horizontal width of images, i.e. number of columns.
53  int width = 0;
54 
55  /// Vertical height of images, i.e. number of rows.
56  int height = 0;
57 };
58 
59 // TODO: make member function?
60 template <class TPixel>
61 inline auto imageCoordsInterval(ImageSize image_size, int border = 0)
62  -> Region2I {
63  // e.g. 10x10 image has valid values [0, ..., 9] in both dimensions
64  // a border of 2 would make valid range [2, ..., 7]
65  return Region2I::fromMinMax(
66  Eigen::Vector2i(border, border),
67  Eigen::Vector2i(
68  image_size.width - border - 1, image_size.height - border - 1));
69 }
70 
71 /// Equality operator.
72 auto operator==(ImageSize const& lhs, ImageSize const& rhs) -> bool;
73 
74 auto operator!=(ImageSize const& lhs, ImageSize const& rhs) -> bool;
75 
76 /// If the original width [height] is odd, the new width [height] will be:
77 /// (width+1)/2 [height+1)/2].
78 auto half(ImageSize size) -> ImageSize;
79 
80 /// Ordering operator, for keys in sets and maps
81 auto operator<(ImageSize const& lhs, ImageSize const& rhs) -> bool;
82 
83 /// Ostream operator.
84 auto operator<<(std::ostream& os, ImageSize const& image_size) -> std::ostream&;
85 
86 } // namespace sophus
Eigen
Definition: params.h:72
sophus::half
auto half(ImageSize image_size) -> ImageSize
If the original width [height] is odd, the new width [height] will be: (width+1)/2 [height+1)/2].
Definition: image_size.cpp:29
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::operator<
auto operator<(ImageSize const &lhs, ImageSize const &rhs) -> bool
Ordering operator, for keys in sets and maps.
Definition: image_size.cpp:41
sophus::ImageSize::from
static auto from(Eigen::Array2< int > const &arr) -> ImageSize
Definition: image_size.h:25
region.h
sophus::Region::fromMinMax
static auto fromMinMax(TPoint const &min, TPoint const &max) noexcept -> Region< TPoint >
Creates Region from two points, min and max.
Definition: region.h:104
sophus::ImageSize::array
auto array() const -> Eigen::Array2< int >
Definition: image_size.h:45
sophus::ImageSize::iwidth
auto iwidth() const -> int
Definition: image_size.h:49
sophus::operator!=
auto operator!=(ImageSize const &lhs, ImageSize const &rhs) -> bool
Definition: image_size.cpp:37
sophus::ImageSize::isEmpty
auto isEmpty() const -> bool
Definition: image_size.h:41
sophus::imageCoordsInterval
auto imageCoordsInterval(ImageSize image_size, int border=0) -> Region2I
Definition: image_size.h:61
sophus::operator==
auto operator==(Region< TT > const &lhs, Region< TT > const &rhs) -> bool
Definition: region.h:367
common.h
sophus::ImageSize::ImageSize
ImageSize(int width, int height)
Definition: image_size.h:23
sophus::ImageSize::height
int height
Vertical height of images, i.e. number of rows.
Definition: image_size.h:56
sophus::ImageSize::width
int width
Horizontal width of images, i.e. number of columns.
Definition: image_size.h:53
sophus::ImageSize::iheight
auto iheight() const -> int
Definition: image_size.h:50
sophus::ImageSize::area
auto area() const -> size_t
Definition: image_size.h:29
sophus::operator<<
auto operator<<(std::ostream &os, ImageSize const &image_size) -> std::ostream &
Ostream operator.
Definition: image_size.cpp:46
sophus::ImageSize::ImageSize
ImageSize()=default
sophus::ImageSize
Image size, hence its width and height.
Definition: image_size.h:21
sophus::ImageSize::contains
auto contains(Eigen::Vector2i const &obs, int border=0) const -> bool
Returns true if obs is within image.
Definition: image_size.cpp:13
sophus::Region
A region is a closed interval [a, b] with a being the lower bound (=min) and b being the upper bound ...
Definition: region.h:19