|
| 1 | +#%% |
| 2 | +import yaml |
| 3 | +from yaml import SafeDumper |
| 4 | +import numpy as np |
| 5 | +import cv2 |
| 6 | +def represent_flow_style_list(dumper, data): |
| 7 | + return dumper.represent_sequence(yaml.resolver.BaseResolver.DEFAULT_SEQUENCE_TAG, data, flow_style=True) |
| 8 | +SafeDumper.add_representer(list, represent_flow_style_list) |
| 9 | +#%% |
| 10 | +class FlowListDumper(yaml.Dumper): |
| 11 | + def represent_list(self, data): |
| 12 | + return self.represent_sequence('tag:yaml.org,2002:seq', data, flow_style=True) |
| 13 | + |
| 14 | +def load_ex(path,mode,ref='rear_axle_center'): |
| 15 | + with open(path) as stream: |
| 16 | + y = yaml.safe_load(stream) |
| 17 | + assert y['reference'] == ref |
| 18 | + if mode == 'matrix': |
| 19 | + ret = np.eye(4) |
| 20 | + ret[0:3,0:3] = y['rotation'] |
| 21 | + ret[:-1,3] = y['position'] |
| 22 | + return ret |
| 23 | + elif mode == 'tuple': |
| 24 | + return np.array(y['rotation']),np.array(y['position']) |
| 25 | + |
| 26 | + |
| 27 | +def save_ex(path,rotation=None,translation=None,matrix=None,ref='rear_axle_center'): |
| 28 | + if matrix is not None: |
| 29 | + rot = matrix[0:3,0:3] |
| 30 | + trans = matrix[0:3,3] |
| 31 | + save_ex(path,rot,trans,ref=ref) |
| 32 | + return |
| 33 | + ret = {} |
| 34 | + ret['reference'] = ref |
| 35 | + ret['rotation'] = rotation |
| 36 | + ret['position'] = translation |
| 37 | + for i in ret: |
| 38 | + if type(ret[i]) == np.ndarray: |
| 39 | + ret[i] = ret[i].tolist() |
| 40 | + print(yaml.dump(ret,Dumper=SafeDumper,default_flow_style=False)) |
| 41 | + with open(path,'w') as stream: |
| 42 | + yaml.dump(ret,stream,Dumper=SafeDumper,default_flow_style=False) |
| 43 | + |
| 44 | +def load_in(path,mode='matrix',return_distort=False): |
| 45 | + with open(path) as stream: |
| 46 | + y = yaml.safe_load(stream) |
| 47 | + if 'skew' not in y: y['skew'] = 0 |
| 48 | + if 'distort' not in y: y['distort'] = [0,0,0,0,0] |
| 49 | + if mode == 'matrix': |
| 50 | + ret = np.zeros((3,3)) |
| 51 | + ret[0,0],ret[1,1] = y['focal'] |
| 52 | + ret[2,2] = 1. |
| 53 | + ret[0:2,2] = y['center'] |
| 54 | + ret[0,1] = y['skew'] |
| 55 | + if return_distort: |
| 56 | + return ret,np.array(y['distort']) |
| 57 | + else: |
| 58 | + return ret |
| 59 | + elif mode == 'tuple': |
| 60 | + return {'focal':np.array(y['focal']), |
| 61 | + 'center':np.array(y['center']), |
| 62 | + 'skew':np.array(y['skew']), |
| 63 | + 'distort':np.array(y['distort'])} |
| 64 | + |
| 65 | +from collections.abc import Iterable |
| 66 | +def save_in(path,focal=None,center=None,skew=0,distort=[0.0]*5,matrix=None): |
| 67 | + if matrix is not None: |
| 68 | + focal = matrix.diagonal()[0:2] |
| 69 | + skew = matrix[0,1] |
| 70 | + center = matrix[0:2,2] |
| 71 | + save_in(path,focal,center,skew,distort) |
| 72 | + return |
| 73 | + ret = {} |
| 74 | + ret['focal'] = focal |
| 75 | + ret['center'] = center |
| 76 | + ret['skew'] = skew |
| 77 | + assert len(distort) in [4,5] |
| 78 | + ret['distort'] = distort |
| 79 | + if len(ret['distort']) == 4: |
| 80 | + ret['distort'] = list(ret['distort'])+[0.0] |
| 81 | + for i in ret: |
| 82 | + if type(ret[i]) == np.ndarray: |
| 83 | + ret[i] = ret[i].tolist() |
| 84 | + if isinstance(ret[i],Iterable): |
| 85 | + ret[i] = [*map(float,ret[i])] |
| 86 | + print(yaml.dump(ret,Dumper=SafeDumper,default_flow_style=False)) |
| 87 | + with open(path,'w') as stream: |
| 88 | + yaml.dump(ret,stream,Dumper=SafeDumper,default_flow_style=False) |
| 89 | + |
| 90 | +def undistort_image(image, camera_matrix, distortion_coefficients): |
| 91 | + h, w = image.shape[:2] |
| 92 | + newK, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, distortion_coefficients, (w,h), 1, (w,h)) |
| 93 | + image = cv2.undistort(image, camera_matrix, distortion_coefficients, None, newK) |
| 94 | + return image, newK |
| 95 | + |
| 96 | + |
| 97 | +#%% |
| 98 | +if __name__ == "__main__": |
| 99 | + #%% |
| 100 | + rot, trans = load_ex('/mnt/GEMstack/GEMstack/knowledge/calibration/gem_e4_ouster.yaml',mode='tuple') |
| 101 | + save_ex('/tmp/test.yaml',rot,trans) |
| 102 | + #%% |
| 103 | + focal = [1,2,3] |
| 104 | + center = [400,500] |
| 105 | + save_in('/tmp/test.yaml',focal,center) |
| 106 | + load_in('/tmp/test.yaml',mode='tuple') |
0 commit comments