RGB-D相机,对比传统的RGB相机,我们很快就能找到不同之处---深度。那么有了深度信息,我们能够做什么呢?
在做背景分割的时候,RGB相机只能通过帧间差分、特定颜色提取、基于混合高斯模型去除背景等方法,做到前景背景分离,有了RGB-D相机的深度信息,那我们就可以通过Z方向的距离来剔除背景了。先看看vs2015中跑的效果图:
下面三张图分别表示只显示距离相机2米以内的图像、3米以内、6米以内的图像:
这样我们就能很好的进行背景分割了,不过只是实现了功能,后续还需要再优化,下面是核心代码,有详尽的注释,看懂应该是很容易的。
main.cpp
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
#include <librealsense2/rs.hpp>
#include "../example.hpp"
#include <imgui.h>
#include "imgui_impl_glfw.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
void render_slider(rect location, float& clipping_dist);
void remove_background(rs2::video_frame& other, const rs2::depth_frame& depth_frame, float depth_scale, float clipping_dist);
float get_depth_scale(rs2::device dev);
rs2_stream find_stream_to_align(const std::vector<rs2::stream_profile>& streams);
bool profile_changed(const std::vector<rs2::stream_profile>& current, const std::vector<rs2::stream_profile>& prev);
int main(int argc, char * argv[]) try
{
// Create and initialize GUI related objects
window app(1280, 720, "CPP - Align Example"); // Simple window handling
ImGui_ImplGlfw_Init(app, false); // ImGui library intializition
rs2::colorizer c; // Helper to colorize depth images
texture renderer; // Helper for renderig images
// Create a pipeline to easily configure and start the camera
rs2::pipeline pipe;
//Calling pipeline's start() without any additional parameters will start the first device
// with its default streams.
//The start function returns the pipeline profile which the pipeline used to start the device
rs2::pipeline_profile profile