Commit 7f9773fb authored by Andrey Filippov's avatar Andrey Filippov
Browse files

done refactoring, starting debugging

parent 0a4fdfe3
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
package com.elphel.imagej.orthomosaic;

public class Affine2Import {
	public static final String AFFINE2_TAG = "AFFINE2";	
	public static final int    AFFINE2_LEN = 10;
	public String name1;
	public String name2;
	public double overlap;
	public double [][] affine;
	public static boolean matches(String [] tokens) {
		return ((tokens.length == AFFINE2_LEN) && AFFINE2_TAG.equals(tokens[0]));
	}
	public Affine2Import(
			String [] tokens) {
		if (!matches(tokens)) {
			throw new IllegalArgumentException ("Not Affine2Import tokens");
		}
		int indx = 1;
		name1 = tokens[indx++];
		name2 = tokens[indx++];
		overlap = Double.parseDouble(tokens[indx++]);
		affine = new double [2][3];
		for (int i = 0; i < affine.length; i++) {
			for (int j = 0; j < affine[i].length; j++) {
				affine[i][j] = Double.parseDouble(tokens[indx++]);
			}
		}
	}
}
+27 −0
Original line number Diff line number Diff line
package com.elphel.imagej.orthomosaic;

public class AffineImport {
	public static final String AFFINE_TAG = "AFFINE";
	public static final int    AFFINE_LEN = 8;
	public String name;
	public double [][] affine;
	public static boolean matches(String [] tokens) {
		return ((tokens.length == AFFINE_LEN) && AFFINE_TAG.equals(tokens[0]));
	}
	public AffineImport(
			String [] tokens) {
		if (!matches(tokens)) {
			throw new IllegalArgumentException ("Not AffineImport tokens");
		}
		int indx = 1;
		name = tokens[indx++];
		affine = new double [2][3];
		for (int i = 0; i < affine.length; i++) {
			for (int j = 0; j < affine[i].length; j++) {
				affine[i][j] = Double.parseDouble(tokens[indx++]);
			}
		}
	}
	
// String[] tokens	
}
+381 −166
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import ij.gui.PointRoi;
import ij.process.FloatPolygon;

public class ComboMatch {
	public static final String [] SUFFIXES_BL_BC=     {"","-BL","-BC"};
	public static GPUTileProcessor GPU_TILE_PROCESSOR = null;
	public static GpuQuad   GPU_QUAD_AFFINE = null;
	public static ImagePlus imp_src1 = null;
@@ -174,9 +175,6 @@ public class ComboMatch {
		boolean display_pairs =            false;
		boolean create_map =               false;
		boolean create_equalize =          false;
                
                
                
		boolean use_saved_collection = false; // true; // false;
		boolean save_collection =      false; // true;
		boolean process_correlation = true; // use false to save new version of data
@@ -191,23 +189,24 @@ public class ComboMatch {
		boolean restore_temp =        true;
		double  frac_remove  =        clt_parameters.imp.pmtch_frac_remove; //   0.15;
		double  metric_error =        clt_parameters.imp.pmtch_metric_err; // 0.05; // 0.02;//  2 cm
                boolean update_files =        false;
                boolean update_lla =          false; // re-read file metadata
                boolean update_kernel_patterns = false;
                boolean update_bl_bc =        false;
                boolean export_affine =       false; // export per-scene affines
                boolean export_affine2 =      false; // export per-pair affines
		
		boolean import_export =       false;
//		boolean update_files =        false;
//		boolean update_lla =          false; // re-read file metadata
//		boolean update_kernel_patterns = false;
//		boolean update_bl_bc =        false;
//		boolean export_affine =       false; // export per-scene affines
//		boolean export_affine2 =      false; // export per-pair affines


		boolean fix_duplicates =      false;
                String [] suffixes_bl_bc=     {"","-BL","-BC"};
		boolean log_append =           clt_parameters.imp.pwise_log_append;
		String log_path =              clt_parameters.imp.pwise_log_path;
		String omtch_img_set =         clt_parameters.imp.omtch_img_set;
		
//		boolean READ_NO_ALT =          false; // to read older format data file where pairs do not have alt_data[]
		
		int  suffix_bc_bl_indx =   2;
//		int  suffix_bc_bl_indx =   2;
		if (!use_marked_image) {
			process_correlation=false; // use already adjusted by default
		}
@@ -217,12 +216,10 @@ public class ComboMatch {
		}
		
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Set image pair",1200,900);
//		gd.addChoice      ("Files list/data path (w/o extension):", FILES_LISTS_PATHS, FILES_LISTS_PATHS[default_list_choice]);
		gd.addChoice      ("Files list/data path (w/o extension):", FILES_LISTS_PATHS, omtch_img_set);
		gd.addCheckbox    ("Use saved maps collection ", use_saved_collection, " (if available). If false - use files list.");
		gd.addCheckbox    ("Save maps collection", save_collection, "Save maps collection to be able to restore.");
		gd.addCheckbox    ("Process correlations", process_correlation, "false to skip to just regenerate new save file.");
//		int num_tries_fit =           10;
		gd.addNumericField("Num tries fit",        num_tries_fit,  0,4,"", "Try matching images this number of times");

		gd.addCheckbox    ("Update match if calculated", update_match, "Will update correlation match for a pair if found.");
@@ -254,20 +251,21 @@ public class ComboMatch {
		if (use_marked_image ) {
			gd.addCheckbox    ("Use marked image data",   true, "Use markes from the selected image");
		}
                gd.addCheckbox    ("Update scene files",        update_files, "Re-read files as specified in .list file, possibly changing versions");
                gd.addCheckbox    ("Update files metadata",     update_lla, "Re-read files metadata (if it was modified)");
                gd.addCheckbox    ("Update kernels/patterns",   update_kernel_patterns, "Re-read kernels and patterns from *.list file");
		gd.addCheckbox    ("Import/Export operations",  import_export, "Import/export and update operations");
//		gd.addCheckbox    ("Update scene files",        update_files, "Re-read files as specified in .list file, possibly changing versions");
//		gd.addCheckbox    ("Update files metadata",     update_lla, "Re-read files metadata (if it was modified)");
//		gd.addCheckbox    ("Update kernels/patterns",   update_kernel_patterns, "Re-read kernels and patterns from *.list file");
		gd.addCheckbox    ("Remove duplicate scenes",   fix_duplicates, "Remove scenes with the same timestamp");

		gd.addCheckbox    ("Update BC/BL suffix",       update_bl_bc,
				"Change source filenames to use -BC for bicubic, -BL - for bilinear, or empty - for old bilinear files");
		gd.addChoice("BL/BC suffix:",        
                                suffixes_bl_bc,
                                suffixes_bl_bc[suffix_bc_bl_indx],
                                "Select interpolation mode of the source files: old bilinear (empty), bilinear (-BL), or bicubic (-BC)", 0);
//		gd.addCheckbox    ("Update BC/BL suffix",       update_bl_bc,
//				"Change source filenames to use -BC for bicubic, -BL - for bilinear, or empty - for old bilinear files");
//		gd.addChoice("BL/BC suffix:",        
//				SUFFIXES_BL_BC,
//				SUFFIXES_BL_BC[suffix_bc_bl_indx],
//				"Select interpolation mode of the source files: old bilinear (empty), bilinear (-BL), or bicubic (-BC)", 0);

                gd.addCheckbox    ("Export scene affines",      export_affine,  "Export per-scene affines in text format for migration.");
                gd.addCheckbox    ("Export pairs affines",      export_affine2, "Export per-pair  affines in text format for migration.");
//		gd.addCheckbox    ("Export scene affines",      export_affine,  "Export per-scene affines in text format for migration.");
//		gd.addCheckbox    ("Export pairs affines",      export_affine2, "Export per-pair  affines in text format for migration.");

		gd.addCheckbox    ("Write log file",            log_append, "Enable writing log file with matching results.");
		gd.addStringField ("Log file full path",        log_path, 150, "Path of the log file to be appended.");     
@@ -285,6 +283,7 @@ public class ComboMatch {
		String orthoMapsCollection_path =omtch_img_set+".data";
		String affines_path =            omtch_img_set+".affines"; // for export per-scene affines
		String affines2_path =           omtch_img_set+".affines2"; // for export per-pair affines
		
		use_saved_collection =     gd.getNextBoolean();
		save_collection =          gd.getNextBoolean();
		String orthoMapsCollection_savepath = save_collection?orthoMapsCollection_path:null;
@@ -317,15 +316,16 @@ public class ComboMatch {
		if (use_marked_image ) { // will only be used if found and asked
			use_marked_image=       gd.getNextBoolean();
		}
                update_files=               gd.getNextBoolean();
                update_lla=                 gd.getNextBoolean();
                update_kernel_patterns=     gd.getNextBoolean();
		import_export =             gd.getNextBoolean();
//		update_files=               gd.getNextBoolean();
//		update_lla=                 gd.getNextBoolean();
//		update_kernel_patterns=     gd.getNextBoolean();
		fix_duplicates=             gd.getNextBoolean();
                update_bl_bc=               gd.getNextBoolean();
                suffix_bc_bl_indx =         gd.getNextChoiceIndex();
//		update_bl_bc=               gd.getNextBoolean();
//		suffix_bc_bl_indx =         gd.getNextChoiceIndex();

                export_affine =             gd.getNextBoolean();
                export_affine2 =            gd.getNextBoolean();
//		export_affine =             gd.getNextBoolean();
//		export_affine2 =            gd.getNextBoolean();


		log_append =                gd.getNextBoolean();
@@ -343,13 +343,13 @@ public class ComboMatch {
			}
			//files_list_path
			if (!use_files_list) {
				if (update_kernel_patterns) {
                                        maps_collection.updateKernels(files_list_path);
                                        maps_collection.updatePatterns(files_list_path);
                                }
                                if (update_files) {
                                        maps_collection.updateFiles(files_list_path);
                                }
//				if (update_kernel_patterns) {
//					maps_collection.updateKernels(files_list_path);
//					maps_collection.updatePatterns(files_list_path);
//				}
//				if (update_files) {
//					maps_collection.updateFiles(files_list_path);
//				}
			}
		} else {
			use_files_list = true;
@@ -360,6 +360,15 @@ public class ComboMatch {
			maps_collection.updateSfmGain();
		}
		
		if (import_export) {
			return updateImportExport(
					omtch_img_set, // String files_base
					maps_collection); //						OrthoMapsCollection maps_collection

		}
		
		
		/*
		if (export_affine) {
			StringBuffer sb = new StringBuffer();
			for (OrthoMap omap :  maps_collection.ortho_maps) {
@@ -415,22 +424,23 @@ public class ComboMatch {
		if (export_affine || export_affine2) {
			return true; // do not save
		}
                
*/

		if (fix_duplicates) {
			removeDuplicateScenes (maps_collection);
		}

/*		
		if (update_bl_bc) {
			boolean OK = updateBlBcFileNames(
					suffixes_bl_bc[suffix_bc_bl_indx], // String suffix,
					SUFFIXES_BL_BC[suffix_bc_bl_indx], // String suffix,
					"-FLAT",// String before,
					maps_collection); // OrthoMapsCollection maps_collection)
			if (!OK) {
				System.out.println("Failed to update filenames for different interpolation mode");
			}
		}
		
		*/
		
		
		String [] names = maps_collection.getNames(); // null
@@ -608,7 +618,7 @@ public class ComboMatch {
				}
			}
			
			maps_collection.reverseRender(
			maps_collection.reverseRender( // assuming image, not alt
					object_list,        // ArrayList<ObjectLocation> objects,
					zool_lev_objects,   // int         zoom_level){
					indices);           // int []      indices){ // null or selected image indices
@@ -843,12 +853,12 @@ public class ComboMatch {
		//getTemperature()
		// get all temperatures
		maps_collection.getAllTemperatures();
		if (update_lla) {
			System.out.println("Updating map files metadata");
			maps_collection.updateLLa();
			maps_collection.updateNumberScenes();
			maps_collection.updateSfmGain();
		}
//		if (update_lla) {
//			System.out.println("Updating map files metadata");
//			maps_collection.updateLLA();
//			maps_collection.updateNumberScenes();
//			maps_collection.updateSfmGain();
//		}
		
        // which pair to compare
//		String [] gpu_spair = {names[gpu_ipair[0]],names[gpu_ipair[1]]}; 
@@ -2652,5 +2662,210 @@ adjusted affines[1] for a pair: 1694564291_293695/1694564778_589341
		}
	}
	
	public static boolean updateImportExport(
			String files_base,
			OrthoMapsCollection maps_collection) {
		if  (maps_collection == null) {
			throw new IllegalArgumentException ("maps_collection is not initialized!");
		}
		int  suffix_bc_bl_indx =   2;
		boolean update_files =           false;
		boolean update_lla =             false; // re-read file metadata
		boolean update_kernel_patterns = false;
		boolean update_bl_bc =           false;
		boolean export_affine =          false; // export per-scene affines
		boolean export_affine2 =         false; // export per-pair affines
		boolean overwrite_affine =       false;
		boolean import_affine =          false; // export per-scene affines
		boolean import_affine2 =         false; // export per-pair affines
		String files_list_path =         files_base+".list";
//		String orthoMapsCollection_path =omtch_img_set+".data";
		String affines_path =            files_base+".affines"; // for export per-scene affines
		String affines2_path =           files_base+".affines2"; // for export per-pair affines


		GenericJTabbedDialog gd = new GenericJTabbedDialog("Select Update, Import or Export operations",1200,900);
		gd.addStringField ("List file full path",        files_list_path, 150, "Path to read/update directories and GEO files.");     
		gd.addStringField ("Scene affines full path",    affines_path, 150, "Path to import/export scenes affines.");     
		gd.addStringField ("Pairwise affines full path", affines2_path, 150, "Path to import/export scenes pairs affines.");     
		gd.addCheckbox    ("Update scene files",         update_files,   "Re-read files as specified in .list file, possibly changing versions");
		gd.addCheckbox    ("Update files metadata",      update_lla,     "Re-read files metadata (if it was modified)");
		gd.addCheckbox    ("Update kernels/patterns",    update_kernel_patterns, "Re-read kernels and patterns from *.list file");
		gd.addCheckbox    ("Export scene affines",       export_affine,  "Export per-scene affines in text format for migration.");
		gd.addCheckbox    ("Export pairs affines",       export_affine2, "Export per-pair  affines in text format for migration.");
		
		gd.addCheckbox    ("Overwrite affines on import",overwrite_affine, "Overwrite existing non-trivial affines on import.");
		gd.addCheckbox    ("Import scene affines",       import_affine,  "Export per-scene affines in text format for migration.");
		gd.addCheckbox    ("Import pairs affines",       import_affine2, "Export per-pair  affines in text format for migration.");
		gd.addCheckbox    ("Update BC/BL suffix",        update_bl_bc,
				"Change source filenames to use -BC for bicubic, -BL - for bilinear, or empty - for old bilinear files");
		gd.addChoice("BL/BC suffix:",        
				SUFFIXES_BL_BC,
				SUFFIXES_BL_BC[suffix_bc_bl_indx],
				"Select interpolation mode of the source files: old bilinear (empty), bilinear (-BL), or bicubic (-BC)", 0);
		gd.showDialog();
		if (gd.wasCanceled()) return false;
		files_list_path =           gd.getNextString();
		affines_path =              gd.getNextString();
		affines2_path =             gd.getNextString();
   		update_files=               gd.getNextBoolean();
		update_lla=                 gd.getNextBoolean();
		update_kernel_patterns=     gd.getNextBoolean();
		export_affine =             gd.getNextBoolean();
		export_affine2 =            gd.getNextBoolean();
		overwrite_affine =          gd.getNextBoolean();
		import_affine =             gd.getNextBoolean();
		import_affine2 =            gd.getNextBoolean();
		update_bl_bc=               gd.getNextBoolean();
		suffix_bc_bl_indx =         gd.getNextChoiceIndex();
		
		if (update_files) {
			maps_collection.updateFiles(files_list_path);
			maps_collection.updateNumberScenes(); // number of scenes in each sequence
		}
		if (update_lla) {
			System.out.println("Updating map files metadata");
			maps_collection.updateLLA();
			maps_collection.updateNumberScenes();
			maps_collection.updateSfmGain();
		}
		if (update_kernel_patterns) {
			maps_collection.updateKernels(files_list_path);
			maps_collection.updatePatterns(files_list_path);
		}
		
		if (export_affine) {
			StringBuffer sb = new StringBuffer();
			for (OrthoMap omap :  maps_collection.ortho_maps) {
				String name = omap.getName();
				double [][] affine = omap.getAffine();
				if (affine != null) {
					sb.append(String.format("AFFINE %s %11.8f  %11.8f  %11.8f  %11.8f  %11.8f  %11.8f\n",
							name, affine[0][0], affine[0][1], affine[0][2], affine[1][0], affine[1][1], affine[1][2]));
				}
			}
			CalibrationFileManagement.saveStringToFile (
					affines_path,          //String path,
					sb.toString(), // data,
					false); // boolean append)
		}

		if (export_affine2) {
			StringBuffer sb = new StringBuffer();
			ArrayList<Point> pairs_list = new ArrayList<Point>(); 
			for (OrthoMap map : maps_collection.ortho_maps) {
				for (String other_name: map.pairwise_matches.keySet()) {
					pairs_list.add(new Point(
							maps_collection.getIndex(map.getName()),
							maps_collection.getIndex(other_name)));
				}
			}
			Collections.sort(pairs_list, new Comparator<Point>() {
				@Override
				public int compare(Point lhs, Point rhs) {
					return (rhs.x > lhs.x) ? -1 : (rhs.x < lhs.x) ? 1 :
						((rhs.y > lhs.y) ? -1 : (rhs.y < lhs.y) ? 1 : 0); // increasing
				}
			});
			for (Point pair: pairs_list) {
				PairwiseOrthoMatch pom = maps_collection.ortho_maps[pair.x].getMatch(
						maps_collection.ortho_maps[pair.y].getName(), false); // undef_only || nan_rms);
						if ((pom !=null) && pom.isDefined()) {
							double overlap = pom.overlap;
							double [][] affine = pom.getAffine();
							sb.append(String.format("AFFINE2 %s %s %11.8f  %11.8f  %11.8f  %11.8f  %11.8f  %11.8f  %11.8f\n",
									maps_collection.ortho_maps[pair.x].getName(),
									maps_collection.ortho_maps[pair.y].getName(),
									overlap,
									affine[0][0], affine[0][1], affine[0][2], affine[1][0], affine[1][1], affine[1][2]));
						}
			}
			CalibrationFileManagement.saveStringToFile (
					affines2_path,          //String path,
					sb.toString(), // data,
					false); // boolean append)
		}

		if (import_affine) {
			ArrayList<AffineImport> affine_import = new ArrayList<AffineImport>();
			OrthoMapsCollection.getPathsAndScenesFromSourceList(
					affines_path,
					null, // scenes0,
					null,       // ArrayList<AltitudeMismatchKernel> kernels,
					null,      // ArrayList<GroundObjectPattern>    patterns);
					null, // ArrayList<ModelRegex>             model_regexes,
					null,   // String []                         regex_use) {
					affine_import,          // ArrayList<AffineImport>           affine_import,
					null);         // ArrayList<Affine2Import>          affine2_import) {
			int num_updated_affines = 0;
			for (AffineImport affineImport: affine_import) {
				OrthoMap map =  maps_collection.getMap(affineImport.name);
				if (map != null) {
					if (overwrite_affine || !map.isAffineNonTrivial()) {
						map.setAffine(affineImport.affine);
						num_updated_affines++;
					}
				}
			}
			maps_collection.updateNumberScenes();
			System.out.println("Updated "+num_updated_affines+" affines of "+affine_import.size()+" specified. There are "+
			maps_collection.ortho_maps.length+" maps in the system.");
		}
		if (import_affine2) {
			ArrayList<Affine2Import> affine2_import = new ArrayList<Affine2Import>();
			OrthoMapsCollection.getPathsAndScenesFromSourceList(
					affines_path,
					null,            // scenes0,
					null,            // ArrayList<AltitudeMismatchKernel> kernels,
					null,            // ArrayList<GroundObjectPattern>    patterns);
					null,            // ArrayList<ModelRegex>             model_regexes,
					null,            // String []                         regex_use) {
					null,            // ArrayList<AffineImport>           affine_import,
					affine2_import); // ArrayList<Affine2Import>          affine2_import) {
			int num_updated_affines = 0;
			for (Affine2Import affine2Import: affine2_import) {
				OrthoMap map =  maps_collection.getMap(affine2Import.name1);
				if (map != null) {
					PairwiseOrthoMatch pom = map.getMatch(affine2Import.name2,true);
					if (pom != null) {
						if (overwrite_affine || !pom.isAffineNonTrivial()) {
							pom.setOverlap(affine2Import.overlap);
							pom.setAffine(affine2Import.affine); // does not clone - OK
						}						
					} else {
						OrthoMap map2 =  maps_collection.getMap(affine2Import.name2);
						int zoom_lev = Math.min(map.getOriginalZoomLevel(), map2.getOriginalZoomLevel());
						pom= new PairwiseOrthoMatch(
								affine2Import.affine,   // double [][] affine,
								null,                   // double [][] jtj,
								Double.NaN,             // double rms,
								zoom_lev,               // int zoom_lev,
								affine2Import.overlap); // double overlap)
					}


				}




			}
		}

		if (update_bl_bc) {
			boolean OK = updateBlBcFileNames(
					SUFFIXES_BL_BC[suffix_bc_bl_indx], // String suffix,
					"-FLAT",// String before,
					maps_collection); // OrthoMapsCollection maps_collection)
			if (!OK) {
				System.out.println("Failed to update filenames for different interpolation mode");
			}
		}
		
		return true;
	}
	
	
	
	
}
+4 −0
Original line number Diff line number Diff line
@@ -238,6 +238,10 @@ public class FloatImageData implements Serializable {
		return extra_zoom[0];
	}
	
	public double needZoomIn() {
		return needZoomIn(pix_meters);
	}
	
	public int getZoomLevel() {
		return zoom_lev;
	}
Loading