{
  "source_file": "0930",
  "source_platform": "技术平台",
  "author": "OpenCV",
  "title_listed": "Camera Calibration",
  "keywords": "相机标定,内参,畸变",
  "domain": "图像处理",
  "content_type": "官方教程",
  "is_paid_flag": "否",
  "url": "https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html",
  "publish_date": "持续更新",
  "corpus_stage": "图像处理",
  "notes": "相机标定",
  "id": "fbef82e77116",
  "collected_at": "2026-10-02T18:37:51",
  "title": "OpenCV: Camera Calibration",
  "body_markdown": "|  | OpenCV 4.13.0    Open Source Computer Vision | \nIn this section, we will learn about\nSome pinhole cameras introduce significant distortion to images. Two major kinds of distortion are radial distortion and tangential distortion.\nRadial distortion causes straight lines to appear curved. Radial distortion becomes larger the farther points are from the center of the image. For example, one image is shown below in which two edges of a chess board are marked with red lines. But, you can see that the border of the chess board is not a straight line and doesn't match with the red line. All the expected straight lines are bulged out. Visit [Distortion (optics)](https://en.wikipedia.org/wiki/Distortion_%28optics%29) for more details.\nIn the following sections several new parameters are introduced. Visit [Camera Calibration and 3D Reconstruction](../../d6/d55/tutorial_table_of_content_calib3d.html) for more details.\nRadial distortion can be represented as follows:\n\\[x_{distorted} = x( 1 + k_1 r^2 + k_2 r^4 + k_3 r^6) \\\\ y_{distorted} = y( 1 + k_1 r^2 + k_2 r^4 + k_3 r^6)\\]\nSimilarly, tangential distortion occurs because the image-taking lense is not aligned perfectly parallel to the imaging plane. So, some areas in the image may look nearer than expected. The amount of tangential distortion can be represented as below:\n\\[x_{distorted} = x + [ 2p_1xy + p_2(r^2+2x^2)] \\\\ y_{distorted} = y + [ p_1(r^2+ 2y^2)+ 2p_2xy]\\]\nIn short, we need to find five parameters, known as distortion coefficients given by:\n\\[Distortion \\; coefficients=(k_1 \\hspace{10pt} k_2 \\hspace{10pt} p_1 \\hspace{10pt} p_2 \\hspace{10pt} k_3)\\]\nIn addition to this, we need to some other information, like the intrinsic and extrinsic parameters of the camera. Intrinsic parameters are specific to a camera. They include information like focal length ( \\(f_x,f_y\\)) and optical centers ( \\(c_x, c_y\\)). The focal length and optical centers can be used to create a camera matrix, which can be used to remove distortion due to the lenses of a specific camera. The camera matrix is unique to a specific camera, so once calculated, it can be reused on other images taken by the same camera. It is expressed as a 3x3 matrix:\n\\[camera \\; matrix = \\left [ \\begin{matrix} f_x & 0 & c_x \\\\ 0 & f_y & c_y \\\\ 0 & 0 & 1 \\end{matrix} \\right ]\\]\nExtrinsic parameters corresponds to rotation and translation vectors which translates a coordinates of a 3D point to a coordinate system.\nFor stereo applications, these distortions need to be corrected first. To find these parameters, we must provide some sample images of a well defined pattern (e.g. a chess board). We find some specific points of which we already know the relative positions (e.g. square corners in the chess board). We know the coordinates of these points in real world space and we know the coordinates in the image, so we can solve for the distortion coefficients. For better results, we need at least 10 test patterns.\nAs mentioned above, we need at least 10 test patterns for camera calibration. OpenCV comes with some images of a chess board (see samples/data/left01.jpg – left14.jpg), so we will utilize these. Consider an image of a chess board. The important input data needed for calibration of the camera is the set of 3D real world points and the corresponding 2D coordinates of these points in the image. 2D image points are OK which we can easily find from the image. (These image points are locations where two black squares touch each other in chess boards)\nWhat about the 3D points from real world space? Those images are taken from a static camera and chess boards are placed at different locations and orientations. So we need to know \\((X,Y,Z)\\) values. But for simplicity, we can say chess board was kept stationary at XY plane, (so Z=0 always) and camera was moved accordingly. This consideration helps us to find only X,Y values. Now for X,Y values, we can simply pass the points as (0,0), (1,0), (2,0), ... which denotes the location of points. In this case, the results we get will be in the scale of size of chess board square. But if we know the square size, (say 30 mm), we can pass the values as (0,0), (30,0), (60,0), ... . Thus, we get the results in mm. (In this case, we don't know square size since we didn't take those images, so we pass in terms of square size).\n3D points are called object points and 2D image points are called image points.\nSo to find pattern in chess board, we can use the function, [cv.findChessboardCorners()](../../d9/d0c/group__calib3d.html#ga93efa9b0aa890de240ca32b11253dd4a). We also need to pass what kind of pattern we are looking for, like 8x8 grid, 5x5 grid etc. In this example, we use 7x6 grid. (Normally a chess board has 8x8 squares and 7x7 internal corners). It returns the corner points and retval which will be True if pattern is obtained. These corners will be placed in an order (from left-to-right, top-to-bottom)\nOnce we find the corners, we can increase their accuracy using [cv.cornerSubPix()](../../dd/d1a/group__imgproc__feature.html#ga354e0d7c86d0d9da75de9b9701a9a87e). We can also draw the pattern using [cv.drawChessboardCorners()](../../d9/d0c/group__calib3d.html#ga6a10b0bb120c4907e5eabbcd22319022). All these steps are included in below code:\nOne image with pattern drawn on it is shown below:\nNow that we have our object points and image points, we are ready to go for calibration. We can use the function, [cv.calibrateCamera()](../../d9/d0c/group__calib3d.html#ga3207604e4b1a1758aa66acb6ed5aa65d) which returns the camera matrix, distortion coefficients, rotation and translation vectors etc. \nNow, we can take an image and undistort it. OpenCV comes with two methods for doing this. However first, we can refine the camera matrix based on a free scaling parameter using [cv.getOptimalNewCameraMatrix()](../../d9/d0c/group__calib3d.html#ga7a6c4e032c97f03ba747966e6ad862b1). If the scaling parameter alpha=0, it returns undistorted image with minimum unwanted pixels. So it may even remove some pixels at image corners. If alpha=1, all pixels are retained with some extra black images. This function also returns an image ROI which can be used to crop the result.\nSo, we take a new image (left12.jpg in this case. That is the first image in this chapter)\nThis is the easiest way. Just call the function and use ROI obtained above to crop the result.\nThis way is a little bit more difficult. First, find a mapping function from the distorted image to the undistorted image. Then use the remap function.\nStill, both the methods give the same result. See the result below:\nYou can see in the result that all the edges are straight.\nNow you can store the camera matrix and distortion coefficients using write functions in NumPy (np.savez, np.savetxt etc) for future uses.\nRe-projection error gives a good estimation of just how exact the found parameters are. The closer the re-projection error is to zero, the more accurate the parameters we found are. Given the intrinsic, distortion, rotation and translation matrices, we must first transform the object point to image point using [cv.projectPoints()](../../d9/d0c/group__calib3d.html#ga1019495a2c8d1743ed5cc23fa0daff8c). Then, we can calculate the absolute norm between what we got with our transformation and the corner finding algorithm. To find the average error, we calculate the arithmetical mean of the errors calculated for all the calibration images.",
  "metadata": {
    "title": "OpenCV: Camera Calibration",
    "engine": "curl_cffi"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": "",
    "retried": true,
    "engine": "curl_cffi+trafilatura"
  },
  "code_blocks": [],
  "images": [],
  "audio": [],
  "videos": [],
  "links": [
    {
      "type": "external",
      "href": "https://www.cloudflare.com"
    },
    {
      "type": "external",
      "href": "https://www.cloudflare.com/privacypolicy/"
    }
  ],
  "retry_at": "2026-10-03T08:12:27",
  "final_url": "https://docs.opencv.org/4.13.0/dc/dbb/tutorial_py_calibration.html",
  "repair_version": 2,
  "repaired_at": "2026-10-20T17:31:01Z",
  "source_url": "https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html",
  "delivery_status": "unverified",
  "discovery_status": "http_403",
  "missing_reason": "http_403",
  "repair_error": "",
  "actual_video_files": [],
  "source_body_markdown": "|  | OpenCV 4.13.0    Open Source Computer Vision | \nIn this section, we will learn about\nSome pinhole cameras introduce significant distortion to images. Two major kinds of distortion are radial distortion and tangential distortion.\nRadial distortion causes straight lines to appear curved. Radial distortion becomes larger the farther points are from the center of the image. For example, one image is shown below in which two edges of a chess board are marked with red lines. But, you can see that the border of the chess board is not a straight line and doesn't match with the red line. All the expected straight lines are bulged out. Visit [Distortion (optics)](https://en.wikipedia.org/wiki/Distortion_%28optics%29) for more details.\nIn the following sections several new parameters are introduced. Visit [Camera Calibration and 3D Reconstruction](../../d6/d55/tutorial_table_of_content_calib3d.html) for more details.\nRadial distortion can be represented as follows:\n\\[x_{distorted} = x( 1 + k_1 r^2 + k_2 r^4 + k_3 r^6) \\\\ y_{distorted} = y( 1 + k_1 r^2 + k_2 r^4 + k_3 r^6)\\]\nSimilarly, tangential distortion occurs because the image-taking lense is not aligned perfectly parallel to the imaging plane. So, some areas in the image may look nearer than expected. The amount of tangential distortion can be represented as below:\n\\[x_{distorted} = x + [ 2p_1xy + p_2(r^2+2x^2)] \\\\ y_{distorted} = y + [ p_1(r^2+ 2y^2)+ 2p_2xy]\\]\nIn short, we need to find five parameters, known as distortion coefficients given by:\n\\[Distortion \\; coefficients=(k_1 \\hspace{10pt} k_2 \\hspace{10pt} p_1 \\hspace{10pt} p_2 \\hspace{10pt} k_3)\\]\nIn addition to this, we need to some other information, like the intrinsic and extrinsic parameters of the camera. Intrinsic parameters are specific to a camera. They include information like focal length ( \\(f_x,f_y\\)) and optical centers ( \\(c_x, c_y\\)). The focal length and optical centers can be used to create a camera matrix, which can be used to remove distortion due to the lenses of a specific camera. The camera matrix is unique to a specific camera, so once calculated, it can be reused on other images taken by the same camera. It is expressed as a 3x3 matrix:\n\\[camera \\; matrix = \\left [ \\begin{matrix} f_x & 0 & c_x \\\\ 0 & f_y & c_y \\\\ 0 & 0 & 1 \\end{matrix} \\right ]\\]\nExtrinsic parameters corresponds to rotation and translation vectors which translates a coordinates of a 3D point to a coordinate system.\nFor stereo applications, these distortions need to be corrected first. To find these parameters, we must provide some sample images of a well defined pattern (e.g. a chess board). We find some specific points of which we already know the relative positions (e.g. square corners in the chess board). We know the coordinates of these points in real world space and we know the coordinates in the image, so we can solve for the distortion coefficients. For better results, we need at least 10 test patterns.\nAs mentioned above, we need at least 10 test patterns for camera calibration. OpenCV comes with some images of a chess board (see samples/data/left01.jpg – left14.jpg), so we will utilize these. Consider an image of a chess board. The important input data needed for calibration of the camera is the set of 3D real world points and the corresponding 2D coordinates of these points in the image. 2D image points are OK which we can easily find from the image. (These image points are locations where two black squares touch each other in chess boards)\nWhat about the 3D points from real world space? Those images are taken from a static camera and chess boards are placed at different locations and orientations. So we need to know \\((X,Y,Z)\\) values. But for simplicity, we can say chess board was kept stationary at XY plane, (so Z=0 always) and camera was moved accordingly. This consideration helps us to find only X,Y values. Now for X,Y values, we can simply pass the points as (0,0), (1,0), (2,0), ... which denotes the location of points. In this case, the results we get will be in the scale of size of chess board square. But if we know the square size, (say 30 mm), we can pass the values as (0,0), (30,0), (60,0), ... . Thus, we get the results in mm. (In this case, we don't know square size since we didn't take those images, so we pass in terms of square size).\n3D points are called object points and 2D image points are called image points.\nSo to find pattern in chess board, we can use the function, [cv.findChessboardCorners()](../../d9/d0c/group__calib3d.html#ga93efa9b0aa890de240ca32b11253dd4a). We also need to pass what kind of pattern we are looking for, like 8x8 grid, 5x5 grid etc. In this example, we use 7x6 grid. (Normally a chess board has 8x8 squares and 7x7 internal corners). It returns the corner points and retval which will be True if pattern is obtained. These corners will be placed in an order (from left-to-right, top-to-bottom)\nOnce we find the corners, we can increase their accuracy using [cv.cornerSubPix()](../../dd/d1a/group__imgproc__feature.html#ga354e0d7c86d0d9da75de9b9701a9a87e). We can also draw the pattern using [cv.drawChessboardCorners()](../../d9/d0c/group__calib3d.html#ga6a10b0bb120c4907e5eabbcd22319022). All these steps are included in below code:\nOne image with pattern drawn on it is shown below:\nNow that we have our object points and image points, we are ready to go for calibration. We can use the function, [cv.calibrateCamera()](../../d9/d0c/group__calib3d.html#ga3207604e4b1a1758aa66acb6ed5aa65d) which returns the camera matrix, distortion coefficients, rotation and translation vectors etc. \nNow, we can take an image and undistort it. OpenCV comes with two methods for doing this. However first, we can refine the camera matrix based on a free scaling parameter using [cv.getOptimalNewCameraMatrix()](../../d9/d0c/group__calib3d.html#ga7a6c4e032c97f03ba747966e6ad862b1). If the scaling parameter alpha=0, it returns undistorted image with minimum unwanted pixels. So it may even remove some pixels at image corners. If alpha=1, all pixels are retained with some extra black images. This function also returns an image ROI which can be used to crop the result.\nSo, we take a new image (left12.jpg in this case. That is the first image in this chapter)\nThis is the easiest way. Just call the function and use ROI obtained above to crop the result.\nThis way is a little bit more difficult. First, find a mapping function from the distorted image to the undistorted image. Then use the remap function.\nStill, both the methods give the same result. See the result below:\nYou can see in the result that all the edges are straight.\nNow you can store the camera matrix and distortion coefficients using write functions in NumPy (np.savez, np.savetxt etc) for future uses.\nRe-projection error gives a good estimation of just how exact the found parameters are. The closer the re-projection error is to zero, the more accurate the parameters we found are. Given the intrinsic, distortion, rotation and translation matrices, we must first transform the object point to image point using [cv.projectPoints()](../../d9/d0c/group__calib3d.html#ga1019495a2c8d1743ed5cc23fa0daff8c). Then, we can calculate the absolute norm between what we got with our transformation and the corner finding algorithm. To find the average error, we calculate the arithmetical mean of the errors calculated for all the calibration images.",
  "body_base_path": "attachments/fbef82e77116/pages/",
  "body_markdown_path": "attachments/fbef82e77116/pages/archived-body.md",
  "local_html": "attachments/fbef82e77116/pages/archived-body.html",
  "body_source": "prior_capture"
}