其实realsense的彩色图与深度图对齐非常简单,因为当你开始采集彩色图与深度图流时,会自动产生一个对齐后的图像流,一个是彩色对齐深度:color_aligned_to_depth,一个是深度对齐彩色:depth_aligned_to_color,表现出来就是一个是显示深度图里具有深度部分才有的图像,一个刚好反过来,所以一个是RGB一个是16UC1,所以接上一篇,我们可以将他们显示出来。不过说看起来彩色图和深度图还是有误差的,所以如果要求准确的话,还是最好自己标定一下然后再自行对齐比较好。
#include <librealsense/rs.hpp>
#include "example.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <thread>
using namespace cv;
using namespace std;
struct rgb_pixel
{
uint8_t r,g,b;
};
int main()
{
rs::log_to_console(rs::log_severity::warn);
rs::context ctx;
if(ctx.get_device_count() == 0) throw std::runtime_error("No device detected. Is it plugged in?");
rs::device & dev = *ctx.get_device(0);
dev.enable_stream(rs::stream::color, 640, 480, rs::format::rgb8, 60);
dev.enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 60);
dev.start();
while(1)
{
dev.wait_for_frames();
uchar* pRgb = (uchar*)dev.get_frame_data(rs::stream::color);
uint16_t *depthImage = (uint16_t *) dev.get_frame_data(rs::stream::depth);
uchar* pCad = (uchar*)dev.get_frame_data(rs::stream::color_aligned_to_depth);
Mat rgb_show;
Mat rgb(480, 640, CV_8UC3, pRgb);
cvtColor(rgb, rgb_show, CV_BGR2RGB);
imshow("RGBImage",rgb_show);
Mat depth(480,640,CV_16UC1, depthImage);
imshow("Depthimage",depth*15);
Mat cad_show;
Mat cad(480, 640, CV_8UC3, pCad);
cvtColor(cad, cad_show, CV_BGR2RGB);
imshow("CADImage",cad_show);
waitKey(10);
}
}