<commit_before>#!/usr/bin/env python

# Copyright (C) 2013, <NAME> <<EMAIL>>
# Copyright (C) 2013, <NAME> <<EMAIL>>
# Copyright (C) 2013, <NAME> <<EMAIL>>

import multiprocessing
import sys
import json
import numpy as np
import os.path
import copy
import operator

from python import tee
from python import common
from python import rpc_model
from python import rpc_utils
from python import geographiclib
from python import pointing_accuracy
from python import rectification
from python import block_matching
from python import triangulation
from python import tile_composer
from python import fusion
from python.config import cfg



def process_pair_single_tile(out_dir, img1, rpc1, img2, rpc2, x=None, y=None,
        w=None, h=None, prv1=None, cld_msk=None, roi_msk=None, A=None):
    """
    Computes a disparity map from a Pair of Pleiades images, without tiling

    Args:
        out_dir: path to the output directory
        img1: path to the reference image.
        rpc1: paths to the xml file containing the rpc coefficients of the
            reference image
        img2: path to the secondary image.
        rpc2: paths to the xml file containing the rpc coefficients of the
            secondary image
        x, y, w, h: four integers defining the rectangular ROI in the reference
            image. (x, y) is the top-left corner, and (w, h) are the dimensions
            of the rectangle.
        prv1 (optional): path to a preview of the reference image
        cld_msk (optional): path to a gml file containing a cloud mask
        roi_msk (optional): path to a gml file containing a mask defining the
            area contained in the full image.
        A (optional, default None): pointing correction matrix. If None, it
            will be estimated by this function.

    Returns:
        nothing
    """
    # create a directory for the experiment
    common.run('mkdir -p %s' % out_dir)

    # output files
    rect1 = '%s/rectified_ref.tif' % (out_dir)
    rect2 = '%s/rectified_sec.tif' % (out_dir)
    disp    = '%s/rectified_disp.tif'   % (out_dir)
    mask    = '%s/rectified_mask.png'   % (out_dir)
    subsampling = '%s/subsampling.txt' % (out_dir)
    pointing = '%s/pointing.txt' % out_dir
    sift_matches = '%s/sift_matches.txt' % out_dir
    H_ref = '%s/H_ref.txt' % out_dir
    H_sec = '%s/H_sec.txt' % out_dir
    disp_min_max = '%s/disp_min_max.txt' % out_dir
    config = '%s/config.json' % out_dir

    if os.path.isfile(disp) and cfg['skip_existing']:
        print "Tile %d, %d, %d, %d already generated, skipping" % (x, y, w, h)
        return

    # redirect stdout and stderr to log file
    if not cfg['debug']:
        fout = open('%s/stdout.log' % out_dir, 'w', 0) # '0' is for no buffering
        sys.stdout = fout
        sys.stderr = fout

    # debug print
    print 'tile %d %d running on process %s' % (x, y, multiprocessing.current_process())

    ## select ROI
    try:
        print "ROI x, y, w, h = %d, %d, %d, %d" % (x, y, w, h)
    except TypeError:
        x, y, w, h = common.get_roi_coordinates(rpc1, prv1)
        print "ROI x, y, w, h = %d, %d, %d, %d" % (x, y, w, h)

    # if subsampling_factor is > 1, (ie 2, 3, 4... it has to be int) then
    # ensure that the coordinates of the ROI are multiples of the zoom factor
    z = cfg['subsampling_factor']
    assert(z > 0 and z == np.floor(z))
    if (z != 1):
        x = z * np.floor(x / z)
        y = z * np.floor(y / z)
        w = z * np.ceil(w / z)
        h = z * np.ceil(h / z)

    ## correct pointing error
    # A is the correction matrix and m is the list of sift matches
    if A is None:
        A, m = pointing_accuracy.compute_correction(img1, rpc1, img2, rpc2, x,
            y, w, h)
        np.savetxt(pointing, A)
        np.savetxt(sift_matches, m)
    else:
        m = None

    ## rectification
    H1, H2, disp_min, disp_max = rectification.rectify_pair(img1, img2, rpc1,
        rpc2, x, y, w, h, rect1, rect2, A, m)

    if cfg['disp_range_method'] in ["auto_srtm", "wider_sift_srtm"]:
        # Read models
        rpci1 = rpc_model.RPCModel(rpc1)
        rpci2 = rpc_model.RPCModel(rpc2)
        srtm_disp_min, srtm_disp_max = rpc_utils.rough_disparity_range_estimation(rpci1, rpci2, x, y, w, h,
            H1, H2, A, cfg['disp_range_srtm_low_margin'], cfg['disp_range_srtm_high_margin'])

        if cfg['disp_range_method'] == "auto_srtm":
            disp_min = srtm_disp_min
            disp_max = srtm_disp_max
            print "Auto srtm disp range: [%s, %s]" % (disp_min, disp_max)
        elif cfg['disp_range_method'] == "wider_sift_srtm":
            disp_min = min(srtm_disp_min, disp_min)
            disp_max = max(srtm_disp_max, disp_max)
            print "Wider sift srtm disp range: [%s, %s]" % (disp_min, disp_max)
    else:<commit_msg>save the barycenter of the keypoints of the secondary image (needed for the global_from_local pointing correction)<commit_after>#!/usr/bin/env python

# Copyright (C) 2013, <NAME> <<EMAIL>>
# Copyright (C) 2013, <NAME> <<EMAIL>>
# Copyright (C) 2013, <NAME> <<EMAIL>>

