其實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);
}
}