Ejemplo n.º 1
0
def color_crop_ref(tile_info, clr=None):
    """
    Colorizations of a crop_ref (for a given tile)

    Args:
        tile_info: a dictionary that provides all you need to process a tile
        clr (optional): if crop_ref is a pan image, will perform the pansharpening with the color image clr

        If clr is None then:
            case 1: tile is an RGBI image, so removes I channel, and perform rescaling of the remaining channels
            case 2: tile is already an RGB image, so just perform rescaling

        Note that if rescaling is already performed, then the file applied_minmax.txt exists:
            if applied_minmax.txt exists and cfg['skip_existing'] is True, rescaling won't be performed again
            if applied_minmax.txt exists and is different from global_minmax, rescaling will be compulsorily performed (can occur if a new tile is added)
    """
    # get info
    x, y, w, h = tile_info["coordinates"]
    tile_dir = tile_info["directory"]
    z = cfg["subsampling_factor"]

    # paths
    crop_ref = tile_dir + "/roi_ref_crop.tif"
    global_minmax = cfg["out_dir"] + "/global_minmax.txt"
    applied_minmax = tile_dir + "/applied_minmax.txt"

    global_minmax_arr = np.loadtxt(global_minmax)

    if cfg["color_ply"]:

        doProcess = False
        if not os.path.exists(applied_minmax):
            doProcess = True
            applied_minmax_arr = global_minmax_arr
        else:
            applied_minmax_arr = np.loadtxt(applied_minmax)

            if (applied_minmax_arr[0] != global_minmax_arr[0]) or (applied_minmax_arr[1] != global_minmax_arr[1]):
                doProcess = True
                applied_minmax_arr = global_minmax_arr

        if not doProcess and cfg["skip_existing"]:
            print "Rescaling of tile %s already done, skip" % tile_dir
        else:

            crop_color = tile_dir + "/roi_color_ref.tif"
            if clr is not None:
                triangulation.colorize(crop_ref, clr, x, y, z, crop_color, applied_minmax_arr[0], applied_minmax_arr[1])
            else:  # use of image_rescaleintensities
                np.savetxt(applied_minmax, applied_minmax_arr)

                if common.image_pix_dim_tiffinfo(crop_ref) == 4:
                    print "the image is pansharpened fusioned"
                    tmp = common.rgbi_to_rgb(crop_ref, out=None, tilewise=True)
                    # common.image_qauto(tmp, crop_color, tilewise=False)
                    common.image_rescaleintensities(tmp, crop_color, applied_minmax_arr[0], applied_minmax_arr[1])
                else:
                    print "no color data"
                    # common.image_qauto(crop_ref, crop_color, tilewise=False)
                    common.image_rescaleintensities(crop_ref, crop_color, applied_minmax_arr[0], applied_minmax_arr[1])
Ejemplo n.º 2
0
def color_crop_ref(tile_info, clr=None):
    """
    Colorizations of a crop_ref (for a given tile)

    Args:
        tile_info: a dictionary that provides all you need to process a tile
        clr (optional): if crop_ref is a pan image, will perform the pansharpening with the color image clr

        If clr is None then:
            case 1: tile is an RGBI image, so removes I channel, and perform rescaling of the remaining channels
            case 2: tile is already an RGB image, so just perform rescaling

        Note that if rescaling is already performed, then the file applied_minmax.txt exists:
            if applied_minmax.txt exists and cfg['skip_existing'] is True, rescaling won't be performed again
            if applied_minmax.txt exists and is different from global_minmax, rescaling will be compulsorily performed (can occur if a new tile is added)
    """
    # get info
    x, y, w, h = tile_info['coordinates']
    tile_dir = tile_info['directory']
    z = cfg['subsampling_factor']

    # paths
    crop_ref = tile_dir + '/roi_ref_crop.tif'
    global_minmax = cfg['out_dir'] + '/global_minmax.txt'
    applied_minmax = tile_dir + '/applied_minmax.txt'

    global_minmax_arr = np.loadtxt(global_minmax)

    if cfg['color_ply']:

        doProcess = False
        if not os.path.exists(applied_minmax):
            doProcess = True
            applied_minmax_arr = global_minmax_arr
        else:
            applied_minmax_arr = np.loadtxt(applied_minmax)

            if (applied_minmax_arr[0] != global_minmax_arr[0]) or (
                    applied_minmax_arr[1] != global_minmax_arr[1]):
                doProcess = True
                applied_minmax_arr = global_minmax_arr

        if not doProcess and cfg['skip_existing']:
            print 'Rescaling of tile %s already done, skip' % tile_dir
        else:

            crop_color = tile_dir + '/roi_color_ref.tif'
            if clr is not None:
                triangulation.colorize(crop_ref, clr, x, y, z, crop_color,
                                       applied_minmax_arr[0],
                                       applied_minmax_arr[1])
            else:  # use of image_rescaleintensities
                np.savetxt(applied_minmax, applied_minmax_arr)

                if common.image_pix_dim_tiffinfo(crop_ref) == 4:
                    print 'the image is pansharpened fusioned'
                    tmp = common.rgbi_to_rgb(crop_ref, out=None, tilewise=True)
                    #common.image_qauto(tmp, crop_color, tilewise=False)
                    common.image_rescaleintensities(tmp, crop_color,
                                                    applied_minmax_arr[0],
                                                    applied_minmax_arr[1])
                else:
                    print 'no color data'
                    #common.image_qauto(crop_ref, crop_color, tilewise=False)
                    common.image_rescaleintensities(crop_ref, crop_color,
                                                    applied_minmax_arr[0],
                                                    applied_minmax_arr[1])
Ejemplo n.º 3
0
def generate_cloud(out_dir, height_map, rpc1, x, y, w, h, im1, clr,
                   do_offset=False):
    """
    Args:
        out_dir: output directory. The file cloud.ply will be written there
        height_map: path to the height map, produced by the process_pair
            or process_triplet function
        rpc1: path to the xml file containing rpc coefficients for the
            reference image
        x, y, w, h: four integers defining the rectangular ROI in the original
            panchro image. (x, y) is the top-left corner, and (w, h) are the
            dimensions of the rectangle.
        im1:  path to the panchro reference image
        clr:  path to the xs (multispectral, ie color) reference image
        do_offset (optional, default: False): boolean flag to decide wether the
            x, y coordinates of points in the ply file will be translated or
            not (translated to be close to 0, to avoid precision loss due to
            huge numbers)
    """
    print "\nComputing point cloud..."

    # output files
    crop_ref = '%s/roi_ref.tif' % out_dir
    cloud = '%s/cloud.ply' % out_dir
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    # ensure that the coordinates of the ROI are multiples of the zoom factor,
    # to avoid bad registration of tiles due to rounding problems.
    z = cfg['subsampling_factor']
    x, y, w, h = common.round_roi_to_nearest_multiple(z, x, y, w, h)

    # build the matrix of the zoom + translation transformation
    if cfg['full_img'] and z == 1:
        trans = None
    else:
        A = common.matrix_translation(-x, -y)
        f = 1.0/z
        Z = np.diag([f, f, 1])
        A = np.dot(Z, A)
        trans = '%s/trans.txt' % out_dir
        np.savetxt(trans, A)

    # compute offset
    if do_offset:
        r = rpc_model.RPCModel(rpc1)
        lat = r.latOff
        lon = r.lonOff
        off_x, off_y = geographiclib.geodetic_to_utm(lat, lon)[0:2]
    else:
        off_x, off_y = 0, 0

    # crop the ROI in ref image, then zoom
    if cfg['full_img'] and z == 1:
        crop_ref = im1
    else:
        if z == 1:
            common.image_crop_TIFF(im1, x, y, w, h, crop_ref)
        else:
            # gdal is used for the zoom because it handles BigTIFF files, and
            # before the zoom out the image may be that big
            tmp_crop = common.image_crop_TIFF(im1, x, y, w, h)
            common.image_zoom_gdal(tmp_crop, z, crop_ref, w, h)

    if cfg['color_ply']:
        crop_color = '%s/roi_color_ref.tif' % out_dir
        if clr is not None:
            print 'colorizing...'
            triangulation.colorize(crop_ref, clr, x, y, z, crop_color)
        elif common.image_pix_dim_tiffinfo(crop_ref) == 4:
            print 'the image is pansharpened fusioned'
            tmp = common.rgbi_to_rgb(crop_ref, out=None, tilewise=True)
            common.image_qauto(tmp, crop_color, tilewise=False)
        else:
            print 'no color data'
            common.image_qauto(crop_ref, crop_color, tilewise=False)
    else:
        crop_color = ''

    triangulation.compute_point_cloud(cloud, height_map, rpc1, trans, crop_color,
                                      off_x, off_y)
    common.garbage_cleanup()