简体   繁体   English

使用cv_bridge(ROS indigo)和OpenCV 3时出现分段错误(核心转储)

[英]Segmentation fault (core dumped) when using cv_bridge(ROS indigo) and OpenCV 3

I am trying to make a simple project that uses ROS (indigo, installed on Ubuntu 14.04) and OpenCV 3.0.0 to read images from a video (webcam or file) and publish them using ROS´s image_transport. 我正在尝试制作一个简单的项目,使用ROS(安装在Ubuntu 14.04上的indigo)和OpenCV 3.0.0从视频(网络摄像头或文件)中读取图像,并使用ROS的image_transport发布它们。

Reading the image and displaying it works fine, but as soon as I try to include and use cv_bridge to convert and publish the image, it still compiles fine but when running the executable it fails with 读取图像并显示它可以正常工作,但是一旦我尝试包含并使用cv_bridge来转换和发布图像,它仍然编译得很好但是在运行可执行文件时它会失败

Segmentation fault (core dumped) 分段故障(核心转储)

My code: 我的代码:

#include <ctime>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "image_transport/image_transport.h"
#include "cv_bridge/cv_bridge.h"
#include <fstream>

int main(int argc, char **argv)
{
cv::Mat image;

ros::init(argc, argv, "webcam_streamer");
ros::NodeHandle nh;
ros::Rate loop_rate(500);
image_transport::ImageTransport it(nh);
image_transport::Publisher pub = it.advertise("cam_img", 10);

cv::VideoCapture cap("//home//milan//drop.avi");

if(!cap.isOpened())
{
    ROS_ERROR("COULD NOT OPEN FILE!\r\n");
    return 0;
}else
    ROS_INFO("Read file successfully!\r\n");

while(nh.ok())
{
ROS_INFO("Reading image");
cap.read(image);
ROS_INFO("...");
if(image.empty())
    break;
ROS_INFO("Success!");
cv::imshow("IMG", image);
cv::waitKey(20);
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
ROS_INFO("STOPPING CAM");
cap.release();
}

In my CMakeLists.txt I do 在我的CMakeLists.txt中

find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
cv_bridge
image_transport
)

find_package(OpenCV REQUIRED)

add_executable(stream_webcam_image src/stream_webcam_image.cpp)
target_link_libraries(stream_webcam_image ${catkin_LIBRARIES})
target_link_libraries(stream_webcam_image ${OpenCV_LIBS})
add_dependencies(stream_webcam_image stream_webcam_image_gencpp)

When I remove cv_bridge from my CMakeLists.txt and my includes and don´t try to publish the image, the program runs fine, else it fails with the following output: 当我从我的CMakeLists.txt和我的包中删除cv_bridge并且不尝试发布图像时,程序运行正常,否则它会失败并显示以下输出: 程序输出

So it seems that it does not even manage to read the image. 所以它似乎甚至无法读取图像。

The problem seems to be that ROS (cv_bridge in particular) uses an older version of OpenCV which is conflicting with the version 3 I have installed. 问题似乎是ROS(特别是cv_bridge)使用的旧版OpenCV与我安装的版本3冲突。

I tried to follow this: Latest OpenCV in ROS but the ls /opt/ros/rosversion -d/lib/libopencv* -hal did not find a current OpenCV in the ROS folder, and I could not find the specified OpenCV folder manually. 我试着遵循这个: ROS中的最新OpenCV,但是ls /opt/ros/rosversion -d/lib/libopencv* -hal在ROS文件夹中找不到当前的OpenCV,我无法手动找到指定的OpenCV文件夹。 I still followed the steps, but still the same error. 我仍然按照步骤,但仍然是同样的错误。

Then I found this question on ROS Answers which seems to be the same problem, but I tried to compile ROS after having installed OpenCV 3, which also should have built opencv_vision from source, as the last comment in the above thread suggests? 然后我在ROS Answers上发现了这个问题似乎是同样的问题,但我在安装OpenCV 3之后尝试编译ROS,也应该从源代码构建opencv_vision,如上面线程中的最后一条评论所暗示的那样?

Did I miss something, or is there a better solution to this Problem? 我错过了什么,或者是否有更好的解决方案?

My goal is to get my Raspberry Pi 2 to send images from it´s camera to my Laptop using ROS, and as Raspbian Jessie only seems to support OpenCV 3, I am limited to that. 我的目标是让我的Raspberry Pi 2使用ROS将相机中的图像发送到我的笔记本电脑,而Raspbian Jessie似乎只支持OpenCV 3,我仅限于此。 Also I am using QT Creator on Ubuntu to write my programs for the Pi, so using OpenCV 2 in Ubuntu and OpenCV 3 on the Pi would be really cumbersome. 另外我在Ubuntu上使用QT Creator为Pi编写程序,所以在Ubuntu中使用OpenCV 2和在Pi上使用OpenCV 3会非常麻烦。

Allright, I solved the Problem (by repeating what I thought I had done already) 好吧,我解决了问题(通过重复我以为我已经做过的事情)

What I did: 我做了什么:

1. Delete both ROS and OpenCV completely (I reset my Virtual Machine)
2. Download, build and install OpenCV 3
3. [Build ROS Jade from source][1] (not sure if the version makes a difference)

This now makes my code run without a problem on Ubuntu. 现在这使我的代码在Ubuntu上运行没有问题。

But guess what, when trying the same on the Pi 2, I get same error. 但是猜猜是什么,当在Pi 2上尝试相同时,我得到同样的错误。

So, same procedure: 所以,同样的程序:

1. Start from scratch (or make sure both OpenCV and ROS are removed completely)
2. Build and install OpenCV 3
3. Build and install ROS (only Indigo is available for the Pi afaik)

For the last step I only managed to install the ROS_comm version (without GUI-tools and robot-generic libs), but the procedure should be the same. 对于最后一步,我只设法安装ROS_comm版本(没有GUI工具和机器人通用库),但程序应该是相同的。

To conclude, it seems necessary to compile ROS from source with OpenCV already installed. 总而言之,似乎有必要从已安装OpenCV的源代码编译ROS。

After this, I am able to stream a videofile between the Pi 2 and my Virtual Machine using ROS´s image_transport and cv_bridge. 在此之后,我能够使用ROS的image_transport和cv_bridge在Pi 2和我的虚拟机之间传输视频文件。

Hope this helps someone with the same problem. 希望这可以帮助有同样问题的人。

I was also having essentially the same issue, probably due to multiple versions of OpenCV being linked with my binary. 我也有同样的问题,可能是因为OpenCV的多个版本与我的二进制文件链接。

I was able to resolve the segfault by cloning the vision_opencv repository into my catkin workspace. 我能够通过将vision_opencv存储库克隆到我的catkin工作区来解决segfault问题。

Then, my package explicitly requires OpenCV 3: 然后,我的包明确要求OpenCV 3:

find_package(OpenCV 3 REQUIRED)

This recompiles the cv_bridge package with OpenCV, and the resulting binary is only linked against OpenCV3 libraries. 这将使用OpenCV重新编译cv_bridge包,并且生成的二进制文件仅与OpenCV3库链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM