split("", img_Md); # {img_Md[img,0..2,0..2]} maps grid from Image to World. split("", img_Mi); # {img_Mi[img,0..2,0..2]} maps grid from World to Image. # Compute the image<-->world matrices for this image if possible: compute_world_image_mapping(); # Now run through all landmarks again and try to compute {XV} from {X,Y,Z} or vice-versa: determine_landmark_coordinates(); else if # Get the default {H,V} from batch table if necessary: if (! hv_def) { img_mark_h[lab] = batch_h[lab]; img_mark_v[lab] = batch_v[lab]; } # Get the default {X,Y,Z} from batch table if necessary: if (! xyz_def) { img_mark_x[lab] = batch_x[lab]; img_mark_y[lab] = batch_y[lab]; img_mark_z[lab] = batch_z[lab]; } } ???????????????? { # Landmark wasn't specified for this image, must take default: if (show_defs) { if (ndefs == 0) { printf "defaulted landmarks:" > "/dev/stderr"; } printf " %s", lab > "/dev/stderr"; } x = mark_x[lab]; y = mark_y[lab]; split("", b); split("", a); split("", cur_Mi); for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) { cur_Mi[i,j] = img_Mi[img,i,j]; } b[0] = 1; b[1] = x; b[2] = y; r3x3_mul_row(b, cur_Mi, a); img_mark_h[img,lab] = a[1]/a[0]; img_mark_v[img,lab] = a[2]/a[0]; ndefs++; print_landmark(img,lab); } } # Compute the World coords {X,Y,Z} of landmark (assumed to have {Z=0}): split("", a); split("", b); a[0] = 1; a[1] = mark_h; a[2] = mark_v; r3x3_mul_row(a, cur_Md, b); mark_x[lab] = b[1]/b[0]; mark_y[lab] = b[2]/b[0]; mark_z[lab] = 0.0; # Read and save the grid projective matrix for this image: split("", cur_Md); split("", cur_Mi); read_img_grid_map(cur_img,cur_Md,cur_Mi); for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) { img_Md[cur_img,i,j] = cur_Md[i,j]; img_Mi[cur_img,i,j] = cur_Mi[i,j]; } # but not specified for this image: ndefs = 0; # Number of defaulted marks for this image # show_defs = (img_nmarks[img] != 0) && (img_nmarks[img] > 0.667*batch_nmarks); show_defs = 1; for (ifr = 0; ifr < batch_nmarks; ifr++) { lab = batch_mark[ifr]; if (! ((img,lab) in img_mark_h)) { # Landmark wasn't specified for this image, must take default: if (show_defs) { if (ndefs == 0) { printf "defaulted landmarks:" > "/dev/stderr"; } printf " %s", lab > "/dev/stderr"; } x = mark_x[lab]; y = mark_y[lab]; split("", b); split("", a); split("", cur_Mi); for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) { cur_Mi[i,j] = img_Mi[img,i,j]; } b[0] = 1; b[1] = x; b[2] = y; r3x3_mul_row(b, cur_Mi, a); img_mark_h[img,lab] = a[1]/a[0]; img_mark_v[img,lab] = a[2]/a[0]; ndefs++; print_landmark(img,lab); } } # Save image data as default for batch: if (show_defs && (ndefs != 0)) { printf "\n" > "/dev/stderr"; } printf " eoi\n"; function init_batch( ) { # Start of a new batch with ID = {batch}, which should have {nmarks} landmarks: cur_batch = batch; cur_batch_nominal_nmarks = nmarks; batch_nimgs = 0; # Number of images in current batch. cur_img = ""; # Current image name. split("", batch_img); # Name of image {k} of current batch is {batch_img[k]}, {k} in {0..batch_nimgs-1}. batch_nmarks = 0; # Number of landmarks in current batch. split("", batch_mark); # Label of landmark {i} of current batch is {batch_mark[i}, for {i} in {0..batch_nmarks-1}. # Data for each landmark: split("", batch_mark_x); # {batch_mark_x[lab]} is the default world coord X of mark {lab} in current batch. split("", batch_mark_y); # {batch_mark_y[lab]} is the default world coord Y of mark {lab} in current batch. split("", batch_mark_z); # {batch_mark_z[lab]} is the default world coord Z of mark {lab} in current batch. split("", batch_mark_rw); # {batch_mark_rw[lab]} is the default world radius of mark {lab} in current batch. # Data for each batch-landmark pair: split("", batch_mark_h); # {batch_mark_h[img,lab]} is the default image coord H of mark {lab} in current batch. split("", batch_mark_v); # {batch_mark_v[img,lab]} is the default image coord V of mark {lab} in current batch. split("", batch_mark_ri); # {batch_mark_ri[img,lab]} is the default image radius of mark {lab} in current image. } function init_image() { # Start of a new image with sorted name {img} and original unsorted name {orig_img}. cur_img = img; cur_orig_img = orig_img; # Data for each image: split("", img_nmarks); # {img_nmarks[img]} is num of marks specified for current image. split("", img_Md); # {img_Md[img,0..2,0..2]} maps grid from Image to World. split("", img_Mi); # {img_Mi[img,0..2,0..2]} maps grid from World to Image. # Data for each landmark: split("", img_mark_x); # {img_mark_x[lab]} World coord X of mark {lab} in current image. split("", img_mark_y); # {img_mark_y[lab]} World coord Y of mark {lab} in current image. split("", img_mark_z); # {img_mark_z[lab]} World coord Z of mark {lab} in current image. split("", img_mark_rw); # {img_mark_rw[lab]} World radius of mark {lab} in current image. # Data for each image-landmark pair: split("", img_mark_h); # {img_mark_h[lab]} Image coord H of mark {lab} in current image. split("", img_mark_v); # {img_mark_v[lab]} Image coord V of mark {lab} in current image. split("", img_mark_ri); # {img_mark_ri[lab]} Image radius of mark {lab} in current image. } function compute_world_image_mapping() { printf "not implemented\n" > "/dev/stderr"; exit(1); } function determine_landmark_coordinates( map-def,hv-def,xyz_def,ifr,lab) { # Assuming that the world-image mapping for this image # is as defined as it can be, tries to fill in the map_def = mapping_defined_for_image(); for (ifr = 0; ifr < batch_nmarks; ifr++) { lab = batch_mark[ifr]; # See what we have for this landmark: hv_def = hv_defined_for_image(lab); xyz_def = xyz_defined_for_image(lab); # We inherit {X,Y,Z} only if we cannot compute it from # user-specified {H,V}: if ((! xyz_def) && ((! map_def) || (! hv_def))) { img_mark_x[lab] = batch_x[lab]; img_mark_y[lab] = batch_y[lab]; img_mark_z[lab] = batch_z[lab]; xyz_def = xyz_defined_for_image(lab); } # We inherit {H,V} only if we cannot compute it from # user-defined or inherited {X,Y,Z}: if ((! hv_def) && ((! map_def) || (! xyz_def))) { img_mark_h[lab] = batch_h[lab]; img_mark_v[lab] = batch_v[lab]; hv_def = hv_defined_for_image(lab); } # We have inherited all that we should or could. if (map_def) { # Compute any missing coordinated using the image's world-image map: if ((! hv_def) && xyz_def) { compute_hv_from_xyz(lab); hv_def = 1; # We assume the computation succeeded. } else if ((! xyz_def) && hv_def) { compute_xyz_from_hv(lab); xyz_def = 1; # We assume the computation succeeded. } } # We have {H,V} and the map but not {X,Y,Z}; compute {X,Y,Z} from # them instead of inheriting from another image: compute_xyz_from_hv(lab); xyz_def = 1; # We assume the computation succeeded. } else { # If we don't have {X,Y,Z}, try getting it from a previous image: if (! xyz_def) { } if (xyz_def && map_def && (! hv_def)) { # We have {X,Y,Z} and the map but not {H,V}; compute {H,V} from # them instead of inheriting from another image: compute_hv_from_xyz(lab); hv_def = 1; # We assume the computation succeeded. } # If we still have no {H,V}, try inheriting from a previous image: if (! hv_def) { img_mark_h[lab] = batch_h[lab]; img_mark_v[lab] = batch_v[lab]; hv_def = hv_defined_for_image(lab); } } } function compute_hv_from_xyz(lab) { # Computes the {H,V} coordinates of landmark {lab} # from the {X,Y,Z} coordinates and the image's # world-to-image map. Assumes that } function compute_xyz_from_hv(lab) { # Computes the {X,Y,Z} coordinates of landmark {lab} # from the {H,V} coordinates and the image's # world-to-image map. Assumes that } function hv_defined_for_image(lab) { # Returns TRUE iff the image coordinates of the landmark with label{lab} # are defined for the current image. if ((!(lab in img_mark_h)) || (img_mark_h[lab] == "@")) { return 0; } if ((!(lab in img_mark_v)) || (img_mark_v[lab] == "@")) { return 0; } return 1; } function xyz_defined_for_image(lab) { # Returns TRUE iff the image coordinates of the landmark with label{lab} # are defined for the current image. if ((!(lab in img_mark_x)) || (img_mark_x[lab] == "@")) { return 0; } if ((!(lab in img_mark_y)) || (img_mark_y[lab] == "@")) { return 0; } if ((!(lab in img_mark_z)) || (img_mark_z[lab] == "@")) { return 0; } return 1; } function print_landmark(img,lab) { printf " %4d %4d %6.1f %6.1f %6.1f %s \\\n", \ img_mark_h[img,lab], img_mark_v[img,lab], \ mark_x[lab], mark_y[lab], mark_z[lab], lab; } function read_img_grid_map(img,Md,Mi, i,j,fname) { fname = (img ".grmap"); if (! r3x3_read(fname, Md)) { data_warning(("file " fname " not found")); # Return a default matrix that simply scales {H,V} by 1/10 for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) { Md[i,j] = (i == j ? (i == 0 ? 10.0 : 1.0) : 0.0); Mi[i,j] = (i == j ? (i == 0 ? 1.0 : 10.0) : 0.0); } return; } if (! r3x3_read(fname, Mi)) { data_error(("problem reading " fname)); } close(fname); }