Example #1
0
def calibrate_from_initialization(img,
                                  mask,
                                  A_init,
                                  R_init,
                                  T_init,
                                  edge_sfactor=0.5,
                                  visualize=False):

    h, w = img.shape[:2]

    edges = image_utils.robust_edge_detection(
        cv2.resize(img, None, fx=edge_sfactor, fy=edge_sfactor))

    edges = cv2.resize(edges, None, fx=1. / edge_sfactor, fy=1. / edge_sfactor)
    edges = cv2.Canny(edges.astype(np.uint8) * 255, 100,
                      200) / 255.0  # find edge

    mask = cv2.dilate(mask, np.ones((25, 25), dtype=np.uint8))

    edges = edges * (1 - mask)
    dist_transf = cv2.distanceTransform((1 - edges).astype(np.uint8),
                                        cv2.DIST_L2, 0)  # distance to the edge

    cam_init = cam_utils.Camera('tmp', A_init, R_init, T_init, h, w)
    template, field_mask = draw_utils.draw_field(cam_init)

    II, JJ = (template > 0).nonzero()
    synth_field2d = np.array([[JJ, II]]).T[:, :, 0]

    field3d = cam_utils.plane_points_to_3d(synth_field2d, cam_init)

    A, R, T = _calibrate_camera_dist_transf(A_init, R_init, T_init,
                                            dist_transf, field3d)

    if visualize:
        cam_res = cam_utils.Camera('tmp', A, R, T, h, w)
        field2d, __ = cam_res.project(field3d)
        io.imshow(img, points=field2d)

    return A, R, T, field3d
Example #2
0
def calibrate_by_click(img1,
                       img2,
                       mask1,
                       mask2,
                       edge_sfactor=0.5,
                       field_img_path='./demo/data/field.png'):

    h, w = img1.shape[0:2]

    points2d_img1, points2d_img2, points3d = _set_correspondences(
        img1, img2, field_img_path=field_img_path)

    points2d_list = [points2d_img1, points2d_img2]
    img_list = [img1, img2]
    mask_list = [mask1, mask2]

    # For save the calibration
    A_out, R_out, T_out = [], [], []

    for i, points2d in enumerate(points2d_list):

        # ------------------------------------------------------------------------------------------------------------------
        # OpenCV initial calibration
        fx, fy = cam_utils.grid_search_focal_length(points3d,
                                                    points2d,
                                                    h,
                                                    w,
                                                    same_f=True)
        A = cam_utils.intrinsic_matrix_from_focal_length(fx, fy, h, w)

        points_3d_cv = points3d[:, np.newaxis, :].astype(np.float32)
        points_2d_cv = points2d[:, np.newaxis, :].astype(np.float32)
        print(points2d)

        _, rvec, tvec, _ = cv2.solvePnPRansac(points_3d_cv, points_2d_cv, A,
                                              None)
        rvec, tvec = np.squeeze(rvec), np.squeeze(tvec)
        R, _ = cv2.Rodrigues(rvec)
        T = np.array([tvec]).T
        cam_cv = cam_utils.Camera('tmp', A, R, T, h, w)

        # ------------------------------------------------------------------------------------------------------------------
        # Photometric refinement
        A__, R__, T__, field3d = calibrate_from_initialization(
            img_list[i], mask_list[i], A, R, T, edge_sfactor)
        cam = cam_utils.Camera('tmp', A__, R__, T__, h, w)

        # Sanity check, project the soccer field points
        p2_cv, _ = cam_cv.project(field3d)
        p2_cv, _ = cam_utils.inside_frame(p2_cv, cam.height, cam.width)

        p2_opt, _ = cam_cv.project(field3d)
        p2_opt, valid_id = cam_utils.inside_frame(p2_opt, cam_cv.height,
                                                  cam_cv.width)

        class Index(object):
            def save_opt(self, event):
                A_out.append(cam.A)
                R_out.append(cam.R)
                T_out.append(cam.T)
                plt.close()

            def save_pnp(self, event):
                A_out.append(cam_cv.A)
                R_out.append(cam_cv.R)
                T_out.append(cam_cv.T)
                plt.close()

            def discard(self, event):
                A_out.append()
                R_out.append()
                T_out.append()
                plt.close()

        fig, ax = plt.subplots(1, 2)
        io_utils.imshow(img_list[i], ax=ax[0], points=p2_opt)
        io_utils.imshow(img_list[i], ax=ax[1], points=p2_cv)
        callback = Index()
        axdisc = plt.axes([0.6, 0.05, 0.1, 0.075])
        axcv = plt.axes([0.7, 0.05, 0.1, 0.075])
        axopt = plt.axes([0.81, 0.05, 0.1, 0.075])
        bnext = Button(axopt, 'Save opt')
        bnext.on_clicked(callback.save_opt)
        bprev = Button(axdisc, 'Discard')
        bprev.on_clicked(callback.discard)
        bpcv = Button(axcv, 'Save cv')
        bpcv.on_clicked(callback.save_pnp)
        plt.show()

    return A_out, R_out, T_out, [points2d_img1, points2d_img2, points3d]
