Skip to content

Commit

Permalink
Merge pull request #37 from 20treeAI/remove_cfg_imports
Browse files Browse the repository at this point in the history
Remove cfg imports
  • Loading branch information
daviddemeij authored Aug 9, 2023
2 parents 0c86128 + 64aca40 commit e97ecc4
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 99 deletions.
20 changes: 12 additions & 8 deletions s2p/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def pointing_correction(tile, i, cfg):
A, m = pointing_accuracy.compute_correction(
img1, img2, rpc1, rpc2, x, y, w, h, method,
cfg['sift_match_thresh'], cfg['max_pointing_error'], cfg['matching_method'],
cfg['min_value'], cfg['max_value'], cfg['confidence_threshold']
cfg['min_value'], cfg['max_value'], cfg['confidence_threshold'], cfg
)

if A is not None: # A is the correction matrix
Expand All @@ -142,7 +142,7 @@ def pointing_correction(tile, i, cfg):
visualisation.plot_matches(img1, img2, rpc1, rpc2, m,
os.path.join(out_dir,
'sift_matches_pointing.png'),
x, y, w, h)
x, y, w, h, cfg)


def global_pointing_correction(cfg, tiles):
Expand Down Expand Up @@ -212,10 +212,14 @@ def rectification_pair(tile, i, cfg):
H1, H2, disp_min, disp_max = rectification.rectify_pair(img1, img2,
rpc1, rpc2,
x, y, w, h,
rect1, rect2, A, m,
rect1, rect2,
cfg,
A=A,
sift_matches=m,
method=cfg['rectification_method'],
hmargin=cfg['horizontal_margin'],
vmargin=cfg['vertical_margin'])
vmargin=cfg['vertical_margin'],
)

np.savetxt(os.path.join(out_dir, 'H_ref.txt'), H1, fmt='%12.6f')
np.savetxt(os.path.join(out_dir, 'H_sec.txt'), H2, fmt='%12.6f')
Expand Down Expand Up @@ -246,7 +250,7 @@ def stereo_matching(tile, i, cfg):
disp_min, disp_max = np.loadtxt(os.path.join(out_dir, 'disp_min_max.txt'))

block_matching.compute_disparity_map(rect1, rect2, disp, mask,
cfg['matching_algorithm'], disp_min,
cfg['matching_algorithm'], cfg, disp_min,
disp_max, timeout=cfg['mgm_timeout'],
max_disp_range=cfg['max_disp_range'])

Expand Down Expand Up @@ -335,7 +339,7 @@ def disparity_to_ply(tile, cfg):
with rasterio.open(os.path.join(out_dir, 'pair_1', 'rectified_ref.tif')) as f:
ww, hh = f.width, f.height

colors = common.tmpfile(".tif")
colors = common.tmpfile(cfg['temporary_dir'], ".tif")
success_state = common.image_apply_homography(colors, cfg['images'][0]['clr'],
np.loadtxt(H_ref), ww, hh)
if success_state is False:
Expand Down Expand Up @@ -450,7 +454,7 @@ def heights_fusion(cfg, tile):
# merge the height maps (applying mean offset to register)
fusion.merge_n(os.path.join(tile_dir, 'height_map.tif'), height_maps,
global_mean_heights, averaging=cfg['fusion_operator'],
threshold=cfg['fusion_thresh'])
threshold=cfg['fusion_thresh'], debug=cfg['debug'])

if cfg['clean_intermediate']:
for f in height_maps:
Expand Down Expand Up @@ -729,7 +733,7 @@ def main(user_cfg, start_from=0, merge_matches=False):
common.print_elapsed_time()

# cleanup
common.garbage_cleanup()
common.garbage_cleanup(cfg['clean_tmp'])
common.print_elapsed_time(since_first_call=True)


Expand Down
23 changes: 11 additions & 12 deletions s2p/block_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import rasterio

from s2p import common
from s2p.config import cfg


