$search
00001 #!/usr/bin/env python 00002 ################################################################# 00003 ##\file 00004 # 00005 # \note 00006 # Copyright (c) 2011-2012 \n 00007 # Fraunhofer Institute for Manufacturing Engineering 00008 # and Automation (IPA) \n\n 00009 # 00010 ################################################################# 00011 # 00012 # \note 00013 # Project name: care-o-bot 00014 # \note 00015 # ROS stack name: cob_calibration 00016 # \note 00017 # ROS package name: cob_camera_calibration 00018 # 00019 # \author 00020 # Author: Sebastian Haug, email:sebhaug@gmail.com 00021 # \author 00022 # Supervised by: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de 00023 # 00024 # \date Date of creation: November 2011 00025 # 00026 ################################################################# 00027 # 00028 # Redistribution and use in source and binary forms, with or without 00029 # modification, are permitted provided that the following conditions are met: 00030 # 00031 # - Redistributions of source code must retain the above copyright 00032 # notice, this list of conditions and the following disclaimer. \n 00033 # - Redistributions in binary form must reproduce the above copyright 00034 # notice, this list of conditions and the following disclaimer in the 00035 # documentation and/or other materials provided with the distribution. \n 00036 # - Neither the name of the Fraunhofer Institute for Manufacturing 00037 # Engineering and Automation (IPA) nor the names of its 00038 # contributors may be used to endorse or promote products derived from 00039 # this software without specific prior written permission. \n 00040 # 00041 # This program is free software: you can redistribute it and/or modify 00042 # it under the terms of the GNU Lesser General Public License LGPL as 00043 # published by the Free Software Foundation, either version 3 of the 00044 # License, or (at your option) any later version. 00045 # 00046 # This program is distributed in the hope that it will be useful, 00047 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00048 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00049 # GNU Lesser General Public License LGPL for more details. 00050 # 00051 # You should have received a copy of the GNU Lesser General Public 00052 # License LGPL along with this program. 00053 # If not, see <http://www.gnu.org/licenses/>. 00054 # 00055 ################################################################# 00056 PKG = 'cob_camera_calibration' 00057 NODE = 'mono_calibration_node' 00058 import roslib; roslib.load_manifest(PKG) 00059 import rospy 00060 00061 import numpy as np 00062 from cob_camera_calibration import Checkerboard, CheckerboardDetector, MonoCalibrator, CalibrationData 00063 00064 class MonoCalibrationNode(): 00065 ''' 00066 Monocular Calibration ROS Node 00067 00068 Runs monocular calibration on a set of images. All settings are configurable via 00069 the ROS parameter server. 00070 ''' 00071 00072 def __init__(self): 00073 ''' 00074 Configures the calibration node 00075 00076 Reads configuration from parameter server or uses default values 00077 ''' 00078 rospy.init_node(NODE) 00079 print "==> started " + NODE 00080 00081 # get parameter from parameter server or set defaults 00082 self.folder = rospy.get_param('~folder', ".") 00083 self.pattern_size = rospy.get_param('~pattern_size', "9x6") 00084 self.square_size = rospy.get_param('~square_size', 0.03) 00085 00086 self.image_prefix = rospy.get_param('~image_prefix', "camera") 00087 self.camera_name = rospy.get_param('~camera_name', "camera") 00088 self.frame_id = rospy.get_param('~frame_id', "/camera") 00089 self.output_file = rospy.get_param('~output_file', self.camera_name+".yaml") 00090 00091 self.alpha = rospy.get_param('~alpha', 0.0) 00092 self.verbose = rospy.get_param('~verbose', False) 00093 00094 # split pattern_size string into tuple, e.g '9x6' -> tuple(9,6) 00095 self.pattern_size = tuple((int(self.pattern_size.split("x")[0]), int(self.pattern_size.split("x")[1]))) 00096 00097 def run_mono_calibration(self): 00098 ''' 00099 Runs the calibration 00100 ''' 00101 print "==> starting monocular calibration" 00102 00103 # set up Checkerboard, CheckerboardDetector and MonoCalibrator 00104 board = Checkerboard(self.pattern_size, self.square_size) 00105 detector = CheckerboardDetector(board) 00106 calibrator = MonoCalibrator(board, detector, self.folder, self.image_prefix) 00107 00108 # run calibration 00109 (rms, camera_matrix, projection_matrix, dist_coeffs, (h, w), _, _) = calibrator.calibrate_monocular_camera(self.alpha) 00110 print "==> successfully calibrated, reprojection RMS (in pixels):", rms 00111 00112 # create CalibrationData object with results 00113 camera_info = CalibrationData(self.camera_name, self.frame_id, w, h) 00114 camera_info.camera_matrix = camera_matrix 00115 camera_info.distortion_coefficients = dist_coeffs 00116 camera_info.projection_matrix = projection_matrix 00117 camera_info.dist_coeffs = dist_coeffs 00118 00119 # save results 00120 camera_info.save_camera_yaml_file(self.output_file) 00121 print "==> saved results to:", self.output_file 00122 00123 # verbose mode 00124 if self.verbose: 00125 print "--> results:" 00126 np.set_printoptions(suppress=1) 00127 print "camera matrix:\n", camera_matrix 00128 print "distortion coefficients:\n", dist_coeffs 00129 print "projection matrix:\n", projection_matrix 00130 00131 if __name__ == '__main__': 00132 node = MonoCalibrationNode() 00133 node.run_mono_calibration() 00134 print "==> done! exiting..."