环境:
ubuntu20.04
opencv4.5.5
ros: noetic
参考链接:
在ROS中使用OpenCV进行简单的图像处理
ROS中使用opencv
第一个链接注释比较多,第二个链接是搞定的,成功的界面是这样的:
读取opencv的程序:
#include
#include
#include
#include
#include
#include
#include
static const char WINDOW[] = "Image window";
static void help()
{
printf("\nThis program demonstrates converting OpenCV Image to ROS Image messages \n"
);
}
int main(int argc, char** argv)
{
help();
ros::init(argc, argv, "image_converter");
//Reading an image from the file
cv::Mat cv_image = cv::imread("/home/qiao/1.png");
if(cv_image.empty() )
{
ROS_ERROR("Read the picture failed!");
return -1;
}
//Convert OpenCV image to ROS message
ros::NodeHandle node;
image_transport::ImageTransport transport(node);
image_transport::Publisher image_pub;
image_pub=transport.advertise("OutImage", 1);
ros::Time time=ros::Time::now();
cv_bridge::CvImage cvi;
cvi.header.stamp = time;
cvi.header.frame_id = "image";
cvi.encoding = "bgr8";
cvi.image = cv_image;
sensor_msgs::Image im;
cvi.toImageMsg(im);
image_pub.publish(im);
ROS_INFO("Converted Successfully!");
//Show the image
cv::namedWindow(WINDOW);
cv::imshow(WINDOW,cv_image);
cv::waitKey(0);
ros::spin();
return 0;
}
后面还需要编译:
mkdir -p 工作区/src
cd 工作区/src
catkin_init_workspace
catkin_create_pkg --rosdistro noetic 工作区 sensor_msgs cv_bridge roscpp std_msgs image_transport
find_package(OpenCV REQUIRED)
include_directories(include ${OpenCV_INCLUDE_DIRS})
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(node src/node.cpp)
target_link_libraries(node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})
add_dependencies(node 工作区_gencpp)
cd 工作区
catkin_make
2022年1月7更新:
find_package(OpenCV 4 REQUIRED)
我之前是加了OpenCV 4 的,但是我全部重新整了一遍发现不需要加4,因为我系统里很干净就一个我编译的opencv4.2,所以我就去掉了4
find_package(OpenCV REQUIRED)
然后就是运行:
roscore
source devel/setup.bash
rosrun 工作区 cpp文件的名字
至此能正确显示图像,流程也是没有问题的。
不过有几个问题我没理清楚:
经验证上面的opencv用的是ROS自带的,我需要将opencv换成自己的4.5.5版本!
参考链接:
Compiling ROS cv_bridge with Python 3
解决Ubuntu环境下ROS Kinetic中的cv_bridge和自己安装OpenCV 2.4.xx兼容性笔记
这个大佬写的巨好!!
Compiling ROS cv_bridge with Python 3
OpenCV4 instead of opencv3 #272
也参考了这两个链接!
这里面要注意的是在Opencv4.5.5 中使用cv_bridge转换msgs 与 opencv图像需要将opencv版本换成自己的,我的是这样的:
set(OpenCV_VERSION 4.5.5)
set(OpenCV_VERSION_MAJOR 4)
set(OpenCV_VERSION_MINOR 5)
set(OpenCV_VERSION_PATCH 5)
set(OpenCV_SHARED ON)
set(OpenCV_CONFIG_PATH /usr/local/share/opencv4)
set(OpenCV_INSTALL_PATH /usr/local/)
set(OpenCV_LIB_COMPONENTS opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_viz;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_cvv;opencv_datasets;opencv_dpm;opencv_face;opencv_fuzzy;opencv_hdf;opencv_img_hash;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_tracking;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto)
set(OpenCV_USE_MANGLED_PATHS TRUE)
set(OpenCV_MODULES_SUFFIX )
最后成功的截图:
ROS这部分算是搞完了,虽然还有很多不理解,但是也算是圆满,接下来就是理解这玩意以及后续的应用!
查看opencv版本信息:pkg-config --cflags opencv
pkg-config --modversion opencv4
OPENCV_GENERATE_PKGCONFIG - pkg-config --modversion opencv4
2022年1月7更新:
我重新构建节点的时候报了个错误
Could neither find file “toplevel.cmake” in any workspace nor relative, checked the following paths
我尝试了网上很多解决办法都不行,会报新的问题,最终解决方案:
cd /opt/ros/noetic/share/
ls -l catkin
发现有catkin文件夹,我把这个文件夹移到了前面的目录
mv catkin ../
也就是share和catkin在同级目录下:
其他都按照之前的处理就好,最终显示也是正常的。
202年1月7更新,重新搞了一遍,存一些中间文件:
1. opencv冲突的解决方案:
https://blog.csdn.net/qq_38441692/article/details/107249191
2. cv_bridge
https://blog.csdn.net/weixin_43493992/article/details/116308137
3. 为什么OpenCV4 “pkg-config --modversion opencv”显示“ No package ‘opencv‘ found”?解决方法!
https://blog.csdn.net/qq_38505858/article/details/117780774
opencv4.pc 这是在pkgconfig中的,默认的会找这个文件
pkg-config --modversion opencv4
4. ubuntu18.04 编译OpenCV4.2.0 OpenCV_Contrib4.2.0
https://blog.csdn.net/PecoHe/article/details/90581526
https://blog.csdn.net/u011171361/article/details/106303216
5. sudo apt install python3-rosdep not sudo apt install python3-rosdep2
DEFAULT_SOURCES_LIST_URL = 'file:///home/qiao/project/rosdistro/rosdep/sources.list.d/20-default.list'
REP3_TARGETS_URL = 'file:///home/qiao/project/rosdistro/releases/targets.yaml'
DEFAULT_INDEX_URL = 'file:///home/qiao/project/rosdistro/index-v4.yaml'
sudo apt install python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
catkin_create_pkg --rosdistro noetic opencv_test sensor_msgs cv_bridge roscpp std_msgs image_transport