Ejemplo n.º 1
0
def load_from_json(filename):
    """
    Loads a synthetic MLA dataset

    Parameters:
    -----------

    filename: string
              Filename of the scene configuration file (format: JSON)

    Returns:
    --------

    lenses: dictionary
              Lens dictionary, keys are the axial coordinates
    """

    basedir = os.path.dirname(filename)

    with open(filename, 'r') as f:
        lenses_json = json.load(f)

    lenses = dict()

    if len(lenses_json) == 0:
        return lenses

    diam = lenses_json[0]['diameter']
    radius = diam / 2.0
    inner_radius = radius - lenses_json[0]['lens_border']
    local_grid = rtxlens.LocalLensGrid(diam)

    # local grid coordinates shared by all lenses
    x, y = local_grid.x, local_grid.y
    xx, yy = local_grid.xx, local_grid.yy
    mask = np.zeros_like(local_grid.xx)
    mask[xx**2 + yy**2 < inner_radius**2] = 1

    for lens in lenses_json:
        # image data
        color_filename = "{0}/{1}".format(basedir,
                                          lens['relative_color_filename'])

        # ground truth depth
        depth_filename = "{0}/{1}".format(basedir,
                                          lens['relative_depth_filename'])

        tmp_lens = rtxlens.Lens(lens['axial_coord'], lens['pixel_coord'],
                                lens['diameter'], lens['focal_type'])

        tmp_lens.col_img = plt.imread(color_filename)
        if tmp_lens.col_img.shape[2] == 4:
            tmp_lens.col_img = tmp_lens.col_img[:, :, :3]
        weights = np.array([0.3, 0.59, 0.11])
        tmp_lens.img = np.sum([
            tmp_lens.col_img[:, :, i].astype(np.float64) * weights[i]
            for i in range(3)
        ],
                              axis=0)
        tmp_lens.grid = local_grid
        tmp_lens.mask = mask
        tmp_lens.num_channels = tmp_lens.col_img.shape[2]
        tmp_lens.img_interp = sinterp.RectBivariateSpline(y, x, tmp_lens.img)
        tmp_lens.img_interp3c[0] = sinterp.RectBivariateSpline(
            y, x, tmp_lens.col_img[:, :, 0])
        tmp_lens.img_interp3c[1] = sinterp.RectBivariateSpline(
            y, x, tmp_lens.col_img[:, :, 1])
        tmp_lens.img_interp3c[2] = sinterp.RectBivariateSpline(
            y, x, tmp_lens.col_img[:, :, 2])
        tmp_lens.inner_radius = inner_radius

        tmp_lens.focal_length = lens['focal_length']
        tmp_lens.focus_distance = lens['focus_distance']
        tmp_lens.fstop = lens['fstop']
        tmp_lens.pixel_size = lens['pixel_size']
        tmp_lens.position = np.array(lens['position'])
        #tmp_lens.position = np.array(lens['location'])  # some datasets needs 'location'
        tmp_lens.rotation = np.array(lens['rotation_mat']).reshape((3, 3))
        #tmp_lens.rotation = np.array([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]).reshape((3,3)) #

        tmp_lens.focus_disparity = tmp_lens.focal_length * tmp_lens.diameter / tmp_lens.focus_distance

        # read the ground truth depth. The 16-Bit PNG depth files
        # contain the clipped depth values, convert to the real depth
        # values
        tmp_depth = plt.imread(depth_filename).astype(np.float64)
        tmp_depth = tmp_depth * (lens['clip_end'] -
                                 lens['clip_start']) + lens['clip_start']
        tmp_lens.depth_img = tmp_depth
        tmp_lens.disp_img = np.copy(tmp_depth)
        tmp_lens.disp_img[tmp_depth > 0] = (
            tmp_lens.focal_length *
            tmp_lens.diameter) / tmp_lens.disp_img[tmp_depth > 0]

        # set the lens border
        tmp_lens.lens_border = lens['lens_border']

        # finally add the lens to the dictionary
        lenses[tuple(lens['axial_coord'])] = tmp_lens

    return lenses
