0
  • I have done camera calibration via Matlab 2019a, then I have saved the variable cameraParams which turns to be all the camera parameters, but I'm only interested in Intrinsic matrix, and distortion coefficients.

  • How can I extract these two arrays from the cameraParams.mat file?Mat file attached here

  • clarification:( the Intrinsic matrix, is 3x3 matrix, and the distortion coefficients, are 4 coefficients in Matlab, 2 for radial, and 2 for tangential distortion)

  • I have tried the following code, but I didn't know how to extract the arrays:

import scipy.io as sio
import numpy as np

Mat = sio.loadmat('CameraParams.mat')

for key in Mat :
    print(key, Mat[key])

The output is:

__header__ b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Tue Feb  2 12:32:06 2021'
__version__ 1.0
__globals__ []
None [(b'cameraParams', b'MCOS', b'cameraParameters', array([[3707764736],
       [         2],
       [         1],
       [         1],
       [         1],
       [         1]], dtype=uint32))]
__function_workspace__ [[ 0  1 73 ...  0  0  0]]

3 Answers 3

1

In an Ipython session:

In [385]: data = loadmat('../Downloads/CameraParams.mat')

data is a dict, from which we can use keys to view variables from the file:

In [386]: data
Out[386]: 
{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Tue Feb  2 12:32:06 2021',
 '__version__': '1.0',
 '__globals__': [],
 'None': MatlabOpaque([(b'cameraParams', b'MCOS', b'cameraParameters', array([[3707764736],
        [         2],
        [         1],
        [         1],
        [         1],
        [         1]], dtype=uint32))],
              dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')]),
 '__function_workspace__': array([[ 0,  1, 73, ...,  0,  0,  0]], dtype=uint8)}

MatlabOpaque denotes some sort of MATLAB object or class that can't be totally converted to a Python/numpy. But here it contains a structured array:

In [387]: data['None']
Out[387]: 
MatlabOpaque([(b'cameraParams', b'MCOS', b'cameraParameters', array([[3707764736],
       [         2],
       [         1],
       [         1],
       [         1],
       [         1]], dtype=uint32))],
             dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])
In [388]: data['None'].dtype
Out[388]: dtype([('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])

From which we can select fields:

In [389]: data['None']['s0']
Out[389]: MatlabOpaque([b'cameraParams'], dtype=object)

In [390]: data['None']['arr']
Out[390]: 
MatlabOpaque([array([[3707764736],
       [         2],
       [         1],
       [         1],
       [         1],
       [         1]], dtype=uint32)], dtype=object)

And use item to pull the array out of the object dtype wrapper:

In [391]: data['None']['arr'].item()
Out[391]: 
array([[3707764736],
       [         2],
       [         1],
       [         1],
       [         1],
       [         1]], dtype=uint32)

That is a (6,1) array.

In [392]: data['__function_workspace__']
Out[392]: array([[ 0,  1, 73, ...,  0,  0,  0]], dtype=uint8)
In [393]: data['__function_workspace__'].shape
Out[393]: (1, 33848)
2
  • thanks for your detailed answer, my confusion is that: how to extract the intrinsic parameters (3x3) matrix = 9 elements, and distortion which can be 4,5,8,12 elements, from the last result 33848in your answer, I'm just confused how to find their correct indices.
    – Bilal
    Feb 13, 2021 at 7:32
  • 1
    MatlabOpaque means loadmat can't look inside it - it's a 'opaque', presumably some sort of MATLAB class or function that cannot be translated into Python/numpy. The best you can do is go back to the MATLAB source, and write these parameters as separate variables.
    – hpaulj
    Feb 13, 2021 at 7:41
1

You've already figured it out IMO. Note it's useful to use type() and isinstance() to see what type of variables you're dealing with. In my code the numpy arrays are available as v under the if instance() statement:

import scipy.io as sio
import numpy as np

Mat = sio.loadmat('CameraParams.mat')

for k, v in Mat.items():
    print("\n\n", k, type(v), v)
    if isinstance(v, np.ndarray):  # Numpy array?
        print(' --> Numpy array')

Output:

 __header__ <class 'bytes'> b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Tue Feb  2 12:32:06 2021'


 __version__ <class 'str'> 1.0


 __globals__ <class 'list'> []


 None <class 'scipy.io.matlab.mio5_params.MatlabOpaque'> [(b'cameraParams', b'MCOS', b'cameraParameters', array([[3707764736],
       [         2],
       [         1],
       [         1],
       [         1],
       [         1]], dtype=uint32))]
 --> Numpy array


 __function_workspace__ <class 'numpy.ndarray'> [[ 0  1 73 ...  0  0  0]]
 --> Numpy array

The last 2 items are already numpy arrays, FYI the MatlabOpaque class returns a numpy array: https://github.com/scipy/scipy/blob/master/scipy/io/matlab/mio5_params.py#L247

As for the output of loadmat() method, it seems some elements can be accessed directed if you know their name: https://github.com/scipy/scipy/blob/v1.6.0/scipy/io/matlab/mio.py#L214

matstruct_squeezed = sio.loadmat(matstruct_fname, squeeze_me=True)
matstruct_squeezed['teststruct']['complexfield'].item()
array([ 1.41421356+1.41421356j,  2.71828183+2.71828183j,
        3.14159265+3.14159265j])
2
  • yes indeed they are numpy array, my issue here is that: how to get the correct indices of my parameters(9 for intrinsic matrix, and 4,5,8 maybe 12 for the distortion) from the last numpy array in your answer.
    – Bilal
    Feb 13, 2021 at 7:35
  • The best is to look at the source to see how the outpout of loadmat() is constructued, the source contains some examples where values are accessed directly by name, see update at the bottom of my answer
    – Max
    Feb 15, 2021 at 8:05
0

The problem is that MatlabOpaque objects can not be accessed as stated in mbauman's answer. The easiest solution is to resave the object in MATLAB as struct(CameraParams).

You can also check this repo for easy conversion from .mat to numpy.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.