Forum

Author Topic: Agisoft and opencv calibration.  (Read 7172 times)

hagorms

  • Newbie
  • *
  • Posts: 12
    • View Profile
Agisoft and opencv calibration.
« on: November 14, 2017, 01:47:48 PM »
Hello. I used opencv calibration to calibrate cameras, using aicon markers.
Now I have  4x4 transform matrix for each camera which contains a rotation matrix and a translation matrix ( 4x4 matrix with 0001 in the last line).
Now what I want is to take my opencv calibration and put it straight to agisoft, but your y and z are negative (for 1rst camera your rotation is eye with 1 -1 -1 1) .
So maybe you can help me with the next question: which transfrom shall I make  to get a valid agisoft matrix?
 next the same question in russian:

Доброго времени суток. Я использовал опенсв калибровку, чтобы откалибровать камеры, используя аикон маркеры.
После калибровки имею 4на4 матрицу ткоторая состоит из матрицы поворота и матрицы сдвига (всего 4на4 с 0 0 0 1 в последней строке)
Борюсь с тем, чтобы взять эту калибровку опенсв и засунуть ее в агисофт, однако в агисофте z and y оси отрицательны (как понял: для первой камеры матрица eye  имеет 1 -1 -1 1 вместо стандартных единиц)
Может быть Вы мне поможете со следующим вопросом: какие действия нужно произвести, чтобы перейти в систему поворота в агисофте.

if need, i can provide opencvxml and agisoft xml and all scripts i did to parse it

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Agisoft and opencv calibration.
« Reply #1 on: November 14, 2018, 06:54:40 PM »
Hello hagorms,

Finally in the version 1.5.0 we have added support for the camera calibration import and export from/to OpenCV calibration format. Note that only interior orientation parameters can be exported/imported in such way via Tools Menu -> Camera Calibration dialog or via Python API (use format = "opencv" for calibration.save and calibration.load methods).

Please see the example of the calibration file below:
Code: [Select]
<?xml version="1.0"?>
<opencv_storage>
<calibration_Time>"Wed Nov 14 18:34:34 2018"</calibration_Time>
<image_Width>4096</image_Width>
<image_Height>2200</image_Height>
<Camera_Matrix type_id="opencv-matrix">
  <rows>3</rows>
  <cols>3</cols>
  <dt>d</dt>
  <data>
    6.41537766696059320e+003 0. 2.39877057830943390e+003 0.
    6.18520333499300700e+003 1.42367426026690030e+003 0. 0. 1.</data></Camera_Matrix>
<Distortion_Coefficients type_id="opencv-matrix">
  <rows>5</rows>
  <cols>1</cols>
  <dt>d</dt>
  <data>
    1.60181299771797150e-001 -7.39161459495649530e+000 -3.02710037047030120e-002 -3.87947847449515710e-003
    6.37705630914984520e+001</data></Distortion_Coefficients>
</opencv_storage>

Best regards,
Alexey Pasumansky,
Agisoft LLC

Goran

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Agisoft and opencv calibration.
« Reply #2 on: June 04, 2020, 05:54:12 PM »
hey
I am trying to import an opencv calibration but its failing with cant load calibration.
I put the mtx values in the xml form Alexey shared here and the dist parameters but no success  :-[
Code: [Select]
<?xml version="1.0"?>
<opencv_storage>
<calibration_Time>"Wed Nov 14 18:34:34 2018"</calibration_Time>
<image_Width>4912</image_Width>
<image_Height>3684</image_Height>
<Camera_Matrix type_id="opencv-matrix">
  <rows>3</rows>
  <cols>3</cols>
  <dt>d</dt>
  <data>
    7.1027199047244882e+04 0. 2.1432310560847563e+03 0. 6.6698200834795687e+04 1.8130108111131310e+03 0. 0. 4.</data></Camera_Matrix>
<Distortion_Coefficients type_id="opencv-matrix">
  <rows>1</rows>
  <cols>5</cols>
  <dt>d</dt>
  <data>
    -1.5335114309410122e+02 4.4860617192493759e+04 3.0359106998357475e+00 1.6586668282629993e+00 8.7565776122760113e+01</data></Distortion_Coefficients>
</opencv_storage>

what am i missing?

EDIT: please delete I simply forgot to select OPENCV Format
EDIT2: i think it should be rows:1 cols:5 and not the other way or not?
« Last Edit: June 12, 2020, 08:14:56 PM by Goran »

frenezulo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Agisoft and opencv calibration.
« Reply #3 on: November 29, 2022, 05:07:44 PM »
Hello,
I tried the function of importing OpenCV intrinsics to Agisoft and found several strange things.
1. focal length f - seems to be different from simple average of fx and fy from OpenCV. Why is that and how is the f parameter calculated in Agisoft?
2. Tangential coefficients p1, p2 - they seem to be switched when the xml file from OpenCV is imported to Agisoft. Is it possible? If yes, why is that?
3. Affinity and skew coefficients b1, b2 - b1 parameter is not present in the OpenCV file, but the value in Agisoft is non-zero. Why is that?

Attached are files:
Sample_calib_OpenCV_import.xml - file that I used to import camera calibration parameters from OpenCV
Sample_calib_OpenCV_export.xml - file exported from Agisoft in the OpenCV format
Sample_calib_Agisoft_export.xml - file exported from Agisfot in the Agisoft format
Sample_calib_OpenCV_import.png - screenshot from Agisoft after import of OpenCV camera calib. file

Thank you
Tomas


Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: Agisoft and opencv calibration.
« Reply #4 on: November 29, 2022, 06:31:13 PM »
Hola frenezulo,

in Agisoft camera calibration format:
f = fy
b1 = fx - fy or b1 + f = fx.

As for inverting tangential distorsion it is certainly possible as it is the same for Pix4d calibration vs Agisoft. Maybe support can confirm,

Hope this helps,
Best Regards,
Paul Pelletier,
Surveyor

frenezulo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Agisoft and opencv calibration.
« Reply #5 on: November 30, 2022, 09:21:51 AM »
Helo Paulo,
thank you for your reply and clarification.
Do you know, why is f=fy in Agisoft? And why is b1 defined in this way?

My final application is to use camera calibration intrinsics from Matlab in Agisoft and because there is no direct import of these parameters from Matlab, I started to look into various definitions of intrinsics in different softwares and it seems there are quite a differences despite the same notation.
Based on Agisoft Manual (https://www.agisoft.com/pdf/metashape-pro_1_8_en.pdf) and Matlab documentation for camera calibration (https://www.mathworks.com/help/vision/ref/estimatecameraparameters.html) it seems that p1 and p2 coefficients are inverted. Is my understanding correct?

Thank you
Tomas

Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: Agisoft and opencv calibration.
« Reply #6 on: November 30, 2022, 03:15:27 PM »
Hi Thomas,

from same Agisoft manual:
Code: [Select]
Frame cameras
x = X / Z
y = Y / Z
r = sqrt(x2 + y2)
x' = x(1 + K1r2 + K2r4 + K3r6 + K4r8) + (P1(r2+2x2) + 2P2xy)
y' = y(1 + K1r2 + K2r4 + K3r6 + K4r8) + (P2(r2+2y2) + 2P1xy)
u = w * 0.5 + cx + x'f + x'B1 + y'B2
v = h * 0.5 + cy + y'f

it is ckear that f = fy and fx = f + B1 as last 2 lines can be expressed in matrix format as:
(u v)T = K * (x'  y')T + (w *0.5+cx  h*0.5+cy)T where :
K is the 2 by 2 calibration matrix:
 f+B1   B2
     0           f

As for inverting p1 and p2 in different camera calibration models, I think you are right...


« Last Edit: November 30, 2022, 03:22:01 PM by Paulo »
Best Regards,
Paul Pelletier,
Surveyor

frenezulo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Agisoft and opencv calibration.
« Reply #7 on: November 30, 2022, 04:39:36 PM »
Hi Paul,
thank you for clarification of the parameters calculations.

Tomas