基於ROS使用OpenCV進行影象的邊緣檢測
阿新 • • 發佈:2018-12-20
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話題。
下面的程式將使用這個話題資料。
2 程式
這個程式是對上篇教程的補充。。。對比下,可以更好的分析出一些有意思的東西。
#include <ros/ros.h> #include <image_transport/image_transport.h> #include <cv_bridge/cv_bridge.h> #include <sensor_msgs/image_encodings.h> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> 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檔案):
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》
<?xml version="1.0"?>
<package>
<name>cv_bridge_tutorial_pkg</name>
<version>0.0.0</version>
<description>The cv_bridge_tutorial_pkg package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
<maintainer email="[email protected]">lentin</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- Url tags are optional, but mutiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/cv_bridge_tutorial_pkg</url> -->
<!-- Author tags are optional, mutiple are allowed, one per tag -->
<!-- Authors do not have to be maintianers, but could be -->
<!-- Example: -->
<!-- <author email="[email protected]">Jane Doe</author> -->
<!-- The *_depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use run_depend for packages you need at runtime: -->
<!-- <run_depend>message_runtime</run_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>cv_bridge</build_depend>
<build_depend>image_transport</build_depend>
<build_depend>roscpp</build_depend>
<build_depend>sensor_msgs</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>sensor_msgs</run_depend>
<run_depend>std_msgs</run_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
3 執行
首先執行:roscore
然後:roslaunch usb_cam usb_cam-test.launch
最後:rosrun cv_bridge_tutorial_pkg sample_cv_bridge_node
結果如圖所示: