• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    OpenCV实现相机校正
    POST TIME:2021-10-18 13:55

    本文实例为大家分享了OpenCV实现相机校正的具体代码,供大家参考,具体内容如下

    1. 相机标定

    根据张正友校正算法,利用棋盘格数据校正对车载相机进行校正,计算其内参矩阵,外参矩阵和畸变系数。

    标定的流程是:

    2.关于相机校正用到的几个API:

    1、寻找棋盘图中的棋盘角点

    rect, corners = cv2.findChessboardCorners(image, pattern_size, flags)

    参数:

          CV_CALIB_CB_ADAPTIVE_THRESH :使用自适应阈值(通过平均图像亮度计算得到)将图像转换为黑白图,而不是一个固定的阈值。

            CV_CALIB_CB_NORMALIZE_IMAGE :在利用固定阈值或者自适应的阈值进行二值化之前,先使用cvNormalizeHist来均衡化图像亮度。

            CV_CALIB_CB_FILTER_QUADS :使用其他的准则(如轮廓面积,周长,方形形状)来去除在轮廓检测阶段检测到的错误方块。

    返回:

    2、检测完角点之后可以将测到的角点绘制在图像上,使用的API是:

    cv2.drawChessboardCorners(img, pattern_size, corners, rect)

    参数:

    注意:如果发现了所有的角点,那么角点将用不同颜色绘制(每行使用单独的颜色绘制),并且把角点以一定顺序用线连接起来。

    3利用定标的结果计算内外参数

    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(object_points, image_points, image_size, None, None)

    参数:

    返回:

    2.1 图像去畸变

    上一步中得到相机的内参及畸变系数,利用其进行图像的去畸变,最直接的方法就是调用opencv中的函数得到去畸变的图像:

    def img_undistort(img, mtx, dist):
        dst = cv2.undistort(img, mtx, dist, None, mtx)
        return dst

    求畸变的API:

    dst = cv2.undistort(img, mtx, dist, None, mtx)

    参数:

    返回:

    3. 相机校正

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import glob
    
    def plot_contrast_imgs(origin_img, converted_img, origin_img_title="origin_img", converted_img_title="converted_img", converted_img_gray=False):
        """
        用于对比显示两幅图像
        """
        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 20))
        ax1.set_title(origin_img_title)
        ax1.imshow(origin_img)
        ax2.set_title(converted_img_title)
        if converted_img_gray==True:
            ax2.imshow(converted_img, cmap="gray")
        else:
            ax2.imshow(converted_img)
        plt.show()
    
    # 1. 参数设定:定义棋盘横向和纵向的角点个数并指定校正图像的位置
    nx = 9
    ny = 6
    file_paths = glob.glob("./camera_cal/calibration*.jpg")
    # 2. 计算相机的内外参数及畸变系数
    def cal_calibrate_params(file_paths):
        object_points = []  # 三维空间中的点:3D
        image_points = []   # 图像空间中的点:2d
        # 2.1 生成真实的交点坐标:类似(0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)的三维点
        objp = np.zeros((nx * ny, 3), np.float32)
        objp[:, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2)
        # 2.2 检测每幅图像角点坐标
        for file_path in file_paths:
            img = cv2.imread(file_path)
            # 将图像转换为灰度图
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # 自动检测棋盘格内4个棋盘格的角点(2白2黑的交点)
            rect, corners = cv2.findChessboardCorners(gray, (nx, ny), None)
            # 若检测到角点,则将其存储到object_points和image_points
            if rect == True:
                object_points.append(objp)
                image_points.append(corners)
        # 2.3 获取相机参数
        ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(object_points, image_points, gray.shape[::-1], None, None)
        return ret, mtx, dist, rvecs, tvecs
    
    
    def img_undistort(img, mtx, dist):
        """
        图像去畸变
        """
        return cv2.undistort(img, mtx, dist, None, mtx)
    
    # 测试去畸变函数的效果
    file_paths = glob.glob("./camera_cal/calibration*.jpg")
    ret, mtx, dist, rvecs, tvecs = cal_calibrate_params(file_paths)
    if mtx.any() != None:  # a.any() or a.all()
        img = mpimg.imread("./camera_cal/calibration1.jpg")
        undistort_img = img_undistort(img, mtx, dist)
        plot_contrast_imgs(img, undistort_img)
        print("done!")
    else:
        print("failed")

    执行代码:

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    您可能感兴趣的文章:
    • Python opencv相机标定实现原理及步骤详解
    上一篇:Keras 如何修改图片通道的顺序
    下一篇:教你怎么用python连接sql server
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信