image_cb_detector.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2008-2012, Willow Garage, Inc.
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 the Willow Garage 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 
00036 
00037 #include <ros/console.h>
00038 #include <ros/ros.h>
00039 #include <cv_bridge/cv_bridge.h>
00040 #include <opencv2/imgproc/imgproc.hpp>
00041 #include <opencv2/calib3d/calib3d.hpp>
00042 #include <image_cb_detector/image_cb_detector.h>
00043 
00044 using namespace image_cb_detector;
00045 using namespace std;
00046 
00047 bool ImageCbDetector::configure(const ConfigGoal& config)
00048 {
00049   config_ = config;
00050   return true;
00051 }
00052 
00053 bool ImageCbDetector::detect(const sensor_msgs::ImageConstPtr& ros_image,
00054                              calibration_msgs::CalibrationPattern& result)
00055 {
00056   cv::Mat image;
00057   try
00058   {
00059     image = cv_bridge::toCvShare(ros_image, "mono8")->image;
00060   }
00061   catch (cv_bridge::Exception error)
00062   {
00063     ROS_ERROR("error: %s", error.what());
00064     return false;
00065   }
00066 
00067   // \todo This code has been pretty much copied from laser_cb_detector. (Wow... this is poor software design)
00068 
00069   // ***** Resize the image based on scaling parameters in config *****
00070   const int scaled_width  = (int) (.5 + image.cols  * config_.width_scaling);
00071   const int scaled_height = (int) (.5 + image.rows * config_.height_scaling);
00072   cv::Mat image_scaled;
00073 #if OPENCV3
00074   cv::resize(image, image_scaled, cv::Size(scaled_width, scaled_height), 0, 0, cv::INTER_LINEAR);
00075 #else
00076   cv::resize(image, image_scaled, cv::Size(scaled_width, scaled_height), 0, 0, CV_INTER_LINEAR);
00077 #endif
00078 
00079   // ***** Allocate vector for found corners *****
00080   vector<cv::Point2f> cv_corners;
00081   cv_corners.resize(config_.num_x*config_.num_y);
00082 
00083   // ***** Do the actual checkerboard extraction *****
00084   cv::Size board_size(config_.num_x, config_.num_y);
00085 #if OPENCV3
00086   int found = cv::findChessboardCorners( image_scaled, board_size, cv_corners, cv::CALIB_CB_ADAPTIVE_THRESH) ;
00087 #else
00088   int found = cv::findChessboardCorners( image_scaled, board_size, cv_corners, CV_CALIB_CB_ADAPTIVE_THRESH) ;
00089 #endif
00090 
00091   if(found)
00092   {
00093     ROS_DEBUG("Found CB");
00094     //ROS_INFO("Found checkerboard. Subpixel window: %i", config_.subpixel_window);
00095 
00096 
00097     cv::Size subpixel_window(config_.subpixel_window,
00098                                        config_.subpixel_window);
00099     cv::Size subpixel_zero_zone(config_.subpixel_zero_zone,
00100                                        config_.subpixel_zero_zone);
00101 
00102     // Subpixel fine-tuning stuff
00103     cv::cornerSubPix( image_scaled, cv_corners, 
00104                        subpixel_window,
00105                        subpixel_zero_zone,
00106 #if OPENCV3
00107                        cv::TermCriteria(cv::TermCriteria::MAX_ITER,20,1e-2));
00108 #else
00109                        cv::TermCriteria(CV_TERMCRIT_ITER,20,1e-2));
00110 #endif
00111   }
00112   else
00113     ROS_DEBUG("Didn't find CB");
00114 
00115   // ***** Downscale the detected corners and generate the CalibrationPattern message *****
00116   result.header.stamp    = ros_image->header.stamp;
00117   result.header.frame_id = ros_image->header.frame_id;
00118 
00119   result.object_points.resize(config_.num_x * config_.num_y);
00120   for (unsigned int i=0; i < config_.num_y; i++)
00121   {
00122     for (unsigned int j=0; j < config_.num_x; j++)
00123     {
00124       result.object_points[i*config_.num_x + j].x = j*config_.spacing_x;
00125       result.object_points[i*config_.num_x + j].y = i*config_.spacing_y;
00126       result.object_points[i*config_.num_x + j].z = 0.0;
00127     }
00128   }
00129 
00130   result.image_points.resize(cv_corners.size());
00131 
00132   for (size_t i=0; i < cv_corners.size(); i++)
00133   {
00134     result.image_points[i].x = cv_corners[i].x / config_.width_scaling;
00135     result.image_points[i].y = cv_corners[i].y / config_.height_scaling;
00136   }
00137 
00138   result.success = found;
00139 
00140   return true;
00141 }


image_cb_detector
Author(s): Vijay Pradeep, Eitan Marder-Eppstein
autogenerated on Wed Aug 26 2015 10:56:01