Ejemplo n.º 2
0
def load_from_xml(image_filename, config_filename):

    img = plt.imread(image_filename)
    calib = read_calibration(config_filename)

    if len(img.shape) < 3:
        print("This version works only for colored image")
        raise ValueError("Unsopported image dimension {0}".format(img.shape))
    elif len(img.shape) == 3:
        weights = np.array([0.3, 0.59, 0.11])
        data = np.sum([img[:, :, i] * weights[i] for i in range(3)], axis=0)
        data_col = img
    else:
        raise ValueError("Unsupported image dimensions {0}".format(img.shape))

    # the image grid in pixels used for the bivariate spline interpolation
    gridy, gridx = range(data.shape[0]), range(data.shape[1])

    data_interp = sinterp.RectBivariateSpline(gridy, gridx, data)
    data_interp_r = sinterp.RectBivariateSpline(gridy, gridx, data_col[:, :,
                                                                       0])
    data_interp_g = sinterp.RectBivariateSpline(gridy, gridx, data_col[:, :,
                                                                       1])
    data_interp_b = sinterp.RectBivariateSpline(gridy, gridx, data_col[:, :,
                                                                       2])
    img_shape = np.asarray(data.shape[0:2])
    coords = rtxhexgrid.hex_lens_grid(img_shape, calib.lens_diameter,
                                      calib.rot_angle, calib.offset,
                                      calib.lbasis)
    focal_type_offsets = [
        tuple(lens['offset'].values()) for lens in calib.lens_types
    ]
    focal_type_offsets = [(int(p[0]), int(p[1])) for p in focal_type_offsets]
    focal_type_map = {
        _hex_focal_type(p): i
        for i, p in enumerate(focal_type_offsets)
    }

    lenses = dict()
    local_grid = rtxlens.LocalLensGrid(calib.lens_diameter)
    x, y = local_grid.x, local_grid.y
    xx, yy = local_grid.xx, local_grid.yy
    mask = np.zeros_like(local_grid.xx)
    mask[xx**2 + yy**2 < calib.inner_lens_radius**2] = 1

    for lc in coords:

        pc = coords[lc]
        lens = rtxlens.Lens(lcenter=lc,
                            pcenter=pc,
                            diameter=calib.lens_diameter)
        lens.focal_type = _hex_focal_type(lc)
        lens.fstop = lens.diameter
        lens.pixel_size = 1.0
        lens.position = np.array([pc[1], pc[0], 0])
        lens.img = np.ones((local_grid.shape[0], local_grid.shape[1], 3))

        cen_x = round(pc[0])
        cen_y = round(pc[1])
        x1 = int(cen_x + round(np.min(x)))
        x2 = int(cen_x + round(np.max(x)))
        y1 = int(cen_y + round(np.min(y)))
        y2 = int(cen_y + round(np.max(y)))
        lens.num_channels = 3
        lens.img = data_interp(y + pc[0], x + pc[1])
        lens.img_interp = sinterp.RectBivariateSpline(y, x, lens.img)
        # colored version of the interpolated image:
        lens.img_interp3c[0] = data_interp_r
        lens.img_interp3c[1] = data_interp_g
        lens.img_interp3c[2] = data_interp_b
        lens.col_img_uint = data_col[
            x1:x2 + 1, y1:y2 +
            1]  #np.zeros((lens.img.shape[0], lens.img.shape[1], lens.num_channels))

        if lens.col_img_uint.dtype == 'uint8':
            new_col_img = np.zeros((lens.col_img_uint.shape))
            new_col_img = lens.col_img_uint / 255.0
            lens.col_img = new_col_img
        else:
            lens.col_img = lens.col_img_uint

        lens.mask = mask
        lens.grid = local_grid

        # the lens area used for matching
        lens.inner_radius = calib.inner_lens_radius

        # minimum and maximum disparities for this lens
        focal_type = focal_type_map[_hex_focal_type(lc)]
        min_depth = calib.lens_types[focal_type]['depth_range']['min']
        max_depth = calib.lens_types[focal_type]['depth_range']['max']

        lens.min_disp = lens.diameter / max_depth
        lens.max_disp = lens.diameter / min_depth
        #pdb.set_trace()

        lenses[tuple(lc)] = lens

    return lenses