我无法创建深度图(Opencv,Python)。 Stereopair校准

时间:2017-05-15 13:00:16

标签: python opencv ubuntu

我尝试校准立体像素并创建深度地图以计算到对象的距离。我无法创建深度图我得到了这个结果:

Result

虽然结果必须是这个:

must be

import cv2
    import numpy as np
    import glob

    cap = cv2.VideoCapture(1)
    cap2 = cv2.VideoCapture(2)


    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1)

    object_point = np.zeros((6*8, 3), np.float32)
    object_point[:, :2] = np.mgrid[0:8, 0:6].T.reshape(-1, 2)


    object_points = []
    object_points2 = []
    image_points_left = []
    image_points_right = []

    while(1):
        _, frame = cap.read()
        image = frame
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        h, w = gray.shape[:2]

        _, frame2 = cap2.read()
        image2 = frame2

        gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
        cv2.imshow('image', image)  
        cv2.imshow('image2', image2)



        ret, cornersL = cv2.findChessboardCorners(gray, (8, 6), cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_NORMALIZE_IMAGE)
        ret2, cornersR = cv2.findChessboardCorners(gray2, (8, 6), cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_NORMALIZE_IMAGE)

        # ret, cornersL = cv2.findChessboardCorners(gray, (8, 6), cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FILTER_QUADS)
        # ret2, cornersR = cv2.findChessboardCorners(gray2, (8, 6),cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_NORMALIZE_IMAGE)



        if ret == True and ret2 == True:
            object_points.append(object_point)
            cv2.cornerSubPix(gray, cornersL, (5, 5), (-1, -1), criteria)
            image_points_left.append(cornersL)

            #object_points2.append(object_point)
            cv2.cornerSubPix(gray2, cornersR, (5, 5), (-1, -1), criteria)
            image_points_right.append(cornersR)

            cv2.drawChessboardCorners(image, (8, 6), cornersL, ret)
            cv2.drawChessboardCorners(image2, (8, 6), cornersR, ret2)
            cv2.imshow('image', image)
            cv2.imshow('image2', image2)

        k = cv2.waitKey(40)
        if (k == 27):
            cv2.destroyAllWindows()
            break


#np.object_points = [object_points , object_points2]


retval, cameraMatrixL, distCoeffsL, rvecs, tvecs = cv2.calibrateCamera(object_points, image_points_left, (640,480), None, None)
retval2, cameraMatrixR, distCoeffsR, rvecs2, tvecs2 = cv2.calibrateCamera(object_points, image_points_right, (640,480), None, None)



#ret , cameraMatrix, distCoeffs, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoRectify(object_points , image_points ,image_points2 ,cameraMatrix , distCoeffs , cameraMatrix2 , distCoeffs2 ,(640,480))

stereocalib_criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 100, 1e-5)
stereocalib_flags = cv2.CALIB_FIX_ASPECT_RATIO + cv2.CALIB_ZERO_TANGENT_DIST + cv2.CALIB_USE_INTRINSIC_GUESS + cv2.CALIB_SAME_FOCAL_LENGTH + cv2.CALIB_RATIONAL_MODEL + cv2.CALIB_FIX_K3 + cv2.CALIB_FIX_K4 + cv2.CALIB_FIX_K5
stereocalib_flags1 =cv2.CALIB_FIX_ASPECT_RATIO + cv2.CALIB_ZERO_TANGENT_DIST + cv2.CALIB_USE_INTRINSIC_GUESS + cv2.CALIB_SAME_FOCAL_LENGTH + cv2.CALIB_RATIONAL_MODEL



ret , cameraMatrixL, distCoeffsL, cameraMatrixR, distCoeffsR, R, T, E, F = cv2.stereoCalibrate(object_points , image_points_left ,image_points_right
,cameraMatrixL , distCoeffsL , cameraMatrixR , distCoeffsR ,(640,480),flags = stereocalib_flags1,criteria = stereocalib_criteria)



R1, R2, P1, P2, Q, roi, roi2 = cv2.stereoRectify(cameraMatrixL, distCoeffsL, cameraMatrixR,
                                                                     distCoeffsR, (640, 480), R, T, cv2.CALIB_ZERO_DISPARITY,alpha= 0.5 )


# newcameraMatrix, roi =cv2.getOptimalNewCameraMatrix(cameraMatrixL,distCoeffsL,(640, 480),1)
# newcameraMatrix2, roi2 =cv2.getOptimalNewCameraMatrix(cameraMatrixR,distCoeffsR,(640, 480),1)




while(1):
    _, frame = cap.read()
    image = frame

    _, frame2 = cap2.read()
    image2=frame2

    cv2.imshow("left1", image)
    cv2.imshow("right1", image2)



    mapxL, mapyL = cv2.initUndistortRectifyMap(cameraMatrixL, distCoeffsL, R1, P1, (640, 648), cv2.CV_32FC1)
    mapxR, mapyR = cv2.initUndistortRectifyMap(cameraMatrixR, distCoeffsR, R2, P2, (640, 480), cv2.CV_32FC1)

    dst = cv2.remap(image, mapxL, mapyL, cv2.INTER_LINEAR)
    dst2 = cv2.remap(image2, mapxR, mapyR, cv2.INTER_LINEAR)

    # mapx, mapy = cv2.initUndistortRectifyMap(cameraMatrixL, distCoeffsL, None,newcameraMatrix, (640, 648), cv2.CV_32FC1)
    # dst = cv2.remap(image, mapxL, mapyL, cv2.INTER_LINEAR)
    # mapx2, mapy2 = cv2.initUndistortRectifyMap(cameraMatrixR, distCoeffsR, None, newcameraMatrix2, (640, 480), cv2.CV_32FC1)
    # dst2 = cv2.remap(image2, mapxR, mapyR, cv2.INTER_LINEAR)


    x, y, w, h = roi
    dst = dst[y:y + h, x:x + w]
    dst = dst[0:w]

    x2, y2, w2, h2 = roi2
    dst2 = dst2[y2:y2 + h2, x2:x2 + w2]
    dst2 = dst2[0:w2]

    dst = cv2.resize(dst, (640, 480))
    dst2 = cv2.resize(dst2, (640, 480))


    cv2.imshow("left22", dst)
    cv2.imshow("right2", dst2)


    stereo = cv2.StereoSGBM_create(
        minDisparity=-16,
        numDisparities=96,
        uniquenessRatio=1,
        speckleWindowSize=150,
        speckleRange=2,
        disp12MaxDiff=10,
        P1=600,
        P2=2400
        , blockSize=5, preFilterCap=4)

    disparity = stereo.compute(dst, dst2)
    qw = cv2.normalize(disparity, disparity, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    cv2.imshow('disparity', qw)
    print(disparity[320][240])

    k = cv2.waitKey(1)
    if (k == 27):
        cv2.destroyAllWindows()
        break

这张图片对我来说是最好的结果。我需要大约十个时间才能得到这个结果:

原始图片:

纠正和解除前的图像:

[纠正和解除之前的图像:] [4]

深度图:

1 个答案:

答案 0 :(得分:0)