文章目录
- 可视化图片
- catkin新建工程
- 主函数代码
- cmakelists.txt
- 编译和运行
- 在rviz中显示
- 可视化realsense
- 安装realsense SDK
- 在ROS中安装realsense
- 在rviz中显示
- 参考
可视化图片
catkin新建工程
mkdir -p rosopencv/src
cd rosopencv/src
catkin_create_pkg rosopencv sensor_msgs cv_bridge roscpp std_msgs image_transport
cd ..
catkin_make
source ./devel/setup.bash
主函数代码
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <stdio.h>
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_publisher");
ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
image_transport::Publisher pub = it.advertise("camera/image", 1);
cv::Mat image = cv::imread("/home/topeet/test.jpg", CV_LOAD_IMAGE_COLOR);
if(image.empty()){
printf("open error\n");
}
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
ros::Rate loop_rate(5);
while (nh.ok()) {
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
}
cmakelists.txt
在功能包中的cmakelists.txt添加如下代码
set(OpenCV_DIR /usr/local/share/OpenCV)
message(STATUS " version: ${OpenCV_VERSION}")
find_package(OpenCV REQUIRED)
add_executable(rosopencv src/rosopencv.cpp)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(cmaketest ${OpenCV_LIBS})
编译和运行
cd ~/dev/workspace/rosopencv
catkin_make
在第一个terminal输入
rosrun rosopencv rosopencv
在第二个terminal输入
rosrun rosopencv rosopencv
在rviz中显示
rosrun rviz rviz
左下角add,按照topic进行添加操作
可视化realsense
安装realsense SDK
sudo apt-key adv --keyserver keys.gnupg.net --recv-key F6E65AC044F831AC80A06380C8B3A55A6F3EFCDE || sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-key F6E65AC044F831AC80A06380C8B3A55A6F3EFCDE
sudo add-apt-repository "deb http://realsense-hw-public.s3.amazonaws.com/Debian/apt-repo bionic main" -u
sudo apt-get install librealsense2-dkms
sudo apt-get install librealsense2-utils
sudo apt-get install librealsense2-dev
sudo apt-get install librealsense2-dbg
# 运行可视化界面
realsense-viewer
在ROS中安装realsense
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src/
git clone https://github.com/IntelRealSense/realsense-ros.git
cd realsense-ros/
git checkout `git tag | sort -V | grep -P "^2.\d+\.\d+" | tail -1`
cd ..
catkin_init_workspace
cd ..
catkin_make clean
catkin_make -DCATKIN_ENABLE_TESTING=False -DCMAKE_BUILD_TYPE=Release
catkin_make install
#添加环境变量
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
# 运行
roslaunch realsense2_camera rs_camera.launch
在rviz中显示
在terminal输入
rviz
添加你感兴趣的topic
这里我可视化了深度图和RGB
参考
https://blog.csdn.net/qq_31918961/article/details/104737351?ops_request_misc=&request_id=&biz_id=102&utm_term=realsensing%20%20%20%E5%9C%A8%20%20%20rviz%E4%B8%AD%E6%98%BE%E7%A4%BA&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-1-104737351.142v67control,201v4add_ask,213v2t3_control1&spm=1018.2226.3001.4187