# Camera calibration and conversion to XML file

**URL:** <https://forum.opencv.org/t/camera-calibration-and-conversion-to-xml-file/2946>\
**Category:** C++\
**Tags:** calib3d\
**Created:** [April 23, 2021, 2:27pm UTC](https://forum.opencv.org/t/camera-calibration-and-conversion-to-xml-file/2946 "2021-04-23T14:27:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CZH](https://avatars.discourse-cdn.com/v4/letter/c/e56c9b/32.png) [@CZH](https://forum.opencv.org/u/CZH)\
**Post date:** [April 23, 2021, 2:27pm UTC](https://forum.opencv.org/t/camera-calibration-and-conversion-to-xml-file/2946/1 "2021-04-23T14:27:54Z")

</div>

In order to form a stereo map, I want to get camera calibration information. However, the calibration program in opencv / samples / cpp is difficult for me to write and operate, so I choose to calibrate the camera in MATLAB, but I don’t know how to convert it into opencv readable XML file. If possible, I also want to know for binocular camera extrinsics.yml (XML) and intrinsics.yml (XML) the format of these two files.

Hope to get your help, thank you very much!

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 23, 2021, 4:50pm UTC](https://forum.opencv.org/t/camera-calibration-and-conversion-to-xml-file/2946/2 "2021-04-23T16:50:23Z")

</div>

> [@CZH](#):
>
> so I choose to calibrate the camera in MATLAB

unfortunately, we cannot help you with that

> [@CZH](#):
>
> the format of these two files.

here’s how a minimal yml file (for a single camera) would look like:

```
%YAML:1.0
---
camera_matrix: !!opencv-matrix
   rows: 3
   cols: 3
   dt: d
   data: [ 5.3591573396163199e+02, 0., 3.4228315473308373e+02, 0.,
       5.3591573396163199e+02, 2.3557082909788173e+02, 0., 0., 1. ]
distortion_coefficients: !!opencv-matrix
   rows: 5
   cols: 1
   dt: d
   data: [ -2.6637260909660682e-01, -3.8588898922304653e-02,
       1.7831947042852964e-03, -2.8122100441115472e-04,
       2.3839153080878486e-01 ]

```

([complete file here](https://github.com/opencv/opencv/blob/master/samples/data/left_intrinsics.yml))  
(you’re free to change the names (“camera\_matrix”, “distortion\_coefficients”) to whatever you like)

to read it back, use:

```
FileStorage fs("my.yml", 0);
Mat cm; fs["camera_matrix"] >> cm; // or whatever name you used before
Mat dc; fs["distortion_coefficients"] >> dc;
fs.release();

```

---

<div class="post-metadata">

**Author:** ![CZH](https://avatars.discourse-cdn.com/v4/letter/c/e56c9b/32.png) [@CZH](https://forum.opencv.org/u/CZH)\
**Post date:** [April 24, 2021, 9:28am UTC](https://forum.opencv.org/t/camera-calibration-and-conversion-to-xml-file/2946/3 "2021-04-24T09:28:25Z")

</div>

Thank you for your reply. I’ll try it further！

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 24, 2021, 4:14pm UTC](https://forum.opencv.org/t/camera-calibration-and-conversion-to-xml-file/2946/4 "2021-04-24T16:14:51Z")

</div>

btw, opencv’s calibration returns (quite weird) distortion coeffs in the format

```
r1, r2, p1, p2, r3 

```

(r rotational, p positional)

---

<div class="post-metadata">

**Author:** ![CZH](https://avatars.discourse-cdn.com/v4/letter/c/e56c9b/32.png) [@CZH](https://forum.opencv.org/u/CZH)\
**Post date:** [April 25, 2021, 10:02am UTC](https://forum.opencv.org/t/camera-calibration-and-conversion-to-xml-file/2946/5 "2021-04-25T10:02:22Z")

</div>

Thank you for your reminding. I have completed the intrinsics.yml, However we need R1,R2.P1,P2,Q in extrinsics.yml

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/00798a6103a6d1bf0396bd75bf08ba231aa456b3.png)  
Do you know what they stand for？

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 25, 2021, 10:32am UTC](https://forum.opencv.org/t/camera-calibration-and-conversion-to-xml-file/2946/6 "2021-04-25T10:32:39Z")

</div>

[explained here](https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga617b1685d4059c6040827800e72ad2b6)
