$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 00057 import yaml 00058 import numpy as np 00059 00060 class CalibrationData: 00061 ''' 00062 Calibration data class stores intrinsic camera parameters 00063 and some additional parameters (name and frame id) 00064 00065 Provides methods to save and read parameters from yaml file. 00066 ''' 00067 00068 def __init__(self, camera_name, frame_id, image_width, image_height): 00069 ''' 00070 Initialize object 00071 ''' 00072 self.camera_name = camera_name 00073 self.frame_id = frame_id 00074 self.image_width = image_width 00075 self.image_height = image_height 00076 00077 # initialize matrices 00078 self.camera_matrix = np.identity(3) 00079 self.rectification_matrix = np.identity(3) 00080 self.projection_matrix = np.hstack((np.identity(3), np.zeros((3,1)))) 00081 self.distortion_model = "plumb_bob" 00082 self.distortion_coefficients = np.zeros((1,5)) 00083 00084 # # DEBUG 00085 # print self.camera_matrix 00086 # print self.rectification_matrix 00087 # print self.projection_matrix 00088 # print self.distortion_model 00089 # print self.distortion_coefficients 00090 00091 00092 def read_camera_yaml_file(self, filename): 00093 ''' 00094 Load camera calibration info from the yaml file located at 'filename'. 00095 ''' 00096 calib = yaml.load(file(filename)) 00097 00098 # checks 00099 assert calib['camera_matrix']['rows'] == 3 00100 assert calib['camera_matrix']['cols'] == 3 00101 assert calib['rectification_matrix']['rows'] == 3 00102 assert calib['rectification_matrix']['cols'] == 3 00103 assert calib['projection_matrix']['rows'] == 3 00104 assert calib['projection_matrix']['cols'] == 4 00105 assert calib['distortion_model'] == 'plumb_bob' 00106 assert calib['distortion_coefficients']['rows'] == 1 00107 assert calib['distortion_coefficients']['cols'] == 5 00108 00109 # import 00110 self.camera_name = calib['camera_name'] 00111 self.frame_id = calib['frame_id'] 00112 self.camera_matrix = np.matrix(calib['camera_matrix']['data']).reshape((3,3)) 00113 self.rectification_matrix = np.matrix(calib['rectification_matrix']['data']).reshape((3,3)) 00114 self.projection_matrix = np.matrix(calib['projection_matrix']['data']).reshape((3,4)) 00115 self.distortion_model = calib['distortion_model'] 00116 self.distortion_coefficients = np.matrix(calib['distortion_coefficients']['data']).reshape((1,5)) 00117 self.image_width = calib['image_width'] 00118 self.image_height = calib['image_height'] 00119 pass 00120 00121 def save_camera_yaml_file(self, filename): 00122 ''' 00123 Save camera calibration info to the yaml file located at 'filename'. 00124 ''' 00125 open(filename, "w").writelines(self._as_yaml_string()) 00126 00127 def _as_yaml_string(self): 00128 ''' 00129 Convert calibration data to yaml string. 00130 ''' 00131 return yaml_template % (self.camera_name, 00132 self.frame_id, 00133 self.image_width, 00134 self.image_height, 00135 np.array(self.camera_matrix).flatten().tolist(), 00136 np.array(self.distortion_coefficients).flatten().tolist(), 00137 np.array(self.rectification_matrix).flatten().tolist(), 00138 np.array(self.projection_matrix).flatten().tolist()) 00139 00140 yaml_template='''camera_name: %s 00141 frame_id: %s 00142 image_width: %d 00143 image_height: %d 00144 camera_matrix: 00145 rows: 3 00146 cols: 3 00147 data: %s 00148 distortion_model: plumb_bob 00149 distortion_coefficients: 00150 rows: 1 00151 cols: 5 00152 data: %s 00153 rectification_matrix: 00154 rows: 3 00155 cols: 3 00156 data: %s 00157 projection_matrix: 00158 rows: 3 00159 cols: 4 00160 data: %s 00161 '''