【问题标题】:Is my code doing camera calibration correctly?我的代码是否正确进行了相机校准?
【发布时间】:2019-11-26 21:31:20
【问题描述】:

我已经阅读了每一个相机校准和 3D 重建,但我找不到比文档更多的代码的人。因此,话虽如此,我有我的相机校准代码,我在其中拍摄了多张照片,这些照片是为相机校准而保存的。我确信除了实际的相机校准外一切正常。每当我使用 Cv2.undistort 进行不失真处理时,我永远不会得到一个好的图像,而是得到一个甚至不是棋盘一部分的块。我做错了什么吗?

import numpy as np
import cv2

cam = cv2.VideoCapture(0)

cv2.namedWindow("test")

img_counter = 0
imgNames = []
size = (7,5)
while True:
    ret, frame = cam.read()
    gray =  cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow("test", gray)
    if not ret:
        break
    k = cv2.waitKey(1)
    if k%256 == 27:
        break
    elif k%256 == 32:
        img_name = "{}.png".format(img_counter)
        cv2.imwrite(img_name, frame) 
        imgtemp = cv2.imread(img_name)
        graytemp = cv2.cvtColor(imgtemp,cv2.COLOR_BGR2GRAY)
        ret, corners = cv2.findChessboardCorners(graytemp, size,None)
        print (ret)
        if ret == True:
            print ("good!")
            imgNames.append(img_name)
            cv2.imwrite(img_name, frame)
            img_counter += 1
        else:
            print ("again")

cam.release()
cv2.destroyAllWindows()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 
0.001)
objp = np.zeros((7*5,3), np.float32)
objp[:,:2] = np.mgrid[0:size[0],0:size[1]].T.reshape(-1,2) #multiply by 
length
objpoints = [] 
imgpoints = [] 

for fname in imgNames:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret, corners = cv2.findChessboardCorners(gray,size,None)

    if ret == True:
        objpoints.append(objp)
        corners2 = cv2.cornerSubPix(gray,corners,(11,11), 
(-1,-1),criteria)
        imgpoints.append(corners2)
    ret,mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, 
imgpoints,gray.shape[::-1], None, None)
    img = cv2.imread(fname)
    h,  w = img.shape[:2]
    newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1, 
(w,h))
    # undistort
    dst = cv2.undistort(img, mtx, dist, None, newcameramtx)

    # crop the image
    x,y,w,h = roi
    dst = dst[y:y+h, x:x+w]
    cv2.imshow('calibresult.png',dst)
    #Save parameters into numpy file
    np.save("ret", ret)
    np.save("mtx", mtx)
    np.save("dist", dist)
    np.save("rvecs", rvecs)
    np.save("tvecs", tvecs)

应该发生的是我得到一个未失真的图像并且矩阵被保存。但是,图像是随机的,我不明白出了什么问题。我查看了在 Google 上的相机校准下弹出的所有内容以及所有堆栈溢出问题。 我正在使用https://medium.com/@omar.ps16/stereo-3d-reconstruction-with-opencv-using-an-iphone-camera-part-ii-77754b58bfe0https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_calib3d/py_calibration/py_calibration.html

提前致谢! 也很抱歉发布这么多问题。我只是在任何地方都找不到答案。

【问题讨论】:

    标签: opencv camera-calibration


    【解决方案1】:

    在您从所有图像中附加objpointsimgpoints 之后,您应该执行cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)。它不应该在 for 循环中。

    此外,请确保您有足够的图像以获得良好的校准结果。

    【讨论】:

      猜你喜欢
      • 2015-09-16
      • 2012-01-26
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      相关资源
      最近更新 更多