track_sequential.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (Modified BSD License)
00003  *
00004  *  Copyright (c) 2016, PAL Robotics, S.L.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of PAL Robotics, S.L. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00038 // ROS HEADERS
00039 #include <ros/ros.h>
00040 #include <cv_bridge/cv_bridge.h>
00041 #include <sensor_msgs/image_encodings.h>
00042 #include <sensor_msgs/Image.h>
00043 #include <image_transport/image_transport.h>
00044 
00045 // OpenCV headers
00046 #include <opencv2/objdetect/objdetect.hpp>
00047 #include <opencv2/imgproc/imgproc.hpp>
00048 #include <opencv2/highgui/highgui.hpp>
00049 
00050 class TrackSequential
00051 {
00052 public:
00053         TrackSequential(ros::NodeHandle nh_);
00054         ~TrackSequential();
00055         image_transport::ImageTransport _imageTransport;
00056         image_transport::Subscriber image_sub;
00057 
00058 protected:
00059         void imageCB(const sensor_msgs::ImageConstPtr& msg);
00060         void ImageProcessing();
00061         void ContourDetection(cv::Mat thresh_in, cv::Mat &output_);
00062 
00063 private:
00064         std::string win1, win2;
00065         cv::Mat img_bgr, img1, img2, thresh, diff;
00066         int i;
00067 };
00068 
00069 
00070 TrackSequential::TrackSequential(ros::NodeHandle nh_): _imageTransport(nh_)
00071 {
00072         win1 = "orig";
00073         win2 = "thresh diff";
00074         image_sub = _imageTransport.subscribe("xtion/rgb/image_raw", 1, &TrackSequential::imageCB, this, image_transport::TransportHints("compressed"));
00075         cv::namedWindow(win1, CV_WINDOW_AUTOSIZE);
00076         cv::namedWindow(win2, CV_WINDOW_AUTOSIZE);
00077         i=0;
00078 }
00079 
00080 TrackSequential::~TrackSequential()
00081 {
00082         cv::destroyWindow(win1);
00083         cv::destroyWindow(win2);
00084 }
00085 
00086 void TrackSequential::imageCB(const sensor_msgs::ImageConstPtr& msg)
00087 {
00088         cv_bridge::CvImagePtr cvPtr;
00089         try
00090         { 
00091                 cvPtr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
00092         }
00093         catch (cv_bridge::Exception& e) 
00094         {
00095                 ROS_ERROR("cv_bridge exception: %s", e.what());
00096                 return;
00097         }
00098         cvPtr->image.copyTo(img_bgr);
00099                 cv::cvtColor(cvPtr->image, img1, cv::COLOR_BGR2GRAY);
00100                 this->ImageProcessing();
00101 
00102         img1.copyTo(img2);
00103 }
00104 
00105 void TrackSequential::ImageProcessing()
00106 {
00107         if(i!=0)
00108         {       
00109                 cv::absdiff(img1,img2,diff);
00110                 cv::threshold(diff, thresh, 20, 255, cv::THRESH_BINARY);
00111                 cv::morphologyEx(thresh, thresh, 2, cv::getStructuringElement( 2, cv::Size(3, 3)));
00112                 this->ContourDetection(thresh, img_bgr);
00113                 cv::imshow(win1, img_bgr);
00114                 cv::imshow(win2, thresh);
00115         }
00116         ++i;
00117         cv::waitKey(1);
00118 }
00119 
00120 void TrackSequential::ContourDetection(cv::Mat thresh_in, cv::Mat &output_)
00121 {
00122         cv::Mat temp;
00123         cv::Rect objectBoundingRectangle = cv::Rect(0,0,0,0);
00124         thresh_in.copyTo(temp);
00125         std::vector<std::vector<cv::Point> > contours;
00126         std::vector<cv::Vec4i> hierarchy;
00127         cv::findContours(temp, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
00128         if(contours.size()>0)
00129         {
00130                 std::vector<std::vector<cv::Point> > largest_contour;
00131                 largest_contour.push_back(contours.at(contours.size()-1));
00132                 objectBoundingRectangle = cv::boundingRect(largest_contour.at(0));
00133                 int x = objectBoundingRectangle.x+objectBoundingRectangle.width/2;
00134                 int y = objectBoundingRectangle.y+objectBoundingRectangle.height/2;
00135                 cv::circle(output_,cv::Point(x,y),20,cv::Scalar(0,255,0),2);
00136         }
00137 }
00138 
00139 
00140 int main(int argc, char** argv)
00141 {
00142         ros::init(argc, argv, "TrackSequential");
00143         ros::NodeHandle nh;
00144         TrackSequential ts(nh);
00145         ros::spin();
00146 }


opencv_tut
Author(s): Job van Dieten
autogenerated on Fri Aug 26 2016 13:20:16