Example #3
0
def calibrate_from_initialization(img,
                                  mask,
                                  A_init,
                                  R_init,
                                  T_init,
                                  edge_sfactor=0.5,
                                  visualize=False):
    # set to 1 to use the improvd calibration propagation
    improved_method = 0
    validate_pixels = 0  # check if segmented line pixels are on a white line, slow

    h, w = img.shape[:2]

    if improved_method:
        G_mag, G_direction = get_image_gradients(img)

        field_green = get_field_green(img)

        edges = get_field_markings(G_mag, field_green, mask)

        if validate_pixels:
            edges = validate_white_line(img, G_mag, G_direction, edges,
                                        field_green)

        dist_transf = cv2.distanceTransform((1 - edges).astype(np.uint8),
                                            cv2.DIST_L2, 0)
        cam_init = cam_utils.Camera('tmp', A_init, R_init, T_init, h, w)
        template, field_mask = draw_utils.draw_field(cam_init)

        II, JJ = (template > 0).nonzero()
        synth_field2d = np.array([[JJ, II]]).T[:, :, 0]

        field3d = cam_utils.plane_points_to_3d(synth_field2d, cam_init)

        A, R, T = _calibrate_camera_dist_transf(A_init, R_init, T_init,
                                                dist_transf, field3d)
        if visualize:
            cam_res = cam_utils.Camera('tmp', A, R, T, h, w)
            field2d, __ = cam_res.project(field3d)
            io.imshow(img, points=field2d)

    else:
        edges = image_utils.robust_edge_detection(
            cv2.resize(img, None, fx=edge_sfactor, fy=edge_sfactor))

        edges = cv2.resize(edges,
                           None,
                           fx=1. / edge_sfactor,
                           fy=1. / edge_sfactor)
        edges = cv2.Canny(edges.astype(np.uint8) * 255, 100, 200) / 255.0

        mask = cv2.dilate(mask, np.ones((25, 25), dtype=np.uint8))

        edges = edges * (1 - mask)
        dist_transf = cv2.distanceTransform((1 - edges).astype(np.uint8),
                                            cv2.DIST_L2, 0)

        cam_init = cam_utils.Camera('tmp', A_init, R_init, T_init, h, w)
        template, field_mask = draw_utils.draw_field(cam_init)

        II, JJ = (template > 0).nonzero()
        synth_field2d = np.array([[JJ, II]]).T[:, :, 0]

        field3d = cam_utils.plane_points_to_3d(synth_field2d, cam_init)

        A, R, T = _calibrate_camera_dist_transf(A_init, R_init, T_init,
                                                dist_transf, field3d)

        if visualize:
            cam_res = cam_utils.Camera('tmp', A, R, T, h, w)
            field2d, __ = cam_res.project(field3d)
            io.imshow(img, points=field2d)

    return A, R, T, field3d