import multiprocessing
import sys
import json
import numpy as np
import os.path
import copy
import operator

from python import tee
from python import common
from python import rpc_model
from python import rpc_utils
from python import geographiclib
from python import pointing_accuracy
from python import rectification
from python import block_matching
from python import triangulation
from python import tile_composer
from python import fusion
from python.config import cfg



def process_pair_single_tile(out_dir, img1, rpc1, img2, rpc2, x=None, y=None,
        w=None, h=None, prv1=None, cld_msk=None, roi_msk=None, A=None):
    """
    Computes a disparity map from a Pair of Pleiades images, without tiling

    Args:
        out_dir: path to the output directory
        img1: path to the reference image.
        rpc1: paths to the xml file containing the rpc coefficients of the
            reference image
        img2: path to the secondary image.
        rpc2: paths to the xml file containing the rpc coefficients of the
            secondary image
        x, y, w, h: four integers defining the rectangular ROI in the reference
            image. (x, y) is the top-left corner, and (w, h) are the dimensions
            of the rectangle.
        prv1 (optional): path to a preview of the reference image
        cld_msk (optional): path to a gml file containing a cloud mask
        roi_msk (optional): path to a gml file containing a mask defining the
            area contained in the full image.
        A (optional, default None): pointing correction matrix. If None, it
            will be estimated by this function.

    Returns:
        nothing
    """
    # create a directory for the experiment
    common.run('mkdir -p %s' % out_dir)

    # output files
    rect1 = '%s/rectified_ref.tif' % (out_dir)
    rect2 = '%s/rectified_sec.tif' % (out_dir)
    disp    = '%s/rectified_disp.tif'   % (out_dir)
    mask    = '%s/rectified_mask.png'   % (out_dir)
    subsampling = '%s/subsampling.txt' % (out_dir)
    pointing = '%s/pointing.txt' % out_dir
    center = '%s/center_keypts_sec.txt' % out_dir
    sift_matches = '%s/sift_matches.txt' % out_dir
    H_ref = '%s/H_ref.txt' % out_dir
    H_sec = '%s/H_sec.txt' % out_dir
    disp_min_max = '%s/disp_min_max.txt' % out_dir
    config = '%s/config.json' % out_dir

    if os.path.isfile(disp) and cfg['skip_existing']:
        print "Tile %d, %d, %d, %d already generated, skipping" % (x, y, w, h)
        return

    # redirect stdout and stderr to log file
    if not cfg['debug']:
        fout = open('%s/stdout.log' % out_dir, 'w', 0) # '0' is for no buffering
        sys.stdout = fout
        sys.stderr = fout

    # debug print
    print 'tile %d %d running on process %s' % (x, y, multiprocessing.current_process())

    ## select ROI
    try:
        print "ROI x, y, w, h = %d, %d, %d, %d" % (x, y, w, h)
    except TypeError:
        x, y, w, h = common.get_roi_coordinates(rpc1, prv1)
        print "ROI x, y, w, h = %d, %d, %d, %d" % (x, y, w, h)

    # if subsampling_factor is > 1, (ie 2, 3, 4... it has to be int) then
    # ensure that the coordinates of the ROI are multiples of the zoom factor
    z = cfg['subsampling_factor']
    assert(z > 0 and z == np.floor(z))
    if (z != 1):
        x = z * np.floor(x / z)
        y = z * np.floor(y / z)
        w = z * np.ceil(w / z)
        h = z * np.ceil(h / z)

    ## correct pointing error
    # A is the correction matrix and m is the list of sift matches
    if A is None:
        A, m = pointing_accuracy.compute_correction(img1, rpc1, img2, rpc2, x,
            y, w, h)
        np.savetxt(pointing, A)
        np.savetxt(sift_matches, m)
        np.savetxt(center, np.mean(m[:, 2:4], 0))
    else:
        m = None

    ## rectification
    H1, H2, disp_min, disp_max = rectification.rectify_pair(img1, img2, rpc1,
        rpc2, x, y, w, h, rect1, rect2, A, m)

    if cfg['disp_range_method'] in ["auto_srtm", "wider_sift_srtm"]:
        # Read models
        rpci1 = rpc_model.RPCModel(rpc1)
        rpci2 = rpc_model.RPCModel(rpc2)
        srtm_disp_min, srtm_disp_max = rpc_utils.rough_disparity_range_estimation(rpci1, rpci2, x, y, w, h,
            H1, H2, A, cfg['disp_range_srtm_low_margin'], cfg['disp_range_srtm_high_margin'])

        if cfg['disp_range_method'] == "auto_srtm":
            disp_min = srtm_disp_min
            disp_max = srtm_disp_max
            print "Auto srtm disp range: [%s, %s]" % (disp_min, disp_max)
        elif cfg['disp_range_method'] == "wider_sift_srtm":
            disp_min = min(srtm_disp_min, disp_min)
            disp_max = max(srtm_disp_max, disp_max)
            print "Wider sift srtm disp range: [%s, %s]" % (disp_min, disp_max)
    else:
{'fraction_non_alphanumeric': 0.10779419256240448, 'fraction_numerical': 0.01130922058074376, 'mean_word_length': 2.642300556586271, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4947077', 'n_tokens_mistral': 3489, 'n_tokens_neox': 3373, 'n_words': 1113}