How to use opencv3 package with indigo

asked 2016-07-24 06:15:22 -0600

jesst gravatar image

updated 2016-07-24 06:17:18 -0600

I have segmentation fault when using cv_bridge with opencv3 package in ros indigo, i searched similar questions but cant find any proper solution, ı am trying to use cv_bridge without recompile with opencv3 package; and ı guess ı am not give right dependencies,

My CMakeList

find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  message_generation
  roscpp
  std_msgs
)

find_package(OpenCV 3 REQUIRED)
include_directories(
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

add_executable(kcf_algorithm src/kcf_algorithm.cpp)
target_link_libraries(kcf_algorithm ${OpenCV_LIBRARIES} ${catkin_LIBRARIES} )
add_dependencies(kcf_algorithm beginner_tutorials_gencpp)

My Package.xml

  <buildtool_depend>catkin</buildtool_depend>
   <build_depend>cv_bridge</build_depend> 
  <build_depend>image_transport</build_depend>
  <build_depend>message_generation</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>std_msgs</build_depend>
  <run_depend>cv_bridge</run_depend> 
  <run_depend>image_transport</run_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>std_msgs</run_depend>

And my code

#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>


#include <ros/ros.h>
#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/image_encodings.h>

using namespace std;
using namespace cv;


int flag = 0;
Rect2d roi;
Mat frame;
Ptr<Tracker> tracker = Tracker::create( "KCF" );
cv_bridge::CvImagePtr cv_ptr;


class kcf_class{
    image_transport::ImageTransport it_;
    image_transport::Subscriber image_sub_;
    image_transport::Publisher image_pub_;
    ros::NodeHandle nh_;

public:
      kcf_class()
        : it_(nh_)
      {
        // Subscrive to input video feed and publish output video feed
        image_sub_ = it_.subscribe("/camera/image_raw", 1,
          &kcf_class::imageCb, this);

      }

    void imageCb(const sensor_msgs::ImageConstPtr& msg)
    {
        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;
        }

          if(!cv_ptr->image.empty())
          {
              frame = cv_ptr->image;
              detectAndDisplay();
          }
    }



     void detectAndDisplay()
     {
         if( flag == 0)
         {
         roi=selectROI("tracker",frame);

         //quit if ROI was not selected
         if(roi.width==0 || roi.height==0)
             return;

         tracker->init(frame,roi);

         printf("Start the tracking process, press ESC to quit.\n");
         flag ++;
         }
         else {
             for ( ;; ){
                 if(frame.rows==0 || frame.cols==0)
                     break;
                 tracker->update(frame,roi);
                 rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
                 imshow("tracker",frame);
                 if(waitKey(1)==27)break;
             }
         }
         return ;
     }

};

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

    ros::init(argc,argv,"image_converter");
    kcf_class kcf;
    ros::spin();
    return 0;
}
edit retag flag offensive close merge delete