Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
temporal-filter.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 #include "types.h"
6 
7 namespace librealsense
8 {
9  const size_t PRESISTENCY_LUT_SIZE = 256;
10 
12  {
13  public:
15 
16  protected:
17  void update_configuration(const rs2::frame& f);
18 
20 
21  template<typename T>
22  void temp_jw_smooth(void* frame_data, void * _last_frame_data, uint8_t *history)
23  {
24  static_assert((std::is_arithmetic<T>::value), "temporal filter assumes numeric types");
25 
26  const bool fp = (std::is_floating_point<T>::value);
27 
28  // In disparity domain 0.001 stands for 5cm at 5 m
29  const T noise = fp ? static_cast<T>(0.001f) : static_cast<T>(3);
30  // For disparity mode the gradient threshold is strictly bounded to avoid visual artefacts
31  T max_radius = static_cast<T>(fp ? _delta_param/250.f : _delta_param);
32 
33  auto frame = reinterpret_cast<T*>(frame_data);
34  auto _last_frame = reinterpret_cast<T*>(_last_frame_data);
35 
36  unsigned char mask = 1 << _cur_frame_index;
37 
38  // pass one -- go through image and update all
39  for (size_t i = 0; i < _current_frm_size_pixels; i++)
40  {
41  T cur_val = frame[i];
42  T prev_val = _last_frame[i];
43 
44  if (cur_val)
45  {
46  if (!prev_val)
47  {
48  _last_frame[i] = cur_val;
49  history[i] = mask;
50  }
51  else
52  { // old and new val
53  T diff = static_cast<T>(fabs(cur_val - prev_val));
54 
55  if (diff > noise && (diff/cur_val) < max_radius)
56  { // old and new val agree
57  history[i] |= mask;
58  float filtered = _alpha_param * cur_val + _one_minus_alpha * prev_val;
59  T result = static_cast<T>(filtered);
60  frame[i] = result;
61  _last_frame[i] = result;
62  }
63  else
64  {
65  _last_frame[i] = cur_val;
66  history[i] = mask;
67  }
68  }
69  }
70  else
71  { // no cur_val
72  if (prev_val)
73  { // only case we can help
74  unsigned char hist = history[i];
75  unsigned char classification = _persistence_map[hist];
76  if (classification & mask)
77  { // we have had enough samples lately
78  frame[i] = prev_val;
79  }
80  }
81  history[i] &= ~mask;
82  }
83  }
84 
85  _cur_frame_index = (_cur_frame_index + 1) % 8; // at end of cycle
86  }
87 
88  private:
89  void on_set_persistence_control(uint8_t val);
90  void on_set_alpha(float val);
91  void on_set_delta(float val);
92 
93  void recalc_persistence_map();
94  std::mutex _mutex;
95  uint8_t _persistence_param;
96 
97  float _alpha_param; // The normalized weight of the current pixel
98  float _one_minus_alpha;
99  uint8_t _delta_param; // A threshold when a filter is invoked
100  size_t _width, _height, _stride;
101  size_t _bpp;
102  rs2_extension _extension_type; // Strictly Depth/Disparity
103  size_t _current_frm_size_pixels;
104  rs2::stream_profile _source_stream_profile;
105  rs2::stream_profile _target_stream_profile;
106  std::vector<uint8_t> _last_frame; // Hold the last frame received for the current profile
107  std::vector<uint8_t> _history; // represents the history over the last 8 frames, 1 bit per frame
108  uint8_t _cur_frame_index;
109  // encodes whether a particular 8 bit history is good enough for all 8 phases of storage
110  std::array<uint8_t, PRESISTENCY_LUT_SIZE> _persistence_map;
111  };
112 }
Definition: rs_frame.hpp:21
Definition: rs_frame.hpp:202
Definition: backend.h:351
Definition: synthetic-stream.h:41
Definition: temporal-filter.h:11
Definition: archive.h:63
Definition: algo.h:16
Definition: rs_processing.hpp:13
void temp_jw_smooth(void *frame_data, void *_last_frame_data, uint8_t *history)
Definition: temporal-filter.h:22
void update_configuration(const rs2::frame &f)
rs2::frame prepare_target_frame(const rs2::frame &f, const rs2::frame_source &source)
rs2_extension
Specifies advanced interfaces (capabilities) objects may implement.
Definition: rs_types.h:93
const size_t PRESISTENCY_LUT_SIZE
Definition: temporal-filter.h:9