Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing camera matrix into xml/yaml file

Tags:

python

opencv

I am using opencv and python I have calibrated my camera having the following parameters:

camera_matrix=[[ 532.80990646 ,0.0,342.49522219],[0.0,532.93344713,233.88792491],[0.0,0.0,1.0]] 
dist_coeff = [-2.81325798e-01,2.91150014e-02,1.21234399e-03,-1.40823665e-04,1.54861424e-01]

I am working in python.I wrote the following code to save the above into a file but the file was like a normal text file.

f = open("../calibration_camera.xml","w")
f.write('Camera Matrix:\n'+str(camera_matrix))
f.write('\n')
f.write('Distortion Coefficients:\n'+str(dist_coefs))
f.write('\n')
f.close()

How can i save this data into an xml/yaml file using python commands thus getting the desired output.Please help. Thanks in advance

like image 353
kushal Avatar asked Apr 30 '14 20:04

kushal


1 Answers

Using JSON

JSON seems to be the easiest format for serialization in your case

camera_matrix=[[ 532.80990646 ,0.0,342.49522219],[0.0,532.93344713,233.88792491],[0.0,0.0,1.0]]
dist_coeff = [-2.81325798e-01,2.91150014e-02,1.21234399e-03,-1.40823665e-04,1.54861424e-01]
data = {"camera_matrix": camera_matrix, "dist_coeff": dist_coeff}
fname = "data.json"
import json
with open(fname, "w") as f:
    json.dump(data, f)

data.json:

{"dist_coeff": [-0.281325798, 0.0291150014, 0.00121234399, -0.000140823665, 0.154861424], "camera_matrix": [[532.80990646, 0.0, 342.49522219], [0.0, 532.93344713, 233.88792491], [0.0, 0.0, 1.0]]}

Using YAML

YAML is best option, if you expect human editing of the content

In contrast to json module, yaml is not part of Python and must be installed first:

$ pip install pyyaml

Here goes the code to save the data:

fname = "data.yaml"
import yaml
with open(fname, "w") as f:
    yaml.dump(data, f)

data.yaml:

camera_matrix:
- [532.80990646, 0.0, 342.49522219]
- [0.0, 532.93344713, 233.88792491]
- [0.0, 0.0, 1.0]
dist_coeff: [-0.281325798, 0.0291150014, 0.00121234399, -0.000140823665, 0.154861424]

Using XML

My example is using my favourite lxml package, other XML packages are also available.

from lxml import etree
from lxml.builder import E

camera_matrix=[[ 532.80990646 ,0.0,342.49522219],[0.0,532.93344713,233.88792491],[0.0,0.0,1.0]]
dist_coeff = [-2.81325798e-01,2.91150014e-02,1.21234399e-03,-1.40823665e-04,1.54861424e-01]

def triada(itm):
    a, b, c = itm
    return E.Triada(a = str(a), b = str(b), c = str(c))

camera_matrix_xml = E.CameraMatrix(*map(triada, camera_matrix))
dist_coeff_xml = E.DistCoef(*map(E.Coef, map(str, dist_coeff)))


xmldoc = E.CameraData(camera_matrix_xml, dist_coeff_xml)

fname = "data.xml"
with open(fname, "w") as f:
    f.write(etree.tostring(xmldoc, pretty_print=True))

data.xml:

<CameraData>
  <CameraMatrix>
    <Triada a="532.80990646" c="342.49522219" b="0.0"/>
    <Triada a="0.0" c="233.88792491" b="532.93344713"/>
    <Triada a="0.0" c="1.0" b="0.0"/>
  </CameraMatrix>
  <DistCoef>
    <Coef>-0.281325798</Coef>
    <Coef>0.0291150014</Coef>
    <Coef>0.00121234399</Coef>
    <Coef>-0.000140823665</Coef>
    <Coef>0.154861424</Coef>
  </DistCoef>
</CameraData>

You shall play a bit with the code to format strings representing the numbers with proper precision. This I leave to you.

like image 174
Jan Vlcinsky Avatar answered Nov 19 '22 06:11

Jan Vlcinsky