Skip to content

Commit

Permalink
Bugfix for multiview
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-alexandrov committed Aug 5, 2024
1 parent 1d09a58 commit ceb3653
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 30 deletions.
42 changes: 39 additions & 3 deletions src/asp/Tools/parallel_stereo
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,47 @@ def produce_tiles(args, settings, tile_w, tile_h):
that. Skip the tiles that have no valid disparity.
'''

# This cannot happen before we know trans_left_image_size
key = 'trans_left_image_size'
if key not in settings:
raise Exception("Cannot produce tiles until L.tif is known.")

sep = ","
tile_opt = ['--parallel-tile-size', str(tile_w), str(tile_h)]
verbose = False
print("--now in produce_tiles")
print("tile opt is ", tile_opt)
print("args is ", " ".join(args))

num_pairs = int(settings['num_stereo_pairs'][0]) # for multiview
print("num_pairs is ", num_pairs)

if opt.seed_mode != 0 and num_pairs == 1:
# D_sub must exist
d_sub_file = settings['out_prefix'][0] + '-D_sub.tif'
if not os.path.exists(d_sub_file):
raise Exception('The file ' + d_sub_file + ' does not exist.')

# Create the tiles
run_and_parse_output("stereo_parse", args + tile_opt, sep, verbose)

# print("---local prefix is ", local_prefix)
# print("---out prefix is ", out_prefix)

# # This is a bugfix for multiview. The tiles are produced in a subdirectory
# if local_prefix != out_prefix:
# local_tile_list = local_prefix + '-dirList.txt'
# out_tile_list = out_prefix + '-dirList.txt'
# if os.path.exists(local_tile_list):
# print("--copy file ", local_tile_list, " to ", out_tile_list)
# shutil.copyfile(local_tile_list, out_tile_list)
# else:
# raise Exception('The file ' + local_tile_list + ' does not exist.')

tiles = readTiles(settings['out_prefix'][0])

print("--size of tiles is ", len(tiles))

return tiles

def sym_link_prev_run(prev_run_prefix, out_prefix):
Expand Down Expand Up @@ -192,7 +227,7 @@ def rename_files(settings, subdirs, postfix_in, postfix_out):
os.remove(filename_out)
os.rename(filename_in, filename_out)

def create_symlinks_for_multiview(opt, settings):
def create_symlinks_for_multiview(opt, args, settings):
"""
Running parallel_stereo for each pair in a multiview run
creates files like:
Expand All @@ -201,13 +236,14 @@ def create_symlinks_for_multiview(opt, settings):
out-prefix-4096_4096_1629_1629/4096_4096_1629_1629-pair2/2-F.tif
Create the latter as a sym link.
"""
print("args 6 is ", " ".join(args))
subdirs = create_subdirs_symlink(opt, args, settings) # symlink L.tif, etc

for s in sorted(settings.keys()):

# Find the settings for each stereo command that together comprise the
# multiview run.
# TODO(oalexan1): This should be factored out as a function, and the abve
# TODO(oalexan1): This should be factored out as a function, and the above
# loop should iterate over the output of this function.
m = re.match(r'multiview_command', s)
if not m:
Expand Down Expand Up @@ -853,7 +889,7 @@ def setup_run_multiview(opt, settings, parallel_args, args):
# We will arrive here after this script invokes itself
# for multiview. Set up the directories. After this
# exits, triangulation will run.
create_symlinks_for_multiview(opt, settings)
create_symlinks_for_multiview(opt, args, settings)

if __name__ == '__main__':
usage = '''parallel_stereo [options] <images> [<cameras>]
Expand Down
68 changes: 41 additions & 27 deletions src/asp/Tools/stereo_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ int main(int argc, char* argv[]) {
<< stereo_settings().left_image_crop_win.width() << ","
<< stereo_settings().left_image_crop_win.height() << "\n";

// This is different from opt.out_prefix for multiview
vw_out() << "out_prefix," << output_prefix << "\n";

Vector2i left_image_size = file_image_size(opt.in_file1),
Expand Down Expand Up @@ -206,17 +207,25 @@ int main(int argc, char* argv[]) {
vw_out() << "corr_memory_limit_mb," << stereo_settings().corr_memory_limit_mb << "\n";
vw_out() << "save_lr_disp_diff," << stereo_settings().save_lr_disp_diff << "\n";
vw_out() << "correlator_mode," << stereo_settings().correlator_mode << "\n";

if (asp::stereo_settings().parallel_tile_size != vw::Vector2i(0, 0)) {
// Produce the tiles for parallel_stereo. Skip the ones not having valid
// disparity.
// if (trans_left_image_size == vw::Vector2(0, 0))
// vw_throw(ArgumentErr() << "Cannot produce tiles without a valid L.tif.\n");

std::string d_sub_file = opt.out_prefix + "-D_sub.tif";
if (trans_left_image_size == vw::Vector2(0, 0))
vw_throw(ArgumentErr() << "Cannot produce tiles without a valid L.tif.\n");

// We can check for valid D_sub only if seed_mode is not 0 and not part of a multiview
// run, as that one is tricky to get right.
bool have_D_sub = false;
std::string d_sub_file = output_prefix + "-D_sub.tif";
std::cout << "===d sub file is " << d_sub_file << std::endl;
vw::ImageView<vw::PixelMask<vw::Vector2f>> sub_disp;
vw::Vector2 upsample_scale;
asp::load_D_sub_and_scale(opt.out_prefix, d_sub_file, sub_disp, upsample_scale);
vw::Vector2 upsample_scale(0, 0);
bool is_multiview = stereo_settings().part_of_multiview_run;
if (stereo_settings().seed_mode != 0 && !is_multiview) {
have_D_sub = true;
asp::load_D_sub_and_scale(output_prefix, d_sub_file, sub_disp, upsample_scale);
}
std::cout << "--upsample_scale is " << upsample_scale << std::endl;

std::cout << "--trans_left_image size is " << trans_left_image_size << std::endl;
Expand All @@ -229,8 +238,9 @@ int main(int argc, char* argv[]) {
int tiles_ny = int(ceil(double(trans_left_image_size[1]) / tile_y));
std::cout << "--tiles_nx is " << tiles_nx << std::endl;
std::cout << "--tiles_ny is " << tiles_ny << std::endl;
std::cout << "--is multiview is " << is_multiview << std::endl;

std::string dirList = opt.out_prefix + "-dirList.txt";
std::string dirList = output_prefix + "-dirList.txt";
// Open this for writing
std::ofstream ofs(dirList.c_str());
// Iterate in iy from 0 to tiles_ny - 1, and in ix from 0 to tiles_nx - 1.
Expand All @@ -248,29 +258,33 @@ int main(int argc, char* argv[]) {
int beg_x = ix * tile_x;
int beg_y = iy * tile_y;

int min_sub_x = floor((beg_x - sgm_collar_size) / upsample_scale[0]);
int min_sub_y = floor((beg_y - sgm_collar_size) / upsample_scale[1]);
int max_sub_x = ceil((beg_x + curr_tile_x + sgm_collar_size) / upsample_scale[0]);
int max_sub_y = ceil((beg_y + curr_tile_y + sgm_collar_size) / upsample_scale[1]);
bool has_valid_vals = true;
if (have_D_sub) {
has_valid_vals = false;
int min_sub_x = floor((beg_x - sgm_collar_size) / upsample_scale[0]);
int min_sub_y = floor((beg_y - sgm_collar_size) / upsample_scale[1]);
int max_sub_x = ceil((beg_x + curr_tile_x + sgm_collar_size) / upsample_scale[0]);
int max_sub_y = ceil((beg_y + curr_tile_y + sgm_collar_size) / upsample_scale[1]);

min_sub_x = std::max(min_sub_x, 0);
min_sub_y = std::max(min_sub_y, 0);
max_sub_x = std::min(max_sub_x, sub_disp.cols() - 1);
max_sub_y = std::min(max_sub_y, sub_disp.rows() - 1);

min_sub_x = std::max(min_sub_x, 0);
min_sub_y = std::max(min_sub_y, 0);
max_sub_x = std::min(max_sub_x, sub_disp.cols() - 1);
max_sub_y = std::min(max_sub_y, sub_disp.rows() - 1);

bool has_valid_vals = false;
for (int y = min_sub_y; y <= max_sub_y; y++) {
for (int x = min_sub_x; x <= max_sub_x; x++) {
if (is_valid(sub_disp(x, y))) {
has_valid_vals = true;
break;
for (int y = min_sub_y; y <= max_sub_y; y++) {
for (int x = min_sub_x; x <= max_sub_x; x++) {
if (is_valid(sub_disp(x, y))) {
has_valid_vals = true;
break;
}
}
}
if (has_valid_vals) break;
}
if (has_valid_vals)
break;
}
}

if (has_valid_vals)
ofs << opt.out_prefix << "-" << beg_x << "_" << beg_y << "_"
ofs << output_prefix << "-" << beg_x << "_" << beg_y << "_"
<< curr_tile_x << "_" << curr_tile_y << "\n";
}
}
Expand Down

0 comments on commit ceb3653

Please sign in to comment.