Exemplo n.º 1
0
def extract_rays(seq, sub, skip=2, max_frame=None, imtype='png'):
    edge_dir = os.path.join(seq, 'edges', '*.'+imtype)
    paths = glob.glob(edge_dir)
    paths.sort()
    F = len(paths)

    cam = IO.read_cam_param( os.path.join(seq, 'cam.yaml') )
    Rt_path = os.path.join(seq, 'tracking.csv')
    R, t, times, valid = IO.read_tracking(Rt_path)
    valid_bool = zeros(len(R), bool)
    valid_bool[valid] = True

    cloud = RayCloud.RayCloud()
    cloud.set_cam(cam)
    cloud.set_poses(R, t)

    fig = pynutmeg.figure('segments', 'figs/segments.qml')

    if max_frame is None:
        max_frame = F

    print("Estimating bounding box volume from labels")
    frames, boxes = IO.read_rects( os.path.join(seq, 'rects.yaml') )
    cloud.apply_bounding_boxes(frames, boxes)

    print("Extracting Edge Rays")
    for f in range(0, max_frame, skip):
        if not valid_bool[f]:
            continue
        print("\r\tFrame: {} of {}".format(f, F), end=' '*16, flush=True)

        E = IO.imread( paths[f] )
        pts, labels = Contours.find_contours_edge(E, low=20, high=35, min_length=30)
        im = np.empty_like(E)
        im[:] = 255
        im[pts[:,1], pts[:,0]] = 0
        # TODO: Maybe put the next 2 into the one function. This is fine for now though
        pts, labels = Contours.simplify_labeled(pts, labels, eps=1.5)
        tangents, labels = Contours.segment_tangents(pts, labels, thresh=35.)

        # fig.set('ax.im', binary=255-E)
        fig.set('ax.im', binary=im)
        cloud.add_pixels(pts, tangents, f, labels)

    print("\nSaving ray cloud...")
    cloud.save( os.path.join(seq, sub) )