ros opencv segmantion fault [closed]

asked 2015-02-17 15:27:29 -0600

Maesltrom gravatar image

updated 2015-02-17 16:05:54 -0600

Hi guys! I'm trying to using opencv with ros-indigo. Communication between opencv and ros are fine but I'm getting stuck in retrieving images from /ardrone/image_raw topic from tum_simulator. Here's my code:


#include <stdio.h>
#include <iostream>
#include "ros/ros.h"
#include "std_msgs/Empty.h"
#include "std_msgs/String.h"
#include <image_transport/image_transport.h>
#include <sensor_msgs/image_encodings.h>
#include "cv_bridge/cv_bridge.h"
#include "geometry_msgs/Twist.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

ros::Publisher pub_takeoff, pub_land, pub_vel;

void imageCb(const sensor_msgs::ImageConstPtr& msg);

int main( int argc, char **argv ){

    cv::namedWindow("Teste", 1);

    ros::init( argc, argv, "ardrone_camera" );
    ros::NodeHandle nh_;

    ros::NodeHandle n;
    image_transport::ImageTransport it(n);
    image_transport::Subscriber image_sub = it.subscribe("/ardrone/image_raw", 1, &imageCb);

    pub_takeoff = nh_.advertise<std_msgs::Empty>("/ardrone/takeoff",1, true);
    pub_land = nh_.advertise<std_msgs::Empty>("/ardrone/land",1, true);
    pub_vel = nh_.advertise<geometry_msgs::Twist>("/cmd_vel",1);

    int key = 0;

    while( key != 27 ){
        std::cout << image_sub.getNumPublishers() << std::endl;

        if ( (char)key == 'l' ){
            pub_land.publish( std_msgs::Empty());
        }
        else if( (char)key == 't' ) pub_takeoff.publish( std_msgs::Empty());

        // Get a key pressed by user
        key = cv::waitKey( 30 );
        if ((char)key == 'q' || (char)key == 27) break;
    }

    //system( "rostopic pub /ardrone/land std_msgs/Empty");
    printf("End");
    return 0;
}

void imageCb(const sensor_msgs::ImageConstPtr& msg){
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
        cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("cv_bridge exception: %s", e.what());
        return;
    }
//  cv::imshow("Teste", cv_ptr->image);
//  cv::waitKey(3);
/*
    // Draw an example circle on the video stream
    if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
        cv::circle(cv_ptr->image, cv::Point(50, 50), 10, cv::Scalar(255,0,0));
    // Update GUI Window
    cv::imshow("Teste", cv_ptr->image);
    cv::waitKey(3);*/
}

If i uncomment the line "// cv::imshow("Teste", cv_ptr->image); // cv::waitKey(3);"

I get segmentation fault (core dumped) erro. But it is strange due i putted a cout command inside the image callback and no messages were printed out. The output of "std::cout << image_sub.getNumPublishers() << std::endl;" is 1 what means that i have a connection with this topic.

The same problem occur with the example from ros page: http://wiki.ros.org/cv_bridge/Tutoria... . Compile but nothing happen when I run the program

What you guy think about it?

I'm already built a program to get the camera data and process with opencv using python :D

Thanks!

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by tfoote
close date 2018-01-11 20:13:50.541456

Comments

Using opencv's GUI functions in ros nodes can be tricky because they must be accessed by only one thread... Can you cv::imwrite("Teste.png", cv_ptr->image); in that postion where the seg fault happens?

Wolf gravatar image Wolf  ( 2015-02-18 01:11:28 -0600 )edit

Hi Wolf! The same occurs with cv::imwrite. When using eclipse to run code, nothing happen but when I run the program manually from terminal the segmentation fault message error appear.

Thanks answering!

Maesltrom gravatar image Maesltrom  ( 2015-02-18 05:38:31 -0600 )edit

I don't know why Tfoote close this topic because don't have solution.. and is not outdated!!! Because i have the same problem!!!

barovehicles gravatar image barovehicles  ( 2018-02-05 08:11:08 -0600 )edit