Exemplo n.º 1
0
def calibrate(imagedir, cbrow, cbcol):
    nimages = 0
    datapoints = []
    im_dims = (0,0)
    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = numpy.zeros((cbrow * cbcol, 3), numpy.float32)
    objp[:, :2] = numpy.mgrid[0:cbcol, 0:cbrow].T.reshape(-1, 2)
    # Arrays to store object points and image points from all the images.
    objpoints = [] # 3d point in real world space
    imgpoints = [] # 2d points in image plane.

    files = file_list(imagedir, ['jpg', 'jpeg', 'png'])
    for f in files:
        colour = cv2.imread(f)
        grey = cv2.cvtColor(colour, cv2.COLOR_BGR2GRAY)
        ret, corners = cv2.findChessboardCorners(grey, (cbcol, cbrow), flags=cv2.CALIB_CB_ADAPTIVE_THRESH)
        
        if (ret):
            print('using ' + f)
            cv2.cornerSubPix(grey,corners,(11,11),(-1,-1),(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.01))
            objpoints.append(objp)
            imgpoints.append(corners)
            im_dims = grey.shape[:2]

    if len(imgpoints) == 0:
        print("Not enough good quality images. Aborting")
        return
    
    ret, K, D, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, grey.shape[::-1], None, None)

    # storing results using CameraParams
    C = CameraParams(lens=lens, sensorwidth=sensorwidth, xresolution=im_dims[1], yresolution=im_dims[0])
    C.setParams(K, D)
    C.save(os.path.join(imagedir, "paramsout.json"))
    print("Saved params in " + os.path.join(imagedir, "paramsout.json"))
Exemplo n.º 2
0
def test_cam_params_dict():
    C = CameraParams(lens=4.0, sensorwidth=5.0, xresolution=3000, yresolution=4000)
    C.setParams([[1, 2, 3], [4, 5, 6]], [0, 6, 7, 8])
    Cdict = C.todict()
    C2dict = CameraParams.fromdict(Cdict)
    assert C.todict() == C2dict.todict()
    assert numpy.array_equal(C.K, C2dict.K)
    assert numpy.array_equal(C.D, C2dict.D)
Exemplo n.º 3
0
def test_cam_params_txt():
    C = CameraParams(lens=4.0, sensorwidth=5.0, xresolution=1280, yresolution=960)
    C.setParams([[1, 2, 3], [4, 5, 6]], [0, 6, 7, 8])
    C.save('foo.txt')
    C2 = CameraParams.fromfile('foo.txt')
    assert str(C) == str(C2)
    assert numpy.array_equal(C.K, C2.K)
    assert numpy.array_equal(C.D, C2.D)
    os.remove('foo.txt')
Exemplo n.º 4
0
def test_cam_params_dict():
    C = CameraParams(lens=4.0,
                     sensorwidth=5.0,
                     xresolution=3000,
                     yresolution=4000)
    C.setParams([[1, 2, 3], [4, 5, 6]], [0, 6, 7, 8])
    Cdict = C.todict()
    C2dict = CameraParams.fromdict(Cdict)
    assert C.todict() == C2dict.todict()
    assert numpy.array_equal(C.K, C2dict.K)
    assert numpy.array_equal(C.D, C2dict.D)
Exemplo n.º 5
0
def test_cam_params_txt():
    C = CameraParams(lens=4.0,
                     sensorwidth=5.0,
                     xresolution=1280,
                     yresolution=960)
    C.setParams([[1, 2, 3], [4, 5, 6]], [0, 6, 7, 8])
    C.save('foo.txt')
    C2 = CameraParams.fromfile('foo.txt')
    assert str(C) == str(C2)
    assert numpy.array_equal(C.K, C2.K)
    assert numpy.array_equal(C.D, C2.D)
    os.remove('foo.txt')
Exemplo n.º 6
0
def calibrate(imagedir, cbrow, cbcol):
    nimages = 0
    datapoints = []
    im_dims = (0, 0)
    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = numpy.zeros((cbrow * cbcol, 3), numpy.float32)
    objp[:, :2] = numpy.mgrid[0:cbcol, 0:cbrow].T.reshape(-1, 2)
    # Arrays to store object points and image points from all the images.
    objpoints = []  # 3d point in real world space
    imgpoints = []  # 2d points in image plane.

    files = file_list(imagedir, ['jpg', 'jpeg', 'png'])
    for f in files:
        colour = cv2.imread(f)
        grey = cv2.cvtColor(colour, cv2.COLOR_BGR2GRAY)
        ret, corners = cv2.findChessboardCorners(
            grey, (cbcol, cbrow), flags=cv2.CALIB_CB_ADAPTIVE_THRESH)

        if (ret):
            print('using ' + f)
            cv2.cornerSubPix(
                grey, corners, (11, 11), (-1, -1),
                (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.01))
            objpoints.append(objp)
            imgpoints.append(corners)
            im_dims = grey.shape[:2]

    if len(imgpoints) == 0:
        print("Not enough good quality images. Aborting")
        return

    ret, K, D, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                  grey.shape[::-1], None, None)

    # storing results using CameraParams
    C = CameraParams(lens=lens,
                     sensorwidth=sensorwidth,
                     xresolution=im_dims[1],
                     yresolution=im_dims[0])
    C.setParams(K, D)
    C.save(os.path.join(imagedir, "paramsout.json"))
    print("Saved params in " + os.path.join(imagedir, "paramsout.json"))