コード例 #1
0
def add_views_to_depth_pruner(data: UndistortedDataSet, neighbors, dp):
    for shot in neighbors:
        if not data.clean_depthmap_exists(shot.id):
            continue
        depth, plane, score = data.load_clean_depthmap(shot.id)
        height, width = depth.shape
        color_image = data.load_undistorted_image(shot.id)
        labels = load_segmentation_labels(data, shot)
        height, width = depth.shape
        image = scale_down_image(color_image, width, height)
        labels = scale_image(labels, image.shape[1], image.shape[0],
                             cv2.INTER_NEAREST)
        K = shot.camera.get_K_in_pixel_coordinates(width, height)
        R = shot.pose.get_rotation_matrix()
        t = shot.pose.translation
        dp.add_view(K, R, t, depth, plane, image, labels)
コード例 #2
0
ファイル: dense.py プロジェクト: alexwangxiang/OpenSfM
def add_views_to_depth_estimator(data: UndistortedDataSet, neighbors, de):
    """Add neighboring views to the DepthmapEstimator."""
    num_neighbors = data.config["depthmap_num_matching_views"]
    for shot in neighbors[:num_neighbors + 1]:
        assert shot.camera.projection_type == "perspective"
        color_image = data.load_undistorted_image(shot.id)
        mask = load_combined_mask(data, shot)
        gray_image = cv2.cvtColor(color_image, cv2.COLOR_RGB2GRAY)
        original_height, original_width = gray_image.shape
        width = min(original_width, int(data.config["depthmap_resolution"]))
        height = width * original_height // original_width
        image = scale_down_image(gray_image, width, height)
        mask = scale_down_image(mask, width, height, cv2.INTER_NEAREST)
        K = shot.camera.get_K_in_pixel_coordinates(width, height)
        R = shot.pose.get_rotation_matrix()
        t = shot.pose.translation
        de.add_view(K, R, t, image, mask)
コード例 #3
0
def export(
    reconstruction,
    index,
    image_graph,
    tracks_manager,
    base_output_path,
    data: DataSet,
    undistorted,
    udata: UndistortedDataSet,
    with_points,
    export_only,
):
    logger.info("Reconstruction %d" % index)
    output_path = os.path.join(base_output_path, "recon%d" % index)
    io.mkdir_p(output_path)
    io.mkdir_p(os.path.join(output_path, "visualize"))
    io.mkdir_p(os.path.join(output_path, "txt"))
    io.mkdir_p(os.path.join(output_path, "models"))

    shot_index = {image: i for i, image in enumerate(reconstruction.shots)}

    fvis = open(os.path.join(output_path, "vis.dat"), "w")
    fvis.write("VISDATA\n")
    fvis.write("%d\n" % len(shot_index))

    for image, i in shot_index.items():
        shot = reconstruction.shots[image]
        base = "%08d" % i
        logger.info("Image: %s %s" % (image, base))

        # vis.dat for this image
        if image_graph:
            adj_indices = []
            for adj_image in image_graph[image]:
                weight = image_graph[image][adj_image]["weight"]
                if weight > 0 and adj_image in shot_index:
                    adj_indices.append(shot_index[adj_image])

            num_covisible = len(adj_indices)
            fvis.write("%d " % i)
            fvis.write("%d " % num_covisible)
            for ai in adj_indices:
                fvis.write("%d " % ai)
            fvis.write("\n")

        # radially undistort the original image
        camera = shot.camera
        if undistorted:
            undistorted_image = udata.load_undistorted_image(image)
        else:
            original_image = data.load_image(image)[:, :, ::-1]
            original_h, original_w = original_image.shape[:2]
            K = camera.get_K_in_pixel_coordinates(original_w, original_h)
            distortion = np.array([camera.k1, camera.k2, 0, 0])
            undistorted_image = cv2.undistort(original_image, K, distortion)

        # resize and save the undistorted to visualize/%08d.jpg
        resized_image = features.resized_image(
            undistorted_image, data.config["feature_process_size"])
        new_image_path = os.path.join(output_path, "visualize", base + ".jpg")
        cv2.imwrite(new_image_path, resized_image)

        # write camera projection matrix to txt/%08d.txt
        resized_h, resized_w = resized_image.shape[:2]
        resized_K = camera.get_K_in_pixel_coordinates(resized_w, resized_h)
        P = resized_K.dot(shot.pose.get_world_to_cam()[:3])

        new_txt = os.path.join(output_path, "txt", base + ".txt")
        with open(new_txt, "wb") as f:
            np.savetxt(f, P, str("%f"), header="CONTOUR")

    fvis.close()

    # txt
    with open(os.path.join(output_path, "pmvs_txt"), "w") as f:
        f.write("level 1\n")
        f.write("csize 2\n")
        f.write("threshold 0.7\n")
        f.write("wsize 7\n")
        f.write("minImageNum 3\n")
        f.write("CPU 8\n")
        f.write("setEdge 0\n")
        f.write("useBound 0\n")
        f.write("useVisData {}\n".format(int(image_graph is not None)))
        f.write("sequence -1\n")
        f.write("timages -1 0 %d\n" % len(shot_index))
        f.write("oimages 0\n")