重写kinect2_viewer,编译高博kinect2在orbslam2上跑的程序(解决cmakefile中库依赖和头文件的问题)
该方法详述了高博kinect2_viewer的编译过程
//.................................................................................
//單獨編譯kinect2_viewer,實現KInectv2 ORBSLAM2運行
//viewer.cpp修改參考“http://www.cnblogs.com/gaoxiang12/p/5161223.html”
//kinect2_viewer編譯參考“http://www.cnblogs.com/bigzhao/p/6278993.html”
1.修改viewer.cpp參考“http://www.cnblogs.com/gaoxiang12/p/5161223.html”
注意:
viewer.cpp中将原来的#include "orbslam2/System.h"改为现在的#include "System.h"
這兩行也對應修改
string orbVocFile = "/home/tommy/catkin_ws/src/tommy/config/ORBvoc.txt";
string orbSetiingsFile = "/home/tommy/catkin_ws/src/tommy/config/kinect2_qhd.yaml";
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <mutex>
#include <thread>
#include <chrono> #include <ros/ros.h>
#include <ros/spinner.h>
#include <sensor_msgs/CameraInfo.h>
#include <sensor_msgs/Image.h> #include <cv_bridge/cv_bridge.h> #include <image_transport/image_transport.h>
#include <image_transport/subscriber_filter.h> #include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/exact_time.h>
#include <message_filters/sync_policies/approximate_time.h> #include <kinect2_bridge/kinect2_definitions.h> #include "System.h" class Receiver
{
public:
enum Mode
{
IMAGE = 0,
CLOUD,
BOTH
}; private:
std::mutex lock; const std::string topicColor, topicDepth;
const bool useExact, useCompressed; bool updateImage, updateCloud;
bool save;
bool running;
size_t frame;
const size_t queueSize; cv::Mat color, depth;
cv::Mat cameraMatrixColor, cameraMatrixDepth;
cv::Mat lookupX, lookupY; typedef message_filters::sync_policies::ExactTime<sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::CameraInfo> ExactSyncPolicy;
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::CameraInfo> ApproximateSyncPolicy; ros::NodeHandle nh;
ros::AsyncSpinner spinner;
image_transport::ImageTransport it;
image_transport::SubscriberFilter *subImageColor, *subImageDepth;
message_filters::Subscriber<sensor_msgs::CameraInfo> *subCameraInfoColor, *subCameraInfoDepth; message_filters::Synchronizer<ExactSyncPolicy> *syncExact;
message_filters::Synchronizer<ApproximateSyncPolicy> *syncApproximate; std::thread imageViewerThread;
Mode mode; std::ostringstream oss;
std::vector<int> params; //RGBDSLAM slam; //the slam object
ORB_SLAM2::System* orbslam =nullptr; public:
Receiver(const std::string &topicColor, const std::string &topicDepth, const bool useExact, const bool useCompressed)
: topicColor(topicColor), topicDepth(topicDepth), useExact(useExact), useCompressed(useCompressed),
updateImage(false), updateCloud(false), save(false), running(false), frame(0), queueSize(5),
nh("~"), spinner(0), it(nh), mode(CLOUD)
{
cameraMatrixColor = cv::Mat::zeros(3, 3, CV_64F);
cameraMatrixDepth = cv::Mat::zeros(3, 3, CV_64F);
params.push_back(cv::IMWRITE_JPEG_QUALITY);
params.push_back(100);
params.push_back(cv::IMWRITE_PNG_COMPRESSION);
params.push_back(1);
params.push_back(cv::IMWRITE_PNG_STRATEGY);
params.push_back(cv::IMWRITE_PNG_STRATEGY_RLE);
params.push_back(0); string orbVocFile = "/home/tommy/catkin_ws/src/tommy/config/ORBvoc.txt";
string orbSetiingsFile = "/home/tommy/catkin_ws/src/tommy/config/kinect2_qhd.yaml"; orbslam = new ORB_SLAM2::System( orbVocFile, orbSetiingsFile ,ORB_SLAM2::System::RGBD, true );
} ~Receiver()
{
if (orbslam)
{
orbslam->Shutdown();
delete orbslam;
}
} void run(const Mode mode)
{
start(mode);
stop();
} void finish()
{
}
private:
void start(const Mode mode)
{
this->mode = mode;
running = true; std::string topicCameraInfoColor = topicColor.substr(0, topicColor.rfind('/')) + "/camera_info";
std::string topicCameraInfoDepth = topicDepth.substr(0, topicDepth.rfind('/')) + "/camera_info"; image_transport::TransportHints hints(useCompressed ? "compressed" : "raw");
subImageColor = new image_transport::SubscriberFilter(it, topicColor, queueSize, hints);
subImageDepth = new image_transport::SubscriberFilter(it, topicDepth, queueSize, hints);
subCameraInfoColor = new message_filters::Subscriber<sensor_msgs::CameraInfo>(nh, topicCameraInfoColor, queueSize);
subCameraInfoDepth = new message_filters::Subscriber<sensor_msgs::CameraInfo>(nh, topicCameraInfoDepth, queueSize); if(useExact)
{
syncExact = new message_filters::Synchronizer<ExactSyncPolicy>(ExactSyncPolicy(queueSize), *subImageColor, *subImageDepth, *subCameraInfoColor, *subCameraInfoDepth);
syncExact->registerCallback(boost::bind(&Receiver::callback, this, _1, _2, _3, _4));
}
else
{
syncApproximate = new message_filters::Synchronizer<ApproximateSyncPolicy>(ApproximateSyncPolicy(queueSize), *subImageColor, *subImageDepth, *subCameraInfoColor, *subCameraInfoDepth);
syncApproximate->registerCallback(boost::bind(&Receiver::callback, this, _1, _2, _3, _4));
} spinner.start(); std::chrono::milliseconds duration(1);
while(!updateImage || !updateCloud)
{
if(!ros::ok())
{
return;
}
std::this_thread::sleep_for(duration);
}
createLookup(this->color.cols, this->color.rows); switch(mode)
{
case IMAGE:
imageViewer();
break;
case BOTH:
imageViewerThread = std::thread(&Receiver::imageViewer, this);
break;
}
} void stop()
{
spinner.stop(); if(useExact)
{
delete syncExact;
}
else
{
delete syncApproximate;
} delete subImageColor;
delete subImageDepth;
delete subCameraInfoColor;
delete subCameraInfoDepth; running = false;
if(mode == BOTH)
{
imageViewerThread.join();
}
} void callback(const sensor_msgs::Image::ConstPtr imageColor, const sensor_msgs::Image::ConstPtr imageDepth,
const sensor_msgs::CameraInfo::ConstPtr cameraInfoColor, const sensor_msgs::CameraInfo::ConstPtr cameraInfoDepth)
{
cv::Mat color, depth; readCameraInfo(cameraInfoColor, cameraMatrixColor);
readCameraInfo(cameraInfoDepth, cameraMatrixDepth);
readImage(imageColor, color);
readImage(imageDepth, depth); // IR image input
if(color.type() == CV_16U)
{
cv::Mat tmp;
color.convertTo(tmp, CV_8U, 0.02);
cv::cvtColor(tmp, color, CV_GRAY2BGR);
} lock.lock();
this->color = color;
this->depth = depth;
updateImage = true;
updateCloud = true;
lock.unlock();
} void imageViewer()
{
cv::Mat color, depth;
for(; running && ros::ok();)
{
if(updateImage)
{
lock.lock();
color = this->color;
depth = this->depth;
updateImage = false;
lock.unlock(); if (orbslam)
{
orbslam->TrackRGBD( color, depth, ros::Time::now().toSec() );
}
} } cv::destroyAllWindows();
cv::waitKey(100);
} void readImage(const sensor_msgs::Image::ConstPtr msgImage, cv::Mat &image) const
{
cv_bridge::CvImageConstPtr pCvImage;
pCvImage = cv_bridge::toCvShare(msgImage, msgImage->encoding);
pCvImage->image.copyTo(image);
} void readCameraInfo(const sensor_msgs::CameraInfo::ConstPtr cameraInfo, cv::Mat &cameraMatrix) const
{
double *itC = cameraMatrix.ptr<double>(0, 0);
for(size_t i = 0; i < 9; ++i, ++itC)
{
*itC = cameraInfo->K[i];
}
} void dispDepth(const cv::Mat &in, cv::Mat &out, const float maxValue)
{
cv::Mat tmp = cv::Mat(in.rows, in.cols, CV_8U);
const uint32_t maxInt = 255; #pragma omp parallel for
for(int r = 0; r < in.rows; ++r)
{
const uint16_t *itI = in.ptr<uint16_t>(r);
uint8_t *itO = tmp.ptr<uint8_t>(r); for(int c = 0; c < in.cols; ++c, ++itI, ++itO)
{
*itO = (uint8_t)std::min((*itI * maxInt / maxValue), 255.0f);
}
} cv::applyColorMap(tmp, out, cv::COLORMAP_JET);
} void combine(const cv::Mat &inC, const cv::Mat &inD, cv::Mat &out)
{
out = cv::Mat(inC.rows, inC.cols, CV_8UC3); #pragma omp parallel for
for(int r = 0; r < inC.rows; ++r)
{
const cv::Vec3b
*itC = inC.ptr<cv::Vec3b>(r),
*itD = inD.ptr<cv::Vec3b>(r);
cv::Vec3b *itO = out.ptr<cv::Vec3b>(r); for(int c = 0; c < inC.cols; ++c, ++itC, ++itD, ++itO)
{
itO->val[0] = (itC->val[0] + itD->val[0]) >> 1;
itO->val[1] = (itC->val[1] + itD->val[1]) >> 1;
itO->val[2] = (itC->val[2] + itD->val[2]) >> 1;
}
}
} void createLookup(size_t width, size_t height)
{
const float fx = 1.0f / cameraMatrixColor.at<double>(0, 0);
const float fy = 1.0f / cameraMatrixColor.at<double>(1, 1);
const float cx = cameraMatrixColor.at<double>(0, 2);
const float cy = cameraMatrixColor.at<double>(1, 2);
float *it; lookupY = cv::Mat(1, height, CV_32F);
it = lookupY.ptr<float>();
for(size_t r = 0; r < height; ++r, ++it)
{
*it = (r - cy) * fy;
} lookupX = cv::Mat(1, width, CV_32F);
it = lookupX.ptr<float>();
for(size_t c = 0; c < width; ++c, ++it)
{
*it = (c - cx) * fx;
}
}
}; int main(int argc, char **argv)
{
ros::init(argc, argv, "kinect2_slam", ros::init_options::AnonymousName); if(!ros::ok())
{
return 0;
}
std::string topicColor = "/kinect2/sd/image_color_rect";
std::string topicDepth = "/kinect2/sd/image_depth_rect";
bool useExact = true;
bool useCompressed = false;
Receiver::Mode mode = Receiver::IMAGE;
// 初始化receiver
Receiver receiver(topicColor, topicDepth, useExact, useCompressed); //OUT_INFO("starting receiver...");
receiver.run(mode); receiver.finish(); ros::shutdown(); return 0;
}
ORBvoc.txt和kinect2_qhd.yaml文件拷貝到和上面對應的文件夾中
2.拷貝ORBSLAM2頭文件到kinect2_viewer文件夾,所有.h文件包含在include文件夾裏面,include文件夾裏面加入orbslam2的Thirdparty文件夾,include文件夾裏面加入libORB_SLAM2.so,按照如下方式修改cmakelist文件
//cmakelist中修改
find_package(Pangolin REQUIRED)
find_package(Eigen3 3.1.0 REQUIRED)
include_directories(include
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
${PCL_INCLUDE_DIRS}
${kinect2_bridge_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}/include
${EIGEN3_INCLUDE_DIR}
${Pangolin_INCLUDE_DIRS}
)
add_executable(kinect2_viewer src/viewer.cpp)
target_link_libraries(kinect2_viewer
${catkin_LIBRARIES}
${OpenCV_LIBRARIES}
${PCL_LIBRARIES}
${kinect2_bridge_LIBRARIES}
${Pangolin_LIBRARIES}
${PROJECT_SOURCE_DIR}/include/libORB_SLAM2.so
${PROJECT_SOURCE_DIR}/include/Thirdparty/DBoW2/lib/libDBoW2.so
${PROJECT_SOURCE_DIR}/include/Thirdparty/g2o/lib/libg2o.so
)
3. 編譯
a).cd /home/tommy/catkin_ws/src/iai_kinect2/kinect2_viewer/build
b).cmake ..
c).make
注意:出现/ws/ORB_SLAM2/src/LoopClosing.cc: In member function ‘void ORB_SLAM2::LoopClosing::Run()’:
/ws/ORB_SLAM2/src/LoopClosing.cc:84:20: error: ‘usleep’ was not declared in this scope
usleep(5000);
需要打开相应的代码,在头文件里面添加usleep 的头文件unistd.h,问题就解决了!
參考“http://blog.csdn.net/wangshuailpp/article/details/70226534”
//.................................................................................
重写kinect2_viewer,编译高博kinect2在orbslam2上跑的程序(解决cmakefile中库依赖和头文件的问题)的更多相关文章
- 编译高博十四讲代码遇到依赖项g2o和cholmod的坑
1. 找不到g2o库!在CMakeLists.txt中使用指令 message(STATUS "${G2O_FOUND}") 打印结果为NO. 问题描述: CMakeLists.t ...
- VScode,code::blocksC语言编译运行出现不支持的16位应用程序解决方法
最近,莫名其妙c代码就是编译运行不了,老是提示不支持的16位应用程序 然后网上找了各种教程 只有这个成功了(害得我还升了下系统) 实现进入Windows设置 然后点更新和安全--恢复 然后点高级启动- ...
- 一键自动发布ipa(更新svn,拷贝资源,压缩资源,加密图片资源,加密数据文件,加密lua脚本,编译代码,ipa签名,上传ftp)
一键自动发布ipa(更新svn,拷贝资源,压缩资源,加密图片资源,加密数据文件,加密lua脚本,编译代码,ipa签名,上传ftp) 程序员的生活要一切自动化,更要幸福^_^. 转载请注明出处http: ...
- 预编译头文件 StdAfx.h
预编译头文件: 最常见的使用场景就是 StdAfx.h 文件,在这个文件中包含常用的头文件,比如windows.h,cstdio,string,别的 .cpp 文件去包含 StdAfx.h 头文件.编 ...
- C++预编译头文件 – stdafx.h
预编译头文件的由来 也许请教了别的高手之后,他们会告诉你,这是预编译头,必须包含.可是,这到底是为什么呢?预编译头有什么用呢? 咱们从头文件的编译原理讲起.其实头文件并不神秘,其在编译时的作用,就是把 ...
- C 头文件、宏、编译问题
@2019-02-15 [小记] > C 头文件的防重复包含是针对同一个源文件而言 原因: #include 头文件就是一段代码的拷贝,头文件中若有类型定义等,重复包含就会造成编译错误,若无类型 ...
- [C] 如何使用头文件 .h 编译 C 源码
在 C 语言中,头文件或包含文件通常是一个源代码文件,程序员使用编译器指令将头文件包含进其他源文件的开始(或头部),由编译器在处理另一个源文件时自动包含进来. 一个头文件一般包含类.子程序.变量和其他 ...
- 预编译头文件pch
1. 预编译头文件 作用:提高编译效率.预编译头文件(扩展名为.PCH),是为了提高编译效率而使用的一种方法,把一个工程中较稳定的代码预先编译好放在一个文件(.PCH)里.避免每次编译 ...
- 一个C++引用库的头文件预编译陷阱
写在前面 老胡最近在工作中,有个场景需要使用一个第三方库,引用头文件,链接库,编译运行,一切都很正常,但是接下来就遇到了一个很诡异的问题,调用该库的中的一个对象方法为对象修改属性的时候,会影响到对象的 ...
随机推荐
- [SRM686]CyclesNumber
题意:求$n$个数的所有排列形成的轮换个数的$m$次方之和 我以前只知道这是GDKOI的题,今天在ckw博客上发现它是TC题...原题真是哪里都有... 就是求$\sum\limits_{i=1}^n ...
- 【计算几何】【分类讨论】Gym - 101173C - Convex Contour
注意等边三角形的上顶点是卡不到边界上的. 于是整个凸包分成三部分:左边的连续的三角形.中间的.右边的连续的三角形. 套个计算几何板子求个三角形顶点到圆的切线.三角形顶点到正方形左上角距离啥的就行了,分 ...
- Java 8:不要再用循环了 Stream替代for循环
原文:http://www.importnew.com/14841.html 在这篇文章里,我们将会去了解传统循环的一些替代方案.在Java 8的新功能特性中,最棒的特性就是允许我们去表达我们想要完成 ...
- linux下activityMQ安装
>下载 到ActiveMQ官网,找到下载点. 目前, 官网为http://activemq.apache.org/ >启动 下载到本机,并解压 wget http://apache.f ...
- Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数
点击浏览器中的URL链接,启动特定的App. 首先做成HTML的页面,页面内容格式如下: <a href="[scheme]://[host]/[path]?[query]" ...
- MLP 之手写数字识别
0. 前言 前面我们利用 LR 模型实现了手写数字识别,但是效果并不好(不到 93% 的正确率). LR 模型从本质上来说还只是一个线性的分类器,只不过在线性变化之后加入了非线性单调递增 sigmoi ...
- iOS: 状态栏、导航栏、标签栏、工具栏
三种项目栏总结: 工具栏:UIToolBar 导航栏:UINavigationBar 标签栏:UITabBar UIToolBar的按钮单元为:UIBarButtonItem UINavigati ...
- SqlServer数据库1433端口问题1
在本地使用telnet ip 1433 命令测试数据库1433端口是否打开,总是提示错误,根据网上查找资料总结了如下两点思路供参考,欢迎指正! (1)第一种情况可能是"Telnet客户端& ...
- asset bundle打包策略
一次引用的 不单独打包 2次的看大小 小的不单独打包 2次以上单独打包 2这个值 可以测一测 取平衡
- vector relation
::std::vector<> 的存储管理 以下成员函数用于存储管理: void reserve( size_t n ); size_t capacity() const; void re ...