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   cv::resize(image, image_scaled, cv::Size(scaled_width, scaled_height), 0, 0, cv::INTER_LINEAR);
00074 
00075   // ***** Allocate vector for found corners *****
00076   vector<cv::Point2f> cv_corners;
00077   cv_corners.resize(config_.num_x*config_.num_y);
00078 
00079   // ***** Do the actual checkerboard extraction *****
00080   cv::Size board_size(config_.num_x, config_.num_y);
00081   int found = cv::findChessboardCorners( image_scaled, board_size, cv_corners, cv::CALIB_CB_ADAPTIVE_THRESH) ;
00082 
00083   if(found)
00084   {
00085     ROS_DEBUG("Found CB");
00086     //ROS_INFO("Found checkerboard. Subpixel window: %i", config_.subpixel_window);
00087 
00088 
00089     cv::Size subpixel_window(config_.subpixel_window,
00090                                        config_.subpixel_window);
00091     cv::Size subpixel_zero_zone(config_.subpixel_zero_zone,
00092                                        config_.subpixel_zero_zone);
00093 
00094     // Subpixel fine-tuning stuff
00095     cv::cornerSubPix( image_scaled, cv_corners, 
00096                        subpixel_window,
00097                        subpixel_zero_zone,
00098                        cv::TermCriteria(cv::TermCriteria::MAX_ITER,20,1e-2));
00099   }
00100   else
00101     ROS_DEBUG("Didn't find CB");
00102 
00103   // ***** Downscale the detected corners and generate the CalibrationPattern message *****
00104   result.header.stamp    = ros_image->header.stamp;
00105   result.header.frame_id = ros_image->header.frame_id;
00106 
00107   result.object_points.resize(config_.num_x * config_.num_y);
00108   for (unsigned int i=0; i < config_.num_y; i++)
00109   {
00110     for (unsigned int j=0; j < config_.num_x; j++)
00111     {
00112       result.object_points[i*config_.num_x + j].x = j*config_.spacing_x;
00113       result.object_points[i*config_.num_x + j].y = i*config_.spacing_y;
00114       result.object_points[i*config_.num_x + j].z = 0.0;
00115     }
00116   }
00117 
00118   result.image_points.resize(cv_corners.size());
00119 
00120   for (size_t i=0; i < cv_corners.size(); i++)
00121   {
00122     result.image_points[i].x = cv_corners[i].x / config_.width_scaling;
00123     result.image_points[i].y = cv_corners[i].y / config_.height_scaling;
00124   }
00125 
00126   result.success = found;
00127 
00128   return true;
00129 }


image_cb_detector
Author(s): Vijay Pradeep, Eitan Marder-Eppstein
autogenerated on Tue Sep 27 2016 04:06:37