ROS下使用OpenCV详细步骤+一些错误解决(段错误,核心已转储!!)
1.OpenCV环境配置和路径设置
进入opencv文件夹,创建编译文件夹
mkdir build
cd build
cmake ..
cmake-gui //先行下载gui版本cmake
选择OpenCV源码所在路径和编译文件所在路径,单击【Configure】按钮,默认选项即可,单击【Finish】后,在cmake界面会出现很多变量。
BUILD_opencv_world和OPENCV_ENABLE_NONFREE两个变量,在其后面的方框上打勾。
OPENCV_EXTRA_MODULES_PATH变量,选择路径为opencv_contrib中的module文件夹
再次Configue,Configuring done后generate
sudo make
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf
在空白文件输入以下路径
/usr/local/lib
打开终端,使路径生效
sudo ldconfig
配置bshrc
sudo gedit /etc/bash.bashrc
在打开的文件最末尾添加以下代码并保存,保存后退出
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
继续在终端执行以下命令使得配置生效
source /etc/bash.bashrc
2.OpenCV测试程序
mkdir -p ros_opencv/src
cd ros_opencv/src
catkin_init_workspace
catkin_create_pkg opencv_test sensor_msgs cv_bridge roscpp std_msgs image_transport
修改opencv_test目录下的CMakeLists.txt
主要在find_package加上
find_package(OpenCV REQUIRED) //单独写,不然可能会报错
以及加上
include_directories(include ${OpenCV_INCLUDE_DIRS})
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(opencv_test_node src/opencv_test_node.cpp)
target_link_libraries(opencv_test_node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})
add_dependencies(opencv_test_node opencv_test_gencpp) //这个后面如果报错,是因为你的环境不是gencpp
然后你需要下载一张图片放在一个已知的位置,为读取图片做准备
touch opencv_test_node.cpp //看上面的CMakeLists.txt的节点名字
#include <ros/ros.h>
#include <stdio.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
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("图片路径");
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;
}
3.编译
cd ros_opencv
catkin_make
可能遇到编译错误:
1.could not find opencv
解决方案:
确认是否正确安装opencv
再看第一部分使用cmakeGUI配置和路径设置是否成功
CMake Error at CMakeLists.txt:xx (find_package):
By not providing "xx.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
解决方案:
将opencv安装目录添加到CMAKE_PREFIX_PATH
例如,按照错误信息指示,在CMakeLists.txt中加入一行list,路径要改成自己的opencv安装路径
list(APPEND CMAKE_PREFIX_PATH "/home/xx/opencv版本号/")
find_package(OpenCV REQUIRED)
或设置OpenCV_DIR为一个包含OpenCVConfig.cmake或者opencv-config.cmake的目录
CMakeLists.txt中加入一行set OpenCV_DIR,以opencv4为例,后面的路径要改成你的路径,注意不是安装根目录,是安装目录中包含OpenCVConfig.cmake的文件夹路径
set(OpenCV_DIR /opt/opencv版本号/lib/cmake/opencv4)
find_package(OpenCV REQUIRED)
3.could not find gencpp
在CMakeLists.txt最后一行将 add_dependencies(opencv_test_node opencv_test_gencpp) 删掉
或者不冲突的话,将CMakeLists.txt的第一行改为
cmake_minimum_required(VERSION 2.8.3)
4.运行
roscore
source devel/setup.bash
rosrun opencv_test opencv_test_node
运行时遇到错误:
1.检查bashrc是否有source环境变量
sudo gedit ~/.bashrc
//在文件末加上
source ~/工作空间/devel/setup.bash
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:~/工作空间/
//重启bashrc
source ~/.bashrc
//查看ros路径是否添加上
echo $ROS_PACKAGE_PATH
2.报错:段错误,核心已转储
原因大致上是;访问不存在的内存地址、访问系统保护的内存地址和访问只读的内存地址
在这里这是因为ros有自带了的opencv,与本地版本有冲突会出现以上错误,可能性太多了,所以讲解思路:
(1)设置core文件,找到段错误生成的core文件
(2)利用core文件,使用GDB测试找到问题所在
到运行这一步,你可以直接在相关路径下的终端启动gdb测试,可以看到对内存出现非法访问时将收到段错误信号SIGSEGV下面就是出错的位置,我们然后使用backtrace回溯定位问题(gdb/bt)
最后,你找到出调用库时出问题的路径,把版本改为你调用的opencv库的版本,问题解决
运行成功
文章来源:https://www.toymoban.com/news/detail-840980.html
原文链接:https://blog.csdn.net/qq_28306361/article/details/851421923文章来源地址https://www.toymoban.com/news/detail-840980.html
到了这里,关于ROS下配置OpenCV+一些错误解决(段错误,核心已转储)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!