Ros_OpenCV_cascade分类器检测程序

41 篇文章 1 订阅
    //Includes all the headers necessary to use the most common public pieces of the ROS system.  
    #include <ros/ros.h>  
    //Use image_transport for publishing and subscribing to images in ROS  
    #include <image_transport/image_transport.h>  
    //Use cv_bridge to convert between ROS and OpenCV Image formats  
    #include <cv_bridge/cv_bridge.h>  
    #include "opencv2/objdetect/objdetect.hpp"  
    #include <sensor_msgs/image_encodings.h>  
    //Include headers for OpenCV Image processing  
    #include <opencv2/imgproc/imgproc.hpp>  
    //Include headers for OpenCV GUI handling  
    #include <opencv2/highgui/highgui.hpp>  
    #include<string>      
    #include <sstream>  
    using namespace cv;  
    using namespace std;  
      
    //Store all constants for image encodings in the enc namespace to be used later.    
    namespace enc = sensor_msgs::image_encodings;    

    //char *output_file = "/home/hsn/catkin_ws/src/rosopencv";  

 /** Function Headers */
 void detectAndDisplay( Mat frame );

 /** Global variables */
 String cascade_name = "/home/hsn/catkin_ws/src/rosopencv/cascadeStage8.xml";
 
 CascadeClassifier cascade;
 
 string window_name = "Capture - Face detection";
 
/** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
  std::vector<Rect> faces;
  Mat frame_gray;

  cvtColor( frame, frame_gray, CV_BGR2GRAY );
  equalizeHist( frame_gray, frame_gray );
  if( !cascade.load( cascade_name ) )
  {
     printf("--(!)Error loading\n");
  // return -1; 
  };
    //-- Detect faces
  cascade.detectMultiScale( frame_gray, faces, 2, 20, 0|CASCADE_SCALE_IMAGE, Size(120, 160) ,Size(300, 400));

  for( size_t i = 0; i < faces.size(); i++ )
  {
    Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
    ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

  }
  //-- Show what you got
  imshow( window_name, frame );
 }      
      
    //This function is called everytime a new image is published  
    void imageCallback(const sensor_msgs::ImageConstPtr& original_image)  
    {  
        //Convert from the ROS image message to a CvImage suitable for working with OpenCV for processing  
        cv_bridge::CvImagePtr cv_ptr;    
        try    
        {    
            //Always copy, returning a mutable CvImage    
            //OpenCV expects color images to use BGR channel order.    
            cv_ptr = cv_bridge::toCvCopy(original_image, enc::BGR8);    
        }    
        catch (cv_bridge::Exception& e)    
        {    
            //if there is an error during conversion, display it    
            ROS_ERROR("tutorialROSOpenCV::main.cpp::cv_bridge exception: %s", e.what());    
            return;    
        }    
       
       Mat img=  cv_ptr->image;
       if( !img.empty() )
       { 
	  detectAndDisplay( img );
       }
       else
       { 
         printf(" --(!) No captured frame -- Break!");
        // break;
       }

       int c = waitKey(10);
       if( (char)c == 'c' )
       { 
          ROS_INFO("Exit boss");//break;  
       }
    }  
      
    /** 
    * This is ROS node to track the destination image 
    */  
    int main(int argc, char **argv)  
    {  
        ros::init(argc, argv, "image_socket");  
        ROS_INFO("-----------------");  
          
        ros::NodeHandle nh;  
      
        image_transport::ImageTransport it(nh);  
      
        image_transport::Subscriber sub = it.subscribe("camera/rgb/image_raw", 1, imageCallback);  
      
        ros::spin();  
      
        //ROS_INFO is the replacement for printf/cout.  
        ROS_INFO("tutorialROSOpenCV::main.cpp::No error.");  
    }  

注解:

C++: void CascadeClassifier::detectMultiScale(const Mat& image, vector<Rect>& objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())

Parameters:

  • cascade – Haar classifier cascade (OpenCV 1.x API only). It can be loaded from XML or YAML file using Load(). When the cascade is not needed anymore, release it using cvReleaseHaarClassifierCascade(&cascade).
  • image – Matrix of the type CV_8U containing an image where objects are detected.
  • objects – Vector of rectangles where each rectangle contains the detected object.
  • scaleFactor – Parameter specifying how much the image size is reduced at each image scale.
  • minNeighbors – Parameter specifying how many neighbors each candidate rectangle should have to retain it.
  • flags – Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.
  • minSize – Minimum possible object size. Objects smaller than that are ignored.
  • maxSize – Maximum possible object size. Objects larger than that are ignored.
http://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值