ubuntu 使用自己的双目相机发布 ROS
https://blog.csdn.net/KID_yuan/article/details/101272384
https://blog.csdn.net/weixin_53073284/article/details/125671358
ls /dev/video* //插上摄像头后打开终端查看是否检测到摄像头
mkdir -p camera_ws/src //新建工作空间 cd camera_ws/src/ git clone https://github.com/bosch-ros-pkg/usb_cam.git //克隆功能包 cd .. catkin_make //编译
报错:
CMake Error at /opt/ros/melodic/share/catkin/cmake/catkin_workspace.cmake:100 (message):
This workspace contains non-catkin packages in it, and catkin cannot build
a non-homogeneous workspace without isolation. Try the
'catkin_make_isolated' command instead.
Call Stack (most recent call first):
CMakeLists.txt:69 (catkin_workspace)
解决:
https://github.com/MIT-SPARK/Kimera-VIO-ROS/issues/13#issuecomment-883298337
重新使用: catkin build
构建
vim src/usb_cam/launch/usb_cam-test.launch
<launch> <node name="usb_cam" pkg="usb_cam" type="usb_cam_node" output="screen" > <param name="video_device" value="/dev/video0" /> <param name="image_width" value="2560" /> <param name="image_height" value="720" /> <param name="pixel_format" value="yuyv" /> <param name="camera_frame_id" value="usb_cam" /> <param name="io_method" value="mmap"/> </node> <node name="image_view" pkg="image_view" type="image_view" respawn="false" output="screen"> <remap from="image" to="/usb_cam/image_raw"/> <param name="autosize" value="true" /> </node> </launch>
cd ~/camera_ws source devel/setup.bash roslaunch usb_cam usb_cam-test.launch
报错:
ERROR: cannot launch node of type [usb_cam/usb_cam_node]: Cannot locate node of type [usb_cam_node] in package [usb_cam]. Make sure file exists in package path and permission is set to executable (chmod +x)
问题: 这好像是 冲突了。https://github.com/ros-drivers/usb_cam/issues/96#issuecomment-1400883250
分割线
注意!注意!注意!我们抛弃了上述方法。
rm -r ~/camera_ws
参考:https://blog.csdn.net/chencaw/article/details/101193038
apt-get install ros-melodic-usb-cam apt-get install ros-melodic-image-* apt-get install ros-melodic-rqt-image-view roscd usb_cam # 进入 `/opt/ros/melodic/share/usb_cam` vim launch/usb_cam-test.launch
修改分辨率为 2560 720:
<param name="image_width" value="2560" /> <param name="image_height" value="720" />
运行:
roslaunch usb_cam usb_cam-test.launch
即可看到双目视频。
但是我们可视化出来是 双目图像在一个 图像中,我们需要分割双目图像。参考:
https://blog.csdn.net/weixin_48592526/article/details/122724361
https://www.guyuehome.com/34995
cd mkdir -p dcamera_ws/src cd dcamera_ws catkin_make
cd src catkin_create_pkg camera_split cv_bridge image_transport roscpp sensor_msgs std_msgs camera_info_manager
vim src/camera_split/CMakeLists.txt ## Specify additional locations of header files ## Your package locations should be listed before other locations include_directories( # include ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ) #添加可执行文件 add_executable(camera_split_node src/camera_split.cpp ) #指定链接库 target_link_libraries(camera_split_node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} )
vim src/camera_split/launch/camera_split_no_calibration.launch <launch> <node pkg="camera_split" type="camera_split_node" name="camera_split_node" output="screen" /> <node pkg="image_view" type="image_view" name="image_view_left" respawn="false" output="screen"> <remap from="image" to="/left_cam/image_raw"/> <param name="autosize" value="true" /> </node> <node pkg="image_view" type="image_view" name="image_view_right" respawn="false" output="screen"> <remap from="image" to="/right_cam/image_raw"/> <param name="autosize" value="true" /> </node> </launch>
vim src/camera_split/src/camera_split.cpp #include <ros/ros.h> #include <iostream> #include <image_transport/image_transport.h> #include <cv_bridge/cv_bridge.h> #include <sensor_msgs/image_encodings.h> #include <camera_info_manager/camera_info_manager.h> #include <opencv2/opencv.hpp> //#include <opencv2/imgproc/imgproc.hpp> //#include <opencv2/highgui/highgui.hpp> using namespace std; class CameraSplitter { public: CameraSplitter():nh_("~"),it_(nh_) { image_sub_ = it_.subscribe("/usb_cam/image_raw", 1, &CameraSplitter::imageCallback, this); image_pub_left_ = it_.advertiseCamera("/left_cam/image_raw", 1); image_pub_right_ = it_.advertiseCamera("/right_cam/image_raw", 1); cinfo_ =boost::shared_ptr<camera_info_manager::CameraInfoManager>(new camera_info_manager::CameraInfoManager(nh_)); //读取参数服务器参数,得到左右相机参数文件的位置 string left_cal_file = nh_.param<std::string>("left_cam_file", ""); string right_cal_file = nh_.param<std::string>("right_cam_file", ""); if(!left_cal_file.empty()) { if(cinfo_->validateURL(left_cal_file)) { cout<<"Load left camera info file: "<<left_cal_file<<endl; cinfo_->loadCameraInfo(left_cal_file); ci_left_ = sensor_msgs::CameraInfoPtr(new sensor_msgs::CameraInfo(cinfo_->getCameraInfo())); } else { cout<<"Can't load left camera info file: "<<left_cal_file<<endl; ros::shutdown(); } } else { cout<<"Did not specify left camera info file." <<endl; ci_left_=sensor_msgs::CameraInfoPtr(new sensor_msgs::CameraInfo()); } if(!right_cal_file.empty()) { if(cinfo_->validateURL(right_cal_file)) { cout<<"Load right camera info file: "<<right_cal_file<<endl; cinfo_->loadCameraInfo(right_cal_file); ci_right_ = sensor_msgs::CameraInfoPtr(new sensor_msgs::CameraInfo(cinfo_->getCameraInfo())); } else { cout<<"Can't load right camera info file: "<<left_cal_file<<endl; ros::shutdown(); } } else { cout<<"Did not specify right camera info file." <<endl; ci_right_=sensor_msgs::CameraInfoPtr(new sensor_msgs::CameraInfo()); } } void imageCallback(const sensor_msgs::ImageConstPtr& msg) { cv_bridge::CvImageConstPtr cv_ptr; namespace enc= sensor_msgs::image_encodings; cv_ptr= cv_bridge::toCvShare(msg, enc::BGR8); //截取ROI(Region Of Interest),即左右图像,会将原图像数据拷贝出来。 leftImgROI_=cv_ptr->image(cv::Rect(0,0,cv_ptr->image.cols/2, cv_ptr->image.rows)); rightImgROI_=cv_ptr->image(cv::Rect(cv_ptr->image.cols/2,0, cv_ptr->image.cols/2, cv_ptr->image.rows )); //创建两个CvImage, 用于存放原始图像的左右部分。CvImage创建时是对Mat进行引用的,不会进行数据拷贝 leftImgPtr_=cv_bridge::CvImagePtr(new cv_bridge::CvImage(cv_ptr->header, cv_ptr->encoding,leftImgROI_) ); rightImgPtr_=cv_bridge::CvImagePtr(new cv_bridge::CvImage(cv_ptr->header, cv_ptr->encoding,rightImgROI_) ); //发布到/left_cam/image_raw和/right_cam/image_raw ci_left_->header = cv_ptr->header; //很重要,不然会提示不同步导致无法去畸变 ci_right_->header = cv_ptr->header; sensor_msgs::ImagePtr leftPtr =leftImgPtr_->toImageMsg(); sensor_msgs::ImagePtr rightPtr =rightImgPtr_->toImageMsg(); leftPtr->header=msg->header; //很重要,不然输出的图象没有时间戳 rightPtr->header=msg->header; image_pub_left_.publish(leftPtr,ci_left_); image_pub_right_.publish(rightPtr,ci_right_); } private: ros::NodeHandle nh_; image_transport::ImageTransport it_; image_transport::Subscriber image_sub_; image_transport::CameraPublisher image_pub_left_; image_transport::CameraPublisher image_pub_right_; boost::shared_ptr<camera_info_manager::CameraInfoManager> cinfo_; sensor_msgs::CameraInfoPtr ci_left_; sensor_msgs::CameraInfoPtr ci_right_; cv::Mat leftImgROI_; cv::Mat rightImgROI_; cv_bridge::CvImagePtr leftImgPtr_=NULL; cv_bridge::CvImagePtr rightImgPtr_=NULL; }; int main(int argc,char** argv) { ros::init(argc,argv, "camera_split"); CameraSplitter cameraSplitter; ros::spin(); return 0; }
cd dcamera_ws catkin_make source ./devel/setup.bash roslaunch camera_split camera_split_no_calibration.launch
请注意:这里我们不会出来画面。我们还需要另外启动一个终端。
cd /opt/ros/melodic/share/usb_cam roslaunch usb_cam usb_cam-test.launch
然后就可以出来 3个 图像窗口了。分别是双目-左目-右目。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】