11import argparse
2- import base64
32import json
43import os
54from dataclasses import dataclass
65
7- import cv2
86import mrcal
97import numpy as np
108from wpimath .geometry import Quaternion as _Quat
119
1210
1311@dataclass
1412class Size :
15- width : int
16- height : int
13+ width : float
14+ height : float
1715
1816
1917@dataclass
@@ -24,14 +22,6 @@ class JsonMatOfDoubles:
2422 data : list [float ]
2523
2624
27- @dataclass
28- class JsonMat :
29- rows : int
30- cols : int
31- type : int
32- data : str # Base64-encoded PNG data
33-
34-
3525@dataclass
3626class Point2 :
3727 x : float
@@ -84,8 +74,7 @@ class Observation:
8474 # If we should use this observation when re-calculating camera calibration
8575 includeObservationInCalibration : bool
8676 snapshotName : str
87- # The actual image the snapshot is from
88- snapshotData : JsonMat
77+ snapshotDataLocation : str
8978
9079
9180@dataclass
@@ -97,6 +86,7 @@ class CameraCalibration:
9786 calobjectWarp : list [float ]
9887 calobjectSize : Size
9988 calobjectSpacing : float
89+ lensmodel : str
10090
10191
10292def __convert_cal_to_mrcal_cameramodel (
@@ -127,6 +117,13 @@ def pose_to_rt(pose: Pose3d):
127117 ]
128118 return np .concatenate ((r , t ))
129119
120+ imagersize = (int (cal .resolution .width ), int (cal .resolution .height ))
121+
122+ def fill_missing_corners (observations : list [list [float ]], width : int , height : int ):
123+ num_corners = width * height
124+ observations += [[0 , 0 , - 1 ] for x in range (num_corners - len (observations ))]
125+ return observations
126+
130127 imagersize = (cal .resolution .width , cal .resolution .height )
131128
132129 # Always weight=1 for Photon data
@@ -135,8 +132,12 @@ def pose_to_rt(pose: Pose3d):
135132 [
136133 # note that we expect row-major observations here. I think this holds
137134 np .array (
138- list (map (lambda it : [it .x , it .y , WEIGHT ], o .locationInImageSpace ))
139- ).reshape ((cal .calobjectSize .width , cal .calobjectSize .height , 3 ))
135+ fill_missing_corners (
136+ list (map (lambda it : [it .x , it .y , WEIGHT ], o .locationInImageSpace )),
137+ int (cal .calobjectSize .width ),
138+ int (cal .calobjectSize .height ),
139+ )
140+ ).reshape ((int (cal .calobjectSize .width ), int (cal .calobjectSize .height ), 3 ))
140141 for o in cal .observations
141142 ]
142143 )
@@ -206,14 +207,6 @@ def from_dict(cls, dict):
206207 if not os .path .exists (output_folder ):
207208 os .makedirs (output_folder )
208209
209- # Decode each image and save it as a png
210- for obs in camera_cal_data .observations :
211- image = obs .snapshotData .data
212- decoded_data = base64 .b64decode (image )
213- np_data = np .frombuffer (decoded_data , np .uint8 )
214- img = cv2 .imdecode (np_data , cv2 .IMREAD_UNCHANGED )
215- cv2 .imwrite (f"{ output_folder } /{ obs .snapshotName } " , img )
216-
217210 # And create a VNL file for use with mrcal
218211 with open (f"{ output_folder } /corners.vnl" , "w+" ) as vnl_file :
219212 vnl_file .write ("# filename x y level\n " )
0 commit comments