rs-align.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 
4 #include <librealsense2/rs.hpp>
5 #include "example-imgui.hpp"
6 
7 /*
8  This example introduces the concept of spatial stream alignment.
9  For example usecase of alignment, please check out align-advanced and measure demos.
10  The need for spatial alignment (from here "align") arises from the fact
11  that not all camera streams are captured from a single viewport.
12  Align process lets the user translate images from one viewport to another.
13  That said, results of align are synthetic streams, and suffer from several artifacts:
14  1. Sampling - mapping stream to a different viewport will modify the resolution of the frame
15  to match the resolution of target viewport. This will either cause downsampling or
16  upsampling via interpolation. The interpolation used needs to be of type
17  Nearest Neighbor to avoid introducing non-existing values.
18  2. Occlussion - Some pixels in the resulting image correspond to 3D coordinates that the original
19  sensor did not see, because these 3D points were occluded in the original viewport.
20  Such pixels may hold invalid texture values.
21 */
22 
23 // This example assumes camera with depth and color
24 // streams, and direction lets you define the target stream
25 enum class direction
26 {
27  to_depth,
28  to_color
29 };
30 
31 // Forward definition of UI rendering, implemented below
33 
34 int main(int argc, char * argv[]) try
35 {
36  // Create and initialize GUI related objects
37  window app(1280, 720, "RealSense Align Example"); // Simple window handling
38  ImGui_ImplGlfw_Init(app, false); // ImGui library intializition
39  rs2::colorizer c; // Helper to colorize depth images
40  texture depth_image, color_image; // Helpers for renderig images
41 
42  // Create a pipeline to easily configure and start the camera
47  pipe.start(cfg);
48 
49  // Define two align objects. One will be used to align
50  // to depth viewport and the other to color.
51  // Creating align object is an expensive operation
52  // that should not be performed in the main loop
53  rs2::align align_to_depth(RS2_STREAM_DEPTH);
54  rs2::align align_to_color(RS2_STREAM_COLOR);
55 
56  float alpha = 0.5f; // Transparancy coefficient
57  direction dir = direction::to_depth; // Alignment direction
58 
59  while (app) // Application still alive?
60  {
61  // Using the align object, we block the application until a frameset is available
62  rs2::frameset frameset = pipe.wait_for_frames();
63 
64  if (dir == direction::to_depth)
65  {
66  // Align all frames to depth viewport
67  frameset = align_to_depth.process(frameset);
68  }
69  else
70  {
71  // Align all frames to color viewport
72  frameset = align_to_color.process(frameset);
73  }
74 
75  // With the aligned frameset we proceed as usual
76  auto depth = frameset.get_depth_frame();
77  auto color = frameset.get_color_frame();
78  auto colorized_depth = c.colorize(depth);
79 
81  // Use the Alpha channel for blending
83 
84  if (dir == direction::to_depth)
85  {
86  // When aligning to depth, first render depth image
87  // and then overlay color on top with transparancy
88  depth_image.render(colorized_depth, { 0, 0, app.width(), app.height() });
89  color_image.render(color, { 0, 0, app.width(), app.height() }, alpha);
90  }
91  else
92  {
93  // When aligning to color, first render color image
94  // and then overlay depth image on top
95  color_image.render(color, { 0, 0, app.width(), app.height() });
96  depth_image.render(colorized_depth, { 0, 0, app.width(), app.height() }, 1 - alpha);
97  }
98 
99  glColor4f(1.f, 1.f, 1.f, 1.f);
101 
102  // Render the UI:
104  render_slider({ 15.f, app.height() - 60, app.width() - 30, app.height() }, &alpha, &dir);
105  ImGui::Render();
106  }
107 
108  return EXIT_SUCCESS;
109 }
110 catch (const rs2::error & e)
111 {
112  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
113  return EXIT_FAILURE;
114 }
115 catch (const std::exception & e)
116 {
117  std::cerr << e.what() << std::endl;
118  return EXIT_FAILURE;
119 }
120 
121 void render_slider(rect location, float* alpha, direction* dir)
122 {
123  static const int flags = ImGuiWindowFlags_NoCollapse
129 
130  ImGui::SetNextWindowPos({ location.x, location.y });
131  ImGui::SetNextWindowSize({ location.w, location.h });
132 
133  // Render transparency slider:
134  ImGui::Begin("slider", nullptr, flags);
136  ImGui::SliderFloat("##Slider", alpha, 0.f, 1.f);
138  if (ImGui::IsItemHovered())
139  ImGui::SetTooltip("Texture Transparancy: %.3f", *alpha);
140 
141  // Render direction checkboxes:
142  bool to_depth = (*dir == direction::to_depth);
143  bool to_color = (*dir == direction::to_color);
144 
145  if (ImGui::Checkbox("Align To Depth", &to_depth))
146  {
147  *dir = to_depth ? direction::to_depth : direction::to_color;
148  }
149  ImGui::SameLine();
150  ImGui::SetCursorPosX(location.w - 140);
151  if (ImGui::Checkbox("Align To Color", &to_color))
152  {
153  *dir = to_color ? direction::to_color : direction::to_depth;
154  }
155 
156  ImGui::End();
157 }
frameset wait_for_frames(unsigned int timeout_ms=RS2_DEFAULT_TIMEOUT) const
float h
Definition: example.hpp:73
void render(const rs2::frame &frame, const rect &rect, float alpha=1.f)
Definition: example.hpp:471
IMGUI_API void SetTooltip(const char *fmt,...) IM_PRINTFARGS(1)
Definition: imgui.cpp:3288
GLuint color
GLint location
video_frame colorize(frame depth) const
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiSetCond cond=0)
Definition: imgui.cpp:4923
GLint GLint GLsizei GLsizei GLsizei depth
#define GL_BLEND
#define glColor4f
#define GL_SRC_ALPHA
e
Definition: rmse.py:177
#define glEnable
direction
Definition: rs-align.cpp:25
The texture class.
Definition: example.hpp:402
depth_frame get_depth_frame() const
Definition: rs_frame.hpp:1006
video_frame get_color_frame() const
Definition: rs_frame.hpp:1015
const std::string & get_failed_args() const
Definition: rs_types.hpp:117
IMGUI_API void SameLine(float pos_x=0.0f, float spacing_w=-1.0f)
Definition: imgui.cpp:9246
GLdouble f
GLfloat GLfloat GLfloat alpha
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:3772
float w
Definition: example.hpp:73
const GLubyte * c
Definition: glext.h:12690
frameset process(frameset frames)
float width() const
Definition: example.hpp:627
bool ImGui_ImplGlfw_Init(GLFWwindow *window, bool install_callbacks)
int main(int argc, char *argv[])
Definition: rs-align.cpp:34
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiSetCond cond=0)
Definition: imgui.cpp:4937
GLbitfield flags
IMGUI_API void SetCursorPosX(float x)
Definition: imgui.cpp:5101
IMGUI_API void PushItemWidth(float item_width)
Definition: imgui.cpp:4486
IMGUI_API void End()
Definition: imgui.cpp:4330
void enable_stream(rs2_stream stream_type, int stream_index, int width, int height, rs2_format format=RS2_FORMAT_ANY, int framerate=0)
#define GL_ONE_MINUS_SRC_ALPHA
float height() const
Definition: example.hpp:628
Definition: example.hpp:70
void ImGui_ImplGlfw_NewFrame(float scale_factor)
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *display_format="%.3f", float power=1.0f, bool render_bg=false)
Definition: imgui.cpp:6570
IMGUI_API void PopItemWidth()
Definition: imgui.cpp:4507
IMGUI_API bool Checkbox(const char *label, bool *v)
Definition: imgui.cpp:7269
std::ostream & cerr()
pipeline_profile start()
#define glBlendFunc
float x
Definition: example.hpp:72
IMGUI_API bool IsItemHovered()
Definition: imgui.cpp:3200
const std::string & get_failed_function() const
Definition: rs_types.hpp:112
void render_slider(rect location, float *alpha, direction *dir)
Definition: rs-align.cpp:121
IMGUI_API void Render()
Definition: imgui.cpp:2619
float y
Definition: example.hpp:72
#define glDisable


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40