opencv实践-ROS+opencv打开USB相机做边缘检测

Posted 殇堼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv实践-ROS+opencv打开USB相机做边缘检测相关的知识,希望对你有一定的参考价值。

功能包资源下载:http://www.hzcourse.com/oep/image/ueditor/jsp/upload/file/20190812/62199-精通ROS机器人编程(原书第2版)_配书资源.rar

一、导入和编译功能包

方法1:

cd ~/catkin_ws/src  
git clone https://github.com/bosch-ros-pkg/usb_cam.git  
cd ~/catkin_ws  
catkin_make  
echo "source /home/zjc/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

方法2:
1.创建一个名为cv_bridge_tutorial_pkg的工作空间

catkin_create_pkg cv_bridge_tutorial_pkg sensor_msgs cv_bridge roscpp std_msgs image_transport

2.编译

catkin_make

二、启动相机进行边缘检测

终端1:

roslaunch usb_cam usb_cam-test.launch 

终端2:

rosrun cv_bridge_tutorial_pkg sample_cv_bridge_node

三、修改代码

源代码解读:

#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>

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_)
  {
    image_sub_ = it_.subscribe("/usb_cam/image_raw", 1,  
      &Edge_Detector::imageCb, this);//订阅输入视频
    image_pub_ = it_.advertise("/edge_detector/raw_image", 1); //发布输出视频
  }
  
    if (cv_ptr->image.rows > 400 && cv_ptr->image.cols > 600)//检测输入图像的是否满足尺寸要求,满足将会执行边缘检测的函数
   {
    detect_edges(cv_ptr->image);
    image_pub_.publish(cv_ptr->toImageMsg());//opencv图像转化为ROS图像,并进行发布。
	}
  }
  
//使用cv_bridge将opencv转化为ROS图像
 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;
    }
    
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("Edge Detection", dst);
    cv::waitKey(3);
  }	
};

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

在修改sample_cv_bridge_node.cpp中的代码后需要进行一次编译,以更新sample_cv_bridge_node文件,且该节点文件存放在/home/zjc/catkin_make/devel/lib/cv_bridge_tutorial_pkg目录下

以上是关于opencv实践-ROS+opencv打开USB相机做边缘检测的主要内容,如果未能解决你的问题,请参考以下文章

opencv学习-实战篇-ROS18.04+Opencv-相机标定+物体识别(持续更新)

ROS+opencv实践-cv_bridge的使用

ROS+opencv实践-cv_bridge的使用

在 Python 中将 USB 视频捕获设备友好名称与 OpenCV 端口号相关联

ubuntu20 ros使用usb相机

ubuntu20 ros使用usb相机