farm-ng-core
interpolation.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/image/image.h"
13 
14 namespace sophus {
15 
16 // Bilinearly interpolates ``img`` at ``uv``.
17 //
18 // Preconditions:
19 // - uv must be inside the image boundary
20 template <class TT>
21 auto interpolate(sophus::ImageView<TT> const& image, Eigen::Vector2f uv) -> TT {
22  sophus::ImageSize image_size = image.imageSize();
24  image_size.contains(uv), "{} vs {}", image_size, uv.transpose());
25  float iu = NAN;
26  float iv = NAN;
27  float frac_u = std::modf(uv.x(), &iu);
28  float frac_v = std::modf(uv.y(), &iv);
29  int u = iu;
30  int v = iv;
31 
32  bool u_corner_case = u == image_size.width - 1;
33  bool v_corner_case = v == image_size.height - 1;
34 
35  TT val00 = image(u, v);
36  TT val01 = v_corner_case ? val00 : image(u, v + 1);
37  TT val10 = u_corner_case ? val00 : image(u + 1, v);
38  TT val11 = u_corner_case || v_corner_case ? val00 : image(u + 1, v + 1);
39 
40  TT val = (1.f - frac_u) * (1.f - frac_v) * val00 //
41  + (1.f - frac_u) * frac_v * val01 //
42  + frac_u * (1.f - frac_v) * val10 //
43  + frac_u * frac_v * val11;
44  return val;
45 }
46 
47 } // namespace sophus
SOPHUS_ASSERT
#define SOPHUS_ASSERT(...)
Definition: common.h:40
image.h
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::ImageView
A view of an (immutable) image, which does not own the data.
Definition: image_view.h:55
sophus::interpolate
auto interpolate(sophus::ImageView< TT > const &image, Eigen::Vector2f uv) -> TT
Definition: interpolation.h:21
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
image_view.h
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