class MaxDisparityRangeError(Exception):
pass


def create_rejection_mask(disp, im1, im2, mask):
def create_rejection_mask(disp, im1, im2, mask, temporary_dir):
"""
Create rejection mask (0 means rejected, 1 means accepted)
Keep only the points that are matched and present in both input images
Expand All @@ -25,14 +24,14 @@ def create_rejection_mask(disp, im1, im2, mask):
im1, im2: rectified stereo pair
mask: path to the output rejection mask
"""
tmp1 = common.tmpfile('.tif')
tmp2 = common.tmpfile('.tif')
tmp1 = common.tmpfile(temporary_dir, '.tif')
tmp2 = common.tmpfile(temporary_dir, '.tif')
common.run(["plambda", disp, "x 0 join", "-o", tmp1])
common.run(["backflow", tmp1, im2, tmp2])
common.run(["plambda", disp, im1, tmp2, "x isfinite y isfinite z isfinite and and vmul", "-o", mask])


def compute_disparity_map(im1, im2, disp, mask, algo, disp_min=None,
def compute_disparity_map(im1, im2, disp, mask, algo, cfg, disp_min=None,
disp_max=None, timeout=600, max_disp_range=None,
extra_params=''):
"""
Expand Down Expand Up @@ -124,14 +123,14 @@ def compute_disparity_map(im1, im2, disp, mask, algo, disp_min=None,

win = 3 # matched block size. It must be a positive odd number
lr = 1 # maximum difference allowed in the left-right disparity check
cost = common.tmpfile('.tif')
cost = common.tmpfile(cfg['temporary_dir'], '.tif')
common.run('sgbm {} {} {} {} {} {} {} {} {} {}'.format(im1, im2,
disp, cost,
disp_min,
disp_max,
win, p1, p2, lr))

create_rejection_mask(disp, im1, im2, mask)
create_rejection_mask(disp, im1, im2, mask, cfg['temporary_dir'])

if algo == 'tvl1':
tvl1 = 'callTVL1.sh'
Expand Down Expand Up @@ -185,14 +184,14 @@ def compute_disparity_map(im1, im2, disp, mask, algo, disp_min=None,
timeout=timeout,
)

create_rejection_mask(disp, im1, im2, mask)
create_rejection_mask(disp, im1, im2, mask, cfg['temporary_dir'])


if algo == 'mgm_multi_lsd':
ref = im1
sec = im2
wref = common.tmpfile('.tif')
wsec = common.tmpfile('.tif')
wref = common.tmpfile(cfg['temporary_dir'], '.tif')
wsec = common.tmpfile(cfg['temporary_dir'], '.tif')
# TODO TUNE LSD PARAMETERS TO HANDLE DIRECTLY 12 bits images?
# image dependent weights based on lsd segments
with rasterio.open(ref, "r") as f:
Expand Down Expand Up @@ -263,7 +262,7 @@ def compute_disparity_map(im1, im2, disp, mask, algo, disp_min=None,
timeout=timeout,
)

create_rejection_mask(disp, im1, im2, mask)
create_rejection_mask(disp, im1, im2, mask, cfg['temporary_dir'])


if algo == 'mgm_multi':
Expand Down Expand Up @@ -307,7 +306,7 @@ def compute_disparity_map(im1, im2, disp, mask, algo, disp_min=None,
timeout=timeout,
)

create_rejection_mask(disp, im1, im2, mask)
create_rejection_mask(disp, im1, im2, mask, cfg['temporary_dir'])

if (algo == 'micmac'):
# add micmac binaries to the PATH environment variable
Expand Down
12 changes: 5 additions & 7 deletions s2p/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import rasterio


from s2p.config import cfg


# silent rasterio NotGeoreferencedWarning
warnings.filterwarnings("ignore",
Expand All @@ -38,18 +36,18 @@ def remove(target):
except OSError:
pass

def garbage_cleanup():
def garbage_cleanup(clean_tmp=True):
"""
Removes all the files listed in the global variable 'garbage'.
"""
if cfg['clean_tmp']:
if clean_tmp:
while garbage:
remove(garbage.pop())


def tmpfile(ext=''):
def tmpfile(temporary_dir, ext=''):
"""
Creates a temporary file in the cfg['temporary_dir'] directory.
Creates a temporary file in the temporary_dir directory.
Args:
ext: desired file extension. The dot has to be included.
Expand All @@ -61,7 +59,7 @@ def tmpfile(ext=''):
at the end of the pipeline.
"""
fd, out = tempfile.mkstemp(suffix=ext, prefix='s2p_',
dir=os.path.expandvars(cfg['temporary_dir']))
dir=os.path.expandvars(temporary_dir))
os.close(fd) # http://www.logilab.org/blogentry/17873
garbage.append(out)
return out
Expand Down
5 changes: 2 additions & 3 deletions s2p/fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import numpy as np
import rasterio

from s2p.config import cfg
from s2p import common


Expand All @@ -22,7 +21,7 @@ def average_if_close(x, threshold):
return np.nanmedian(x)


def merge_n(output, inputs, offsets, averaging='average_if_close', threshold=1):
def merge_n(output, inputs, offsets, averaging='average_if_close', threshold=1, debug=False):
"""
Merge n images of equal sizes by taking the median/mean/min/max pixelwise.
Expand All @@ -47,7 +46,7 @@ def merge_n(output, inputs, offsets, averaging='average_if_close', threshold=1):
for i, img in enumerate(inputs):
with rasterio.open(img, 'r') as f:
x[:, :, i] = f.read(1) - offsets[i]
if cfg['debug']:
if debug:
common.rasterio_write('{}_registered.tif'.format(os.path.splitext(img)[0]),
x[:, :, i] + np.mean(offsets))

Expand Down
13 changes: 6 additions & 7 deletions s2p/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,10 @@ def check_parameters(d):

def build_cfg(cfg, user_cfg):
"""
Populate a dictionary containing the s2p parameters from a user config file.
This dictionary is contained in the global variable 'cfg' of the config
module.
Populate a default cfg dictionary with the s2p parameters from a user config file.
Args:
cfg: default config dictionary
user_cfg: user config dictionary
"""
# check that all the mandatory arguments are defined
Expand Down Expand Up @@ -312,7 +310,7 @@ def is_tile_all_nodata(path:str, window:rasterio.windows.Window):
return False


def is_this_tile_useful(x, y, w, h, images, images_sizes, border_margin):
def is_this_tile_useful(x, y, w, h, images, images_sizes, border_margin, cfg):
"""
Check if a tile contains valid pixels.
Expand All @@ -336,7 +334,7 @@ def is_this_tile_useful(x, y, w, h, images, images_sizes, border_margin):

rpc = images[0]['rpcm']
for img, size in zip(images[1:], images_sizes[1:]):
coords = rpc_utils.corresponding_roi(rpc, img['rpcm'], x, y, w, h)
coords = rpc_utils.corresponding_roi(rpc, img['rpcm'], x, y, w, h, cfg)
if rectangles_intersect(coords, (0, 0, size[1], size[0])):
break # the tile is partly contained
else: # we've reached the end of the loop hence the tile is not contained
Expand All @@ -346,7 +344,7 @@ def is_this_tile_useful(x, y, w, h, images, images_sizes, border_margin):
cld_msk = images[0]['cld']
wat_msk = images[0]['wat']
mask = masking.image_tile_mask(x, y, w, h, roi_msk, cld_msk, wat_msk,
images_sizes[0], border_margin=border_margin)
images_sizes[0], border_margin=border_margin, temporary_dir=cfg['temporary_dir'])
if not mask.any():
return False, None
return True, mask
Expand Down Expand Up @@ -388,6 +386,7 @@ def tiles_full_info(cfg, tw, th, tiles_txt, create_masks=False):
cfg['images'],
images_sizes,
cfg['border_margin'],
cfg,
tilewise=False,
timeout=cfg['timeout'],
)
Expand Down
6 changes: 3 additions & 3 deletions s2p/masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def image_tile_mask(x, y, w, h, roi_gml=None, cld_gml=None, raster_mask=None,
img_shape=None, border_margin=10):
img_shape=None, border_margin=10, temporary_dir=None):
"""
Compute a validity mask for an image tile from vector/raster image masks.
Expand Down Expand Up @@ -44,7 +44,7 @@ def image_tile_mask(x, y, w, h, roi_gml=None, cld_gml=None, raster_mask=None,
mask = np.ones((h, w), dtype=bool)

if roi_gml is not None: # image domain mask (polygons)
tmp = common.tmpfile('.png')
tmp = common.tmpfile(temporary_dir, '.png')
subprocess.check_call('cldmask %d %d -h "%s" %s %s' % (w, h, hij,
roi_gml, tmp),
shell=True)
Expand All @@ -55,7 +55,7 @@ def image_tile_mask(x, y, w, h, roi_gml=None, cld_gml=None, raster_mask=None,
return mask

if cld_gml is not None: # cloud mask (polygons)
tmp = common.tmpfile('.png')
tmp = common.tmpfile(temporary_dir, '.png')
subprocess.check_call('cldmask %d %d -h "%s" %s %s' % (w, h, hij,
cld_gml, tmp),
shell=True)
Expand Down
11 changes: 5 additions & 6 deletions s2p/pointing_accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from s2p import sift
from s2p import rpc_utils
from s2p import estimation
from s2p.config import cfg


def error_vectors(m, F, ind='ref'):
Expand Down Expand Up @@ -60,7 +59,7 @@ def error_vectors(m, F, ind='ref'):
return np.vstack((np.multiply(a, l[:, 0]), np.multiply(a, l[:, 1]))).T


def local_translation(r1, r2, x, y, w, h, m):
def local_translation(r1, r2, x, y, w, h, m, cfg):
"""
Estimates the optimal translation to minimise the relative pointing error
on a given tile.
Expand All @@ -80,7 +79,7 @@ def local_translation(r1, r2, x, y, w, h, m):
"""
# estimate the affine fundamental matrix between the two views
n = cfg['n_gcp_per_axis']
rpc_matches = rpc_utils.matches_from_rpc(r1, r2, x, y, w, h, n)
rpc_matches = rpc_utils.matches_from_rpc(r1, r2, x, y, w, h, n, cfg)
F = estimation.affine_fundamental_matrix(rpc_matches)

# compute the error vectors
Expand All @@ -101,7 +100,7 @@ def local_translation(r1, r2, x, y, w, h, m):

def compute_correction(img1, img2, rpc1, rpc2, x, y, w, h,
method, sift_thresh, epipolar_threshold,
matching_method, min_value, max_value, confidence_threshold):
matching_method, min_value, max_value, confidence_threshold, cfg):
"""
Computes pointing correction matrix for specific ROI
Expand All @@ -126,11 +125,11 @@ def compute_correction(img1, img2, rpc1, rpc2, x, y, w, h,
raise ValueError(f"width or height <= 0 for:\n{img1}\n{img2}\nx={x}, y={y}, w={w}, h={h}. Try a different"
f"tilesize or different ROI.")
m = sift.matches_on_rpc_roi(img1, img2, rpc1, rpc2, x, y, w, h,
method, sift_thresh, epipolar_threshold,
method, sift_thresh, epipolar_threshold, cfg,
matching_method, min_value, max_value, confidence_threshold)

if m is not None:
A = local_translation(rpc1, rpc2, x, y, w, h, m)
A = local_translation(rpc1, rpc2, x, y, w, h, m, cfg)
else:
A = None

Expand Down
Loading

0 comments on commit e97ecc4

Please sign in to comment.