【计算机视觉】OpenCV实现单目相机标定

打印 上一主题 下一主题

主题 578|帖子 578|积分 1734

文章目录


单目相机标定(基于Python OpenCV)

1.上期填坑

在开始本篇博客之前,先填一下上一篇博客【计算机视觉】基于ORB角点+RANSAC算法实现图像全景拼接的坑(不算填吧,算做一个记录,因为并没有解决问题,留着看以后有没有空解决??),不想看的可以直接跳到下一节。
首先解决如何拼接多张图像(上篇博客只能拼接两张图像,多张图像需要保存两张图像的匹配结果,再重新读取计算角点)
改进的方法的基本思路是,仅计算当前两张图像的单应变换M_current,通过先前的单应矩阵M_before再计算一个累积变换,最终通过矩阵乘法得到的M的变换就延续了当前变换的累积变换矩阵:
  1.     # 一开始无先前变换,因此设置为单位阵
  2.     M_before = np.eye(3,3)
  3.     result = cv2.imread('datas/1.jpg')
  4.     # result = CalcUndistort(result, mtx, dist)
  5.     result,_,_ = utils.auto_reshape(result, 1080)
  6.     img2 = result
  7.     cors2, desc2= extraORBfromImg(orb, img2)
  8.     for i in range(1,6):
  9.         print(i)
  10.         img1 = cv2.imread('datas/'+str(i+1)+'.jpg')
  11.         # img1 = CalcUndistort(img1, mtx, dist)
  12.         img1,_,_ = utils.auto_reshape(img1, 1080)
  13.         cors1, desc1= extraORBfromImg(orb, img1)
  14.         
  15.         match_dist, match_idx = ORBMatch(match, desc1, desc2)
  16.         # 得到匹配点对的坐标
  17.         match_pts = findMatchCord(match_idx, cors1, cors2)
  18.         # 可视化匹配点
  19.         # utils.drawMatches(img1, img2, cors1, cors2, match_idx)
  20.         # RANSAC迭代去除异常点对
  21.         update_match_pts = RANSAC(match_pts)
  22.         # 最小二乘方法计算单应矩阵
  23.         M = calc_homography(update_match_pts)
  24.         # 图像拼接结果可视化, 并传递累积单应变换矩阵M ,返回先前累积拼接结果result
  25.         result, M = homography_trans(M, M_before, img1, result)
  26.         M_before = M
  27.         # 不用再提取一遍:
  28.         img2 = img1
  29.         cors2, desc2 = cors1, desc1
复制代码
值得注意的是,代码采用的匹配顺序是从右至左,为了保证每次只提取最左侧图像的角点,img1对于图像的拍摄时序应该要在img2的左侧
相应的,函数homography_trans也要做修改:
[code]# 可视化图像对映射效果def homography_trans(M, M_before, img1, img2):    M = M_before @ M    # out_img 第一张图像映射到第二张    x_min, x_max, y_min, y_max, M2 = calc_border(M, img1.shape)    # 透视变换+平移变换(使得图像在正中央)    M = M2 @ M    w, h = int(round(x_max)-round(x_min)), int(round(y_max)-round(y_min))    out_img = cv2.warpPerspective(img1, M, (w, h))    # print(out_img.shape)    # cv2.imshow('ww',out_img)    # cv2.waitKey(0)    # 调整两张图像位姿一致:    # x方向    out_img_blank_x = np.zeros((out_img.shape[0], abs(round(x_min)), 3)).astype(np.uint8)    img2_blank_x = np.zeros((img2.shape[0], abs(round(x_min)), 3)).astype(np.uint8)    if(x_min>0):        # print(1)        out_img = cv2.hconcat((out_img_blank_x, out_img))    if(x_min0):        # print(3)        out_img = cv2.vconcat((out_img, out_img_blank_y))    if(y_min
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

勿忘初心做自己

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表