[openCV/Python]基于openCV的单目摄像头标定校正系统V1

 目录:

一、import相关库

二、输入棋盘格内角点

三、创建vector以存储每个棋盘图像的3D点矢量

四、创建vector以存储每个棋盘图像的2D点矢量

五、为三维点定义世界坐标系

六、读取图片并进行标定

七、输出相机参数

八、读取待校正图片并读取参数

九、利用undistort函数进行畸变矫正

十、计算反向投影误差

温馨提示:本文仅根据摄像头已采集的图片(多角度拍摄的棋盘格)进行相关操作,摄像头调用及采集教程详情见《[openCV/Python]基于openCV的图片素材采集系统V3》


一、import相关库

cv2需要额外下载opencv-python库,详情见《[Python]如何在新版Pycharm中配置pip源与安装openCV等库》

import cv2
import numpy as np
import glob

二、输入棋盘格内角点

# Defining the dimensions of checkerboard
CHECKERBOARD = (8,6)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

 注:如图为9X7的棋盘格,内角点为8X6


三、创建vector以存储每个棋盘图像的3D点矢量

# Creating vector to store vectors of 3D points for each checkerboard image
objpoints = []

四、创建vector以存储每个棋盘图像的2D点矢量

# Creating vector to store vectors of 2D points for each checkerboard image
imgpoints = []

五、为三维点定义世界坐标系

# Defining the world coordinates for 3D points
objp = np.zeros((1, CHECKERBOARD[0] * CHECKERBOARD[1], 3), np.float32)
objp[0, :, :2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
prev_img_shape = None

六、读取图片并进行标定

# Extracting path of individual image stored in a given directory
images = glob.glob('./calibration/*.jpg')
for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Find the chess board corners
    # If desired number of corners are found in the image then ret = true
    ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD,
                                             cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE)

    """
    If desired number of corner are detected,
    we refine the pixel coordinates and display 
    them on the images of checker board
    """
    if ret == True:
        objpoints.append(objp)
        # refining pixel coordinates for given 2d points.
        corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)

        imgpoints.append(corners2)

        # Draw and display the corners
        img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)

    cv2.imshow('img', img)
    cv2.waitKey(0)

cv2.destroyAllWindows()

h, w = img.shape[:2]

七、输出相机参数

print("Camera matrix : \n")
print(mtx)
print("dist : \n")
print(dist)
print("rvecs : \n")
print(rvecs)
print("tvecs : \n")
print(tvecs)
print("-----------------------------------------------------")

八、读取待校正图片并读取参数

img = cv2.imread(images[0])
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))  # 显示更大范围的图片(正常重映射之后会删掉一部分图像)
print(newcameramtx)

九、利用undistort函数进行畸变矫正

print("------------------使用undistort函数-------------------")
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
cv2.imshow("dst", dst)
print("dst的大小为:", dst.shape)
cv2.waitKey(0)
cv2.destroyAllWindows()

十、计算反向投影误差

print("-------------------计算反向投影误差-----------------------")
tot_error = 0
for i in range(len(objpoints)):
    img_points2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
    error = cv2.norm(imgpoints[i], img_points2, cv2.NORM_L2) / len(img_points2)
    print("error of " + str(i) + ": ", error)
    tot_error += error

mean_error = tot_error / len(objpoints)
print("total error: ", tot_error)
print("mean error: ", mean_error)

注:实测经正常操作mean error(平均反向投影误差)在0.025左右


源代码:

import cv2
import numpy as np
import glob

# Defining the dimensions of checkerboard
CHECKERBOARD = (8,6)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# Creating vector to store vectors of 3D points for each checkerboard image
objpoints = []
# Creating vector to store vectors of 2D points for each checkerboard image
imgpoints = []

# Defining the world coordinates for 3D points
objp = np.zeros((1, CHECKERBOARD[0] * CHECKERBOARD[1], 3), np.float32)
objp[0, :, :2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
prev_img_shape = None

# Extracting path of individual image stored in a given directory
images = glob.glob('./calibration/*.jpg')
for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Find the chess board corners
    # If desired number of corners are found in the image then ret = true
    ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD,
                                             cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE)

    """
    If desired number of corner are detected,
    we refine the pixel coordinates and display 
    them on the images of checker board
    """
    if ret == True:
        objpoints.append(objp)
        # refining pixel coordinates for given 2d points.
        corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)

        imgpoints.append(corners2)

        # Draw and display the corners
        img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)

    cv2.imshow('img', img)
    cv2.waitKey(0)

cv2.destroyAllWindows()

h, w = img.shape[:2]

"""
Performing camera calibration by 
passing the value of known 3D points (objpoints)
and corresponding pixel coordinates of the 
detected corners (imgpoints)
"""
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)

print("Camera matrix : \n")
print(mtx)
print("dist : \n")
print(dist)
print("rvecs : \n")
print(rvecs)
print("tvecs : \n")
print(tvecs)
print("-----------------------------------------------------")

# 畸变校正
img = cv2.imread(images[0])
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))  # 显示更大范围的图片(正常重映射之后会删掉一部分图像)
print(newcameramtx)

print("------------------使用undistort函数-------------------")
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
cv2.imshow("dst", dst)
print("dst的大小为:", dst.shape)
cv2.waitKey(0)
cv2.destroyAllWindows()

print("-------------------计算反向投影误差-----------------------")
tot_error = 0
for i in range(len(objpoints)):
    img_points2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
    error = cv2.norm(imgpoints[i], img_points2, cv2.NORM_L2) / len(img_points2)
    print("error of " + str(i) + ": ", error)
    tot_error += error

mean_error = tot_error / len(objpoints)
print("total error: ", tot_error)
print("mean error: ", mean_error)

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雕雀桑

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值