# Computing the Calibration Model

## Manual estimate

You can manually estimate the calibration when no distortion or perspective are required and the accuracy requirements are low.

To estimate the calibration coefficients:

* Either locate the limits of the field of view and divide the image resolution by the field of view size,
* Or use the following procedure:
  1. Take a picture of the part to inspect or of a calibration target such as a rectangle.
  2. Locate the feature points such as corners in the image (by the eye) and determine their coordinates in pixel units $(i_n, j_n)$.
  3. Use the euclidean distance formula to derive the resolution in pixels per unit ($D$ is the world distance between the corresponding points in units):

  $R = \frac{\sqrt{(i_1 - i_0)^2 + (j_1 - j_0)^2}}{D}$
  1. For non-square pixels, repeat this operation for the pairs of horizontal and vertical points, to get the horizontal and the vertical resolutions.
  2. To estimate a skew angle, apply the following formula to 2 points on the X-axis in the world system:

  $\theta = \arctan \frac{j_1 - j_0}{i_1 - i_0}$
  ![image](assets/images/easygauge/estimatingscalefactorsandskewangle.webp)

Use [`ECameraCalibration::SetSensor`](../../../doxygen_cpp/classeVision_1_1ECameraCalibration.html#a856ab5441f450cbff0fc8456f4a19b36) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1ECameraCalibration.html#ab01574a3fbfa7b03b0f5ed78b4781851) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECameraCalibration.html#ae0dd3480e51892ea2499b2239991eedd) or [`ECameraCalibration::SetSensorUsingResolution`](../../../doxygen_cpp/classeVision_1_1ECameraCalibration.html#a7a5b2306a251ba44570d8f41a0e71370) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1ECameraCalibration.html#a3bff644675616db81f2f002df55c3a5b) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECameraCalibration.html#a9f942cdab28bd3d9fdc19218dad3529d) to specify the parameters of the calibration:

* If there is a skew angle: with the modes [`ECalibrationMode_Scaled`](../../../doxygen_cpp/namespaceeVision.html#aa4f09c74296350db3ffce101d476f05a) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#aadc222f93604cb16a11491a0bf6e52ae) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECalibrationMode.html) and [`ECalibrationMode_Skewed`](../../../doxygen_cpp/namespaceeVision.html#aa4f09c74296350db3ffce101d476f05a) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#aadc222f93604cb16a11491a0bf6e52ae) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECalibrationMode.html).
* For non-square pixels: with the mode [`ECalibrationMode_Anisotropic`](../../../doxygen_cpp/namespaceeVision.html#aa4f09c74296350db3ffce101d476f05a) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#aadc222f93604cb16a11491a0bf6e52ae) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECalibrationMode.html).

## Automatic dot grid calibration

* To compute the calibration automatically, use a dot grid pattern as a plane of reference with the method [`ECameraCalibration::AutoCalibrateDotGrid`](../../../doxygen_cpp/classeVision_1_1ECameraCalibration.html#a8b79189e36f149d62fd87df49c968a54) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1ECameraCalibration.html#a3c2eaca4f3d63a8dde307b3caf0e55a3) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECameraCalibration.html#a46a42ce4ee413906caffe0d282811ab1).

  It takes the following parameters:
  * An image containing the dot grid.
  * The column and the row pitch of the pattern (the horizontal and the vertical spacing between the dots in metric units).
  * A combination of modes [`ECalibrationMode`](../../../doxygen_cpp/namespaceeVision.html#aa4f09c74296350db3ffce101d476f05a) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#aadc222f93604cb16a11491a0bf6e52ae) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECalibrationMode.html) to test or decide whether to test the empirical and advanced models.
* The method performs the detection of the dots:
  1. It assigns them real world coordinates according to the column and row pitch.
  2. It tries all the possible combination of calibration modes.
  3. It selects the set of modes that gives the lowest calibration score.

  #### NOTE
  All blobs whose area is smaller than 5 pixels or $1/50000^{th}$ of the whole image area are considered as noise and rejected. Use the method [`ECameraCalibration::GetCustomMinDotArea`](../../../doxygen_cpp/classeVision_1_1ECameraCalibration.html#addf4d2e562f44ade01923137c49e0220) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1ECameraCalibration.html#ab10be5822cf6d0927c8a6ccb536556a3) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECameraCalibration.html#a9843f7eab0e8c324953493bf952bab17) to override this criteria.

  ![image](assets/images/easygauge/dotgrid.jpg)
* When doing dot grid calibration, make sure that:
  * Most of the dots of the calibration pattern appear in the image.
  * The calibration pattern is well lit, with no minimum variance in illumination across the image.
  * Each part of the image is properly focused.

## Landmark calibration

With the landmark calibration, you can specify the image points and their corresponding real-world coordinates to compute the calibration.

1. Use [`ECameraCalibration::AddLandmark`](../../../doxygen_cpp/classeVision_1_1ECameraCalibration.html#a6a4e70221c62782a7b8f855425b63b38) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1ECameraCalibration.html#ab1aac7bf1af7c0345cc1da4c00c049f9) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECameraCalibration.html#a2d20dd810cf383b3616438cc4936b430) to add your landmarks.
2. Call [`ECameraCalibration::AutoCalibrateLandmarks`](../../../doxygen_cpp/classeVision_1_1ECameraCalibration.html#aa90761316d58419320f3980a685d037d) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1ECameraCalibration.html#ad08598b220d751fc8ee1bdd217328da5) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECameraCalibration.html#a8b7e2bdc6a88c3902326f4e3cf029bac) to compute the calibration based on the specified landmarks.
