cv_bridge ROS image segmentation fault core dumped
Hi,
I'm trying to pull a video feed from rviz into OpenCV to do some real-time image processing on it. For this I publish the rviz video feed onto a ROS topic: /camerawebcam/rgb/imageraw
I'm using:
- Ubuntu 14.04
- ROS Indigo
- OpenCV 3.1
- Rviz 1.11.14
I wrote a ROS node, named talker, to pull in this video feed an do some processing on it. When using the capture function in combination with a webcam feed or a pre-recorded video everything works fine. But now I need to convert the ROS image fro mthe ROS topic to a Mat in order to be able to process it. To do this I wrote a imageCallback function containing the cv_bridge::toCvCopy() function one requires to do this.
All relevant code is listed below.
CMakelist.txt:
cmake_minimum_required(VERSION 2.8.3)
project(camera_corridor_ROS_OpenCV)
find_package(
catkin REQUIRED COMPONENTS
roscpp
std_msgs
rospy
cv_bridge
image_transport
message_generation)
# don't use opencv as found by any prior running of find_package
unset(OpenCV_CONFIG_PATH CACHE)
unset(OpenCV_DIR CACHE)
set(TMP_PREFIX_PATH ${CMAKE_PREFIX_PATH})
set(CMAKE_PREFIX_PATH "$ENV{HOME}/opencv-3.1.0")
find_package(OpenCV 3.0 REQUIRED)
# restore CMAKE_PREFIX_PATH and other cached variables
# so nothing other package finds this opencv
set(CMAKE_PREFIX_PATH ${TMP_PREFIX_PATH})
unset(OpenCV_CONFIG_PATH CACHE)
unset(OpenCV_DIR CACHE)
add_message_files(
FILES
Corridor_vanish.msg
)
generate_messages(
DEPENDENCIES
std_msgs # Or other packages containing msgs
)
catkin_package(
CATKIN_DEPENDS message_runtime std_msgs cv_bridge image_transport
)
include_directories(${catkin_INCLUDE_DIRS} include ${OpenCV_INCLUDE_DIRS})
add_library(talker2 src/KalmanDHU.cpp)
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
target_link_libraries(talker ${OpenCV_LIBRARIES})
target_link_libraries(talker2 ${OpenCV_LIBRARIES})
target_link_libraries(talker talker2)
Package.xml:
<?xml version="1.0"?>
<package>
<name>camera_corridor_ROS_OpenCV</name>
<version>0.0.0</version>
<description>The camera_corridor_ROS_OpenCV package</description>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>image_transport</build_depend>
<build_depend>roscpp</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>cv_bridge</build_depend>
<build_depend>uvc_camera</build_depend>
<build_depend>sensor_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_depend>rospy</build_depend>
<run_depend>image_transport</run_depend>
<run_depend>roscpp</run_depend>
<run_depend>std_msgs</run_depend>
<run_depend>cv_bridge</run_depend>
<run_depend>uvc_camera</run_depend>
<run_depend>sensor_msgs</run_depend>
<run_depend>message_runtime</run_depend>
<export>
</export>
</package>
My main code:
//Includes all the headers necessary to use the most common public pieces of the ROS system.
#include <ros/ros.h>
//Use image_transport for publishing and subscribing to images in ROS
#include <image_transport/image_transport.h>
//Use cv_bridge to convert between ROS and OpenCV Image formats
#include <cv_bridge/cv_bridge.h>
//Include some useful constants for image encoding. Refer to: http://www.ros.org/doc/api/sensor_msgs/html/namespacesensor__msgs_1_1image__encodings.html for more info.
#include <sensor_msgs/image_encodings.h>
//Include headers for OpenCV Image processing
#include <opencv2/imgproc/imgproc.hpp>
//Include headers for OpenCV GUI handling
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include "std_msgs/String.h"
#include <sstream>
#include <iostream>
#include "KalmanDHU.h"
#include <camera_corridor_ROS_OpenCV/Corridor_vanish.h>
//Store all constants for image encodings in the enc namespace to be used later.
namespace enc = sensor_msgs::image_encodings;
using namespace std;
using namespace cv;
void imageCallback(const sensor_msgs::ImageConstPtr& original_image);
void ProcessFrame(Mat inputframe);
bool intersection(Point2f o1, Point2f p1, Point2f o2, Point2f p2,Point2f &r);
Point2f pointVanish,pointLeft,pointRight,pointMiddle;
bool corridorFound = false;
vector<Point2f> intersectvector;
int framecounter = 0;
int framesgood = 0;
int frame_last_good=0;
float theta_lim_1 = 0.35;
float theta_lim_2 = 1.20;
float theta_lim_3 = 1.90;
float theta_lim_4 = 2.70;
cv_bridge::CvImagePtr cv_ptr;
Mat source_img;
/**
The code processes a corridor real-time video feed to find perspective lines and a vanishing point for navigational purposes
*/
int main(int argc, char **argv)
{
ros::init(argc, argv, "image_processor");
ros::NodeHandle nh;
ros::Subscriber chatter_sub = nh.subscribe("/camera_webcam/rgb/image_raw", 1, imageCallback);
ros::Publisher chatter_pub = nh.advertise<camera_corridor_ROS_OpenCV::Corridor_vanish>("corridor_vanish", 10);
ros::Rate loop_rate(15);
//Export frames individually
Mat frame;
frame = source_img.clone();
while (ros::ok())
{
IMAGE PROCESSING DONE HERE
chatter_pub.publish(msg);
}
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
The imageCallback function:
void imageCallback(const sensor_msgs::ImageConstPtr& original_image)
{
try
{
//Always copy, returning a mutable CvImage
//OpenCV expects color images to use BGR channel order.
cv_ptr = cv_bridge::toCvCopy(original_image, sensor_msgs::image_encodings::BGR8);
}
catch (cv_bridge::Exception& e)
{
//if there is an error during conversion, display it
ROS_ERROR("obstacle_avoidance::main.cpp::cv_bridge exception: %s", e.what());
return;
}
source_img = cv_ptr->image;
}
Compilation goes well. But when I try to run the ROS node talker containing this code I get an instant segmentation fault without any further information:
jon@jon-werk ~ $ rosrun --debug camera_corridor_ROS_OpenCV talker
[rosrun] Looking in catkin libexec dirs: /home/jon/indigo_ws/devel/lib/camera_corridor_ROS_OpenCV
[rosrun] Looking in rospack dir: /home/jon/indigo_ws/src/itasc_quad_collision/camera_corridor_ROS_OpenCV
[rosrun] Searching for talker with permissions /111
[rosrun] Running /home/jon/indigo_ws/devel/lib/camera_corridor_ROS_OpenCV/talker
Segmentation fault (core dumped)
I don't know the reason for this. I've read on other fora that it might be due to me using OpenCV 3.1 while cv_bridge for ROS Indigo was intended for OpenCv 2.4, but I'm not sure about this.
Any ideas?
Kind regards,
Jon
Asked by Jon Verbeke on 2016-07-27 02:24:32 UTC
Comments
Hi,
Maybe I'm wrong but it looks like you use source_img unitialized!
Also you should take a look at the image transport package as it can handle videostream http://wiki.ros.org/image_transport.
Nico
Asked by Nico__ on 2016-07-28 00:55:53 UTC