基于ROS使用OpenCV进行图像的边缘检测

1 硬件

摄像头:1个;

USB数据线:1个;

1.1 准备

这里需要使用usb-cam软件包:

$ cd ~/catkin_ws/src  
$ git clone https://github.com/bosch-ros-pkg/usb_cam.git  
$ cd ~/catkin_ws  
$ catkin_make  

 注意:下面的程序运行前,需要启动usb_cam节点。

roslaunch usb_cam usb_cam-test.launch

运行上面的节点,这时该节点会不断的发布/usb_cam/image_raw话题。

基于ROS使用OpenCV进行图像的边缘检测_第1张图片

下面的程序将使用这个话题数据。

2 程序

这个程序是对上篇教程的补充。。。对比下,可以更好的分析出一些有意思的东西。

#include 
#include 
#include 
#include 
#include 
#include 

static const std::string OPENCV_WINDOW = "Raw Image window";
static const std::string OPENCV_WINDOW_1 = "Edge Detection";

class Edge_Detector
{
  ros::NodeHandle nh_;
  image_transport::ImageTransport it_;
  image_transport::Subscriber image_sub_;
  image_transport::Publisher image_pub_;
  
public:
  Edge_Detector()
    : it_(nh_)
  {
    // Subscribe to input video feed and publish output video feed
    image_sub_ = it_.subscribe("/usb_cam/image_raw", 1, 
      &Edge_Detector::imageCb, this);
    image_pub_ = it_.advertise("/edge_detector/raw_image", 1);
    cv::namedWindow(OPENCV_WINDOW);

  }

  ~Edge_Detector()
  {
    cv::destroyWindow(OPENCV_WINDOW);
  }

//回调函数
  void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {

    cv_bridge::CvImagePtr cv_ptr;
    namespace enc = sensor_msgs::image_encodings;

    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;
    }

    // Draw an example circle on the video stream
    if (cv_ptr->image.rows > 400 && cv_ptr->image.cols > 600){

	detect_edges(cv_ptr->image);
    	image_pub_.publish(cv_ptr->toImageMsg());

	}
  }
  //OpenCV的边缘检测程序
  void detect_edges(cv::Mat img)
  {

   	cv::Mat src, src_gray;
	cv::Mat dst, detected_edges;

	int edgeThresh = 1;
	int lowThreshold = 200;
	int highThreshold =300;
	int kernel_size = 5;

	img.copyTo(src);

	cv::cvtColor( img, src_gray, CV_BGR2GRAY );
        cv::blur( src_gray, detected_edges, cv::Size(5,5) );
	cv::Canny( detected_edges, detected_edges, lowThreshold, highThreshold, kernel_size );

  	dst = cv::Scalar::all(0);
  	img.copyTo( dst, detected_edges);
	dst.copyTo(img);

    	cv::imshow(OPENCV_WINDOW, src);
    	cv::imshow(OPENCV_WINDOW_1, dst);
    	cv::waitKey(3);

  }	
 
};

int main(int argc, char** argv)
{
  ros::init(argc, argv, "Edge_Detector");
  Edge_Detector ic;
  ros::spin();
  return 0;
}

 2.1 CMakeLists.txt文件

这个很重要,以后再写程序可以直接参考这个文件:

如果有不懂的可以参考ROS学习笔记15(ROS/CMakeLists.txt文件):

https://blog.csdn.net/qq_27806947/article/details/82709620

cmake_minimum_required(VERSION 2.8.3)
project(cv_bridge_tutorial_pkg)

find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  sensor_msgs
  std_msgs
  )

find_package( OpenCV REQUIRED )

catkin_package()

include_directories(
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

add_executable(sample_cv_bridge_node src/sample_cv_bridge_node.cpp)

target_link_libraries(sample_cv_bridge_node
   ${catkin_LIBRARIES}
   ${OpenCV_LIBRARIES}
 )

include_directories(${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})

2.3 package.xml文件

不懂得也可以参考《ROS学习笔记6(package.xml》

https://blog.csdn.net/qq_27806947/article/details/82084707



  cv_bridge_tutorial_pkg
  0.0.0
  The cv_bridge_tutorial_pkg package

  lentin

  TODO


  catkin
  cv_bridge
  image_transport
  roscpp
  sensor_msgs
  std_msgs
  cv_bridge
  image_transport
  roscpp
  sensor_msgs
  std_msgs


  
  

3 运行

首先运行:roscore

然后:roslaunch usb_cam usb_cam-test.launch

最后:rosrun cv_bridge_tutorial_pkg sample_cv_bridge_node

结果如图所示:

基于ROS使用OpenCV进行图像的边缘检测_第2张图片

 

 

你可能感兴趣的:(PLAY)