00001
00002
00003 import time
00004 from random import *
00005
00006 import roslib
00007 roslib.load_manifest('cob_script_server')
00008 import rospy
00009
00010 import tf
00011 from tf.transformations import euler_from_quaternion
00012
00013 import cv
00014 from sensor_msgs.msg import Image
00015 from cv_bridge import CvBridge, CvBridgeError
00016 from math import *
00017
00018
00019 from simple_script_server import script
00020
00021 class CalibCam(script):
00022
00023 def Initialize(self):
00024 self.bridge = CvBridge()
00025 self.image_sub_r = rospy.Subscriber("/stereo/right/image_color",Image,self.callback_r)
00026 self.cv_image_r = cv.CreateImage((1,1), 1 , 3)
00027 self.image_sub_l = rospy.Subscriber("/stereo/left/image_color",Image,self.callback_l)
00028 self.cv_image_l = cv.CreateImage((1,1), 1 , 3)
00029 self.image_sub_3d = rospy.Subscriber("/cam3d/rgb/image_color",Image,self.callback_3d)
00030 self.cv_image_3d = cv.CreateImage((1,1), 1 , 3)
00031 self.sss.init("head")
00032 self.sss.init("torso")
00033 self.sss.init("sdh")
00034 self.image_number_offset = 1
00035 self.listener = tf.TransformListener()
00036 self.file_path = "./"
00037
00038 def Run(self):
00039 print "start"
00040 seed()
00041 maxVal_pan = 0.125
00042 maxVal_tilt = 0.20
00043 nr_images = 12
00044
00045
00046
00047
00048
00049
00050 self.sss.move("head","back")
00051
00052
00053 self.sss.move("torso","home")
00054
00055
00056 self.sss.wait_for_input()
00057
00058
00059
00060 for j in range(1,4):
00061 if (j==1):
00062
00063 print "Move checkerboard to a close distance and strike any key when ready."
00064 self.sss.wait_for_input()
00065 maxVal_pan = 0.125
00066 maxVal_tilt = 0.15
00067 elif (j==2):
00068
00069 print "Move checkerboard to an intermediate distance and strike any key when ready."
00070 self.sss.wait_for_input()
00071 maxVal_pan = 0.125
00072 maxVal_tilt = 0.20
00073 elif (j==3):
00074
00075 print "Move checkerboard to a far distance and strike any key when ready."
00076 self.sss.wait_for_input()
00077 maxVal_pan = 0.125
00078 maxVal_tilt = 0.20
00079
00080
00081 for i in range(1,nr_images+1):
00082 if i==1:
00083 r1 = maxVal_pan
00084 r2 = maxVal_tilt
00085 elif i==2:
00086 r1 = 0
00087 r2 = maxVal_tilt
00088 elif i==3:
00089 r1 = -maxVal_pan
00090 r2 = maxVal_tilt
00091 elif i==4:
00092 r1 = -maxVal_pan
00093 r2 = 0
00094 elif i==5:
00095 r1 = 0
00096 r2 = 0
00097 elif i==6:
00098 r1 = maxVal_pan
00099 r2 = 0
00100 elif i==7:
00101 r1 = maxVal_pan
00102 r2 = -maxVal_tilt
00103 elif i==8:
00104 r1 = 0
00105 r2 = -maxVal_tilt
00106 elif i==9:
00107 r1 = -maxVal_pan
00108 r2 = -maxVal_tilt
00109 else:
00110 r1 = (random()-0.5)*2*maxVal_pan;
00111 r2 = (random()-0.5)*2*maxVal_tilt;
00112 self.sss.move("torso",[[1./3.*r1,1./3.*r2,2./3.*r1,2./3.*r2]])
00113 self.sss.sleep(1)
00114 if not self.sss.parse:
00115 self.captureImage()
00116
00117 self.sss.move("torso","home")
00118 print "Prepare checkerboard for further views and hit <Enter> for each image capture. Press any other key + <Enter> to finish.\n"
00119
00120 key = self.sss.wait_for_input()
00121 if not self.sss.parse:
00122 while (key==''):
00123 self.captureImage()
00124 key = self.sss.wait_for_input()
00125
00126 print "finished"
00127
00128 def captureImage(self):
00129 try:
00130 (trans,rot) = self.listener.lookupTransform('/base_link', '/head_axis_link', rospy.Time(0))
00131 rpy = euler_from_quaternion(rot)
00132 cyaw = cos(rpy[2])
00133 syaw = sin(rpy[2])
00134 cpitch = cos(rpy[1])
00135 spitch = sin(rpy[1])
00136 croll = cos(rpy[0])
00137 sroll = sin(rpy[0])
00138 R11 = cyaw*cpitch
00139 R12 = cyaw*spitch*sroll-syaw*croll
00140 R13 = cyaw*spitch*croll+syaw*sroll
00141 R21 = syaw*cpitch
00142 R22 = syaw*spitch*sroll+cyaw*croll
00143 R23 = syaw*spitch*croll-cyaw*sroll
00144 R31 = -spitch
00145 R32 = cpitch*sroll
00146 R33 = cpitch*croll
00147 fout = open(self.file_path+'calpic'+str(self.image_number_offset)+'.coords','w')
00148 fout.write(str(R11)+' '+str(R12)+' '+str(R13)+' '+str(trans[0]*1000)+'\n'+str(R21)+' '+str(R22)+' '+str(R23)+' '+str(trans[1]*1000)+'\n'+str(R31)+' '+str(R32)+' '+str(R33)+' '+str(trans[2]*1000))
00149 fout.close()
00150 except (tf.LookupException, tf.ConnectivityException):
00151 print "tf exception"
00152
00153 self.sss.sleep(1)
00154 cv.SaveImage(self.file_path+'right_'+str(self.image_number_offset)+'.png',self.cv_image_r)
00155 cv.SaveImage(self.file_path+'left_'+str(self.image_number_offset)+'.png',self.cv_image_l)
00156 cv.SaveImage(self.file_path+'cam3d_'+str(self.image_number_offset)+'.png',self.cv_image_3d)
00157 self.sss.sleep(1)
00158 self.image_number_offset = self.image_number_offset + 1
00159
00160 def callback_r(self,data):
00161 try:
00162 self.cv_image_r = self.bridge.imgmsg_to_cv(data, "bgr8")
00163 except CvBridgeError, e:
00164 print e
00165
00166 def callback_l(self,data):
00167 try:
00168 self.cv_image_l = self.bridge.imgmsg_to_cv(data, "bgr8")
00169 except CvBridgeError, e:
00170 print e
00171
00172 def callback_3d(self,data):
00173 try:
00174 self.cv_image_3d = self.bridge.imgmsg_to_cv(data, "bgr8")
00175 except CvBridgeError, e:
00176 print e
00177
00178 if __name__ == "__main__":
00179 SCRIPT = CalibCam()
00180 SCRIPT.Start()