66import numpy as np
77import cv2
88import copy
9+ import json
910import argparse
11+ import os
1012import common3Dfunc as c3D
1113from math import *
1214
@@ -33,6 +35,8 @@ def get_argumets():
3335 help = 'file name of the camera intrinsic.' )
3436 parser .add_argument ('--model' , type = str , default = 'data/hammer_mm.ply' ,
3537 help = 'file name of the object model (.pcd or .ply).' )
38+ parser .add_argument ('--init' , type = str , default = 'data/init.json' ,
39+ help = 'file name of the initial transformation (.json).' )
3640
3741 return parser .parse_args ()
3842
@@ -188,7 +192,20 @@ def generateImage( mapping, im_color ):
188192 img_mapped = cv2 .addWeighted (img_m2 , 0.5 , im_color , 0.5 , 0 )
189193 cv2 .imshow ( window_name , img_mapped )
190194
195+ """ save transformation matrix as a .json file. """
196+ def save_transformation ( trans , name ):
197+ trans_list = trans .tolist ()
198+ transform = {"transformation4x4" :[trans_list ]}
199+ f_out = open ( name , 'w' )
200+ json .dump ( transform , f_out )
191201
202+ """ load transformation matrix from a .json file. """
203+ def load_transformation ( name ):
204+ f_in = open ( name , 'r' )
205+ json_data = json .load (f_in )
206+ trans_in = np .array (json_data ["transformation4x4" ][0 ])
207+
208+ return trans_in
192209
193210if __name__ == "__main__" :
194211
@@ -211,15 +228,26 @@ def generateImage( mapping, im_color ):
211228
212229 np_pcd = np .asarray (pcd .points )
213230
214- """Loading and rescaling of the object model"""
231+ """Loading of the object model"""
215232 print ('Loading: {}' .format (args .model ))
216233 cloud_m = o3 .read_point_cloud ( args .model )
217234 cloud_m_ds = o3 .voxel_down_sample ( cloud_m , voxel_size )
218- cloud_m_c , offset = c3D .Centering ( cloud_m_ds )
219- mat_centering = c3D .makeTranslation4x4 ( - 1.0 * offset )
220- all_transformation = np .dot ( mat_centering , all_transformation )
221235
222- CLOUD_ROT = copy .deepcopy (cloud_m_c )
236+ """Loading of the initial transformation"""
237+ initial_trans = np .identity (4 )
238+ if os .path .exists ( args .init ):
239+ initial_trans = load_transformation ( args .init )
240+ print ('Use initial transformation\n ' , initial_trans )
241+ all_transformation = np .dot ( initial_trans , all_transformation )
242+ else :
243+ # if initial transformation is not avairable,
244+ # the object model is moved to its center.
245+ cloud_m_c , offset = c3D .Centering ( cloud_m_ds )
246+ mat_centering = c3D .makeTranslation4x4 ( - 1.0 * offset )
247+ all_transformation = np .dot ( mat_centering , all_transformation )
248+
249+ CLOUD_ROT = copy .deepcopy (cloud_m_ds )
250+ CLOUD_ROT .transform ( all_transformation )
223251
224252 mapping = Mapping ('./data/realsense_intrinsic.json' )
225253 img_mapped = mapping .Cloud2Image ( CLOUD_ROT )
@@ -294,3 +322,5 @@ def generateImage( mapping, im_color ):
294322
295323 print ("\n \n Final transformation is\n " , all_transformation )
296324 print ("You can transform the original model to the final pose by multiplying above matrix." )
325+ save_transformation ( all_transformation , 'trans.json' )
326+
0 commit comments