Commit 75c570c2 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

Tested LMA equalization, some bug fixes

parent f7f4a8dd
Loading
Loading
Loading
Loading
+18 −5
Original line number Original line Diff line number Diff line
@@ -159,6 +159,8 @@ public class ComboMatch {
		boolean create_overlaps =     false;
		boolean create_overlaps =     false;
		boolean equalize_overlaps =   false;
		boolean equalize_overlaps =   false;
		boolean create_map =          false;
		boolean create_map =          false;
		boolean create_equalize =     false;
		
//		boolean show_combo_mask =     false; // generate image mas (where it is defined)_
//		boolean show_combo_mask =     false; // generate image mas (where it is defined)_
//		boolean use_alt =             false;
//		boolean use_alt =             false;
//		boolean show_centers =        true;
//		boolean show_centers =        true;
@@ -194,7 +196,7 @@ public class ComboMatch {
		}
		}
		
		
		
		
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Set image pair",1200,700);
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Set image pair",1200,800);
		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, files_lists_paths[default_list_choice]);
		gd.addCheckbox    ("Use saved maps collection", use_saved_collection, "If false - use files list.");
		gd.addCheckbox    ("Use saved maps collection", use_saved_collection, "If false - use files list.");
		gd.addCheckbox    ("Save maps collection", save_collection, "Save maps collection to be able to restore.");
		gd.addCheckbox    ("Save maps collection", save_collection, "Save maps collection to be able to restore.");
@@ -235,6 +237,7 @@ public class ComboMatch {
		gd.addCheckbox    ("Create overlap pairs",             create_overlaps, "Create scene pairs overlaps.");
		gd.addCheckbox    ("Create overlap pairs",             create_overlaps, "Create scene pairs overlaps.");
		gd.addCheckbox    ("Equalize overlap pairs",           equalize_overlaps, "Equalize intensities in overlaps.");
		gd.addCheckbox    ("Equalize overlap pairs",           equalize_overlaps, "Equalize intensities in overlaps.");
		gd.addCheckbox    ("Create map",                       create_map, "Create combined map from pairwise matches.");
		gd.addCheckbox    ("Create map",                       create_map, "Create combined map from pairwise matches.");
		gd.addCheckbox    ("Equalize intensities",             create_equalize, "Create map intensities equalization from pairwise matches.");
//		gd.addCheckbox    ("Show combo image mask",            show_combo_mask, "Display combo binary image.");
//		gd.addCheckbox    ("Show combo image mask",            show_combo_mask, "Display combo binary image.");
//		gd.addCheckbox    ("Show altitude combo image", use_alt, "Load and process altitude maps.");
//		gd.addCheckbox    ("Show altitude combo image", use_alt, "Load and process altitude maps.");
		gd.addNumericField("Remove fraction of worst matches",   frac_remove,  3,7,"",	"When fitting scenes remove this fraction of worst match.");
		gd.addNumericField("Remove fraction of worst matches",   frac_remove,  3,7,"",	"When fitting scenes remove this fraction of worst match.");
@@ -263,6 +266,7 @@ public class ComboMatch {
		String orthoMapsCollection_path =files_lists_paths[choice_index]+".data";
		String orthoMapsCollection_path =files_lists_paths[choice_index]+".data";
		use_saved_collection =     gd.getNextBoolean();
		use_saved_collection =     gd.getNextBoolean();
		save_collection =          gd.getNextBoolean();
		save_collection =          gd.getNextBoolean();
		String orthoMapsCollection_savepath = save_collection?orthoMapsCollection_path:null;
		process_correlation=       gd.getNextBoolean();
		process_correlation=       gd.getNextBoolean();
		num_tries_fit =      (int) gd.getNextNumber();
		num_tries_fit =      (int) gd.getNextNumber();
		update_match=              gd.getNextBoolean();
		update_match=              gd.getNextBoolean();
@@ -292,6 +296,7 @@ public class ComboMatch {
		create_overlaps =           gd.getNextBoolean();
		create_overlaps =           gd.getNextBoolean();
		equalize_overlaps =         gd.getNextBoolean();
		equalize_overlaps =         gd.getNextBoolean();
		create_map =                gd.getNextBoolean();
		create_map =                gd.getNextBoolean();
		create_equalize =           gd.getNextBoolean();
		frac_remove =               gd.getNextNumber();
		frac_remove =               gd.getNextNumber();
		metric_error=               gd.getNextNumber();
		metric_error=               gd.getNextNumber();
		if (use_marked_image ) { // will only be used if found and asked
		if (use_marked_image ) { // will only be used if found and asked
@@ -778,20 +783,28 @@ public class ComboMatch {
			OrthoMultiLMA.buildOrthoMap(
			OrthoMultiLMA.buildOrthoMap(
					clt_parameters,            // CLTParameters       clt_parameters,
					clt_parameters,            // CLTParameters       clt_parameters,
					maps_collection, // OrthoMapsCollection maps_collection
					maps_collection, // OrthoMapsCollection maps_collection
					orthoMapsCollection_path); // String orthoMapsCollection_path
					orthoMapsCollection_savepath); // String orthoMapsCollection_path
			return true;
		}

		if (create_equalize) {
			OrthoEqualizeLMA.buildEqualize(
					clt_parameters,            // CLTParameters       clt_parameters,
					maps_collection, // OrthoMapsCollection maps_collection
					orthoMapsCollection_savepath); // String orthoMapsCollection_path
			return true;
			return true;
		}
		}
		
		
		if (create_overlaps) {
		if (create_overlaps) {
			boolean ok =maps_collection.getIntersectedPairs(
			boolean ok =maps_collection.getIntersectedPairs(
					clt_parameters, // CLTParameters    clt_parameters,
					clt_parameters, // CLTParameters    clt_parameters,
					orthoMapsCollection_path); // String orthoMapsCollection_path);
					orthoMapsCollection_savepath); // String orthoMapsCollection_path);
			return ok; // Just exit, do not try other commands.  if (!ok) return false;
			return ok; // Just exit, do not try other commands.  if (!ok) return false;
		}
		}
		if (equalize_overlaps) {
		if (equalize_overlaps) {
			boolean ok =maps_collection.equalizeIntersectedPairs(
			boolean ok =maps_collection.equalizeIntersectedPairs(
					clt_parameters, // CLTParameters    clt_parameters,
					clt_parameters, // CLTParameters    clt_parameters,
					orthoMapsCollection_path); // String orthoMapsCollection_path);
					orthoMapsCollection_savepath); // String orthoMapsCollection_path);
			return ok; // Just exit, do not try other commands.  if (!ok) return false;
			return ok; // Just exit, do not try other commands.  if (!ok) return false;
		}
		}
		
		
+659 −0

File added.

Preview size limit exceeded, changes collapsed.

+4 −1
Original line number Original line Diff line number Diff line
@@ -117,6 +117,9 @@ public class OrthoMap implements Comparable <OrthoMap>, Serializable{
	public transient double                     sfm_gain = Double.NaN;     // maximal SfM gain of this map
	public transient double                     sfm_gain = Double.NaN;     // maximal SfM gain of this map
	public transient double []                  equalize = {1,0}; // rectified value = equalize[0]*source_value+equalize[1]
	public transient double []                  equalize = {1,0}; // rectified value = equalize[0]*source_value+equalize[1]
	private void writeObject(ObjectOutputStream oos) throws IOException {
	private void writeObject(ObjectOutputStream oos) throws IOException {
		// temporary fix:
//		double [][] affine_clone = {affine[0].clone(), affine[1].clone()};
//		affine = affine_clone;
		oos.defaultWriteObject();
		oos.defaultWriteObject();
		oos.writeObject(path);
		oos.writeObject(path);
	//	oos.writeObject(scenes_path);
	//	oos.writeObject(scenes_path);
@@ -511,7 +514,7 @@ public class OrthoMap implements Comparable <OrthoMap>, Serializable{
	}
	}
	
	
	public void setAffine(double [][] affine) {
	public void setAffine(double [][] affine) {
		this.affine = affine;
		this.affine = new double[][] {affine[0].clone(),affine[1].clone()};
	}
	}
	public double [][] getAffine(){
	public double [][] getAffine(){
		return affine;
		return affine;
+25 −8
Original line number Original line Diff line number Diff line
@@ -4893,7 +4893,7 @@ public class OrthoMapsCollection implements Serializable{
			int debugLevel) {
			int debugLevel) {
		int [] indices = getScenesSelection(
		int [] indices = getScenesSelection(
				null, // boolean select_all,
				null, // boolean select_all,
				" to process"); // String purpose)
				" to process/display"); // String purpose)
		if (indices == null) {
		if (indices == null) {
			return false;
			return false;
		}
		}
@@ -4907,7 +4907,7 @@ public class OrthoMapsCollection implements Serializable{
			int [] indices,
			int [] indices,
			int debugLevel) {
			int debugLevel) {
		boolean show_map_stats =      false;      
		boolean show_map_stats =      false;      
		boolean show_combo_map =      false; // true;
		boolean show_combo_map =      true;  // false; // true;
		boolean show_alt_map =        false;
		boolean show_alt_map =        false;
		boolean show_combo_mask =     false; // generate image mas (where it is defined)
		boolean show_combo_mask =     false; // generate image mas (where it is defined)
		boolean show_frames =         false;
		boolean show_frames =         false;
@@ -4920,6 +4920,8 @@ public class OrthoMapsCollection implements Serializable{
		boolean show_centers =        true;
		boolean show_centers =        true;
		boolean bounds_to_indices =   true;
		boolean bounds_to_indices =   true;
		boolean merge_layers =        false;  // instead of individuals
		boolean merge_layers =        false;  // instead of individuals
		boolean ignore_affines =      false;
		boolean ignore_equalize =     false;


		String        save_top_dir =      "/media/elphel/NVME/lwir16-proc/ortho_videos/debug/sept12-13/pattern_match/";
		String        save_top_dir =      "/media/elphel/NVME/lwir16-proc/ortho_videos/debug/sept12-13/pattern_match/";
		String        sub_dir =           "combo_maps";
		String        sub_dir =           "combo_maps";
@@ -4946,6 +4948,11 @@ public class OrthoMapsCollection implements Serializable{
		gd.addCheckbox    ("Bounds to selected images",  bounds_to_indices, "Set combo image bounds to selected images only. False - all images.");
		gd.addCheckbox    ("Bounds to selected images",  bounds_to_indices, "Set combo image bounds to selected images only. False - all images.");
		gd.addCheckbox    ("Merge layers",               merge_layers, "Generate composite binary image.");
		gd.addCheckbox    ("Merge layers",               merge_layers, "Generate composite binary image.");


		gd.addCheckbox    ("Ignore affines",             ignore_affines, "Ignore available affines, use unity.");
		gd.addCheckbox    ("Ignore equalization",        ignore_equalize, "Ignore available intensity equalization, use unity.");

		
		
		gd.addStringField ("Pattern match save directory",      save_top_dir, 120, "Top directory to save combo maps");
		gd.addStringField ("Pattern match save directory",      save_top_dir, 120, "Top directory to save combo maps");
		gd.addStringField ("Save subdirectory",                 sub_dir, 80, "Subdirectory for versions of the same scene/pair of scenes");
		gd.addStringField ("Save subdirectory",                 sub_dir, 80, "Subdirectory for versions of the same scene/pair of scenes");
		gd.addCheckbox    ("Show generated images",             show_images, "Display generated images.");
		gd.addCheckbox    ("Show generated images",             show_images, "Display generated images.");
@@ -4968,7 +4975,8 @@ public class OrthoMapsCollection implements Serializable{
		show_centers =       gd.getNextBoolean();
		show_centers =       gd.getNextBoolean();
		bounds_to_indices =  gd.getNextBoolean();
		bounds_to_indices =  gd.getNextBoolean();
		merge_layers =       gd.getNextBoolean();
		merge_layers =       gd.getNextBoolean();

		ignore_affines =     gd.getNextBoolean();
		ignore_equalize =    gd.getNextBoolean();
		save_top_dir=        gd.getNextString();
		save_top_dir=        gd.getNextString();
		sub_dir=             gd.getNextString();
		sub_dir=             gd.getNextString();
		show_images=         gd.getNextBoolean();
		show_images=         gd.getNextBoolean();
@@ -5015,13 +5023,22 @@ public class OrthoMapsCollection implements Serializable{
			int [] wh =     new int[2];
			int [] wh =     new int[2];
			int [] origin = new int[2];
			int [] origin = new int[2];
			double [][] centers = show_centers? (new double [indices.length][]): null;
			double [][] centers = show_centers? (new double [indices.length][]): null;
			double [][][] affines = null;
			if (ignore_affines) {
				affines = new double [indices.length][2][3];
				for (int i = 0; i < indices.length; i++) {
					affines[i][0][0] = 1;
					affines[i][1][1] = 1;
				}
			}
			
			double [][] dmulti = renderMultiDouble (
			double [][] dmulti = renderMultiDouble (
					null,              // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
					null,              // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
					indices,           // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
					indices,           // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
					bounds_to_indices, // boolean       bounds_to_indices,
					bounds_to_indices, // boolean       bounds_to_indices,
					null,              // affines,    // double [][][] affines, // null or [indices.length][2][3]
					affines,           // null,            // affines,    // double [][][] affines, // null or [indices.length][2][3]
					null,              // double [][]   equalize,
					null,              // double [][]   equalize,
					true,              // boolean       ignore_equalize,
					ignore_equalize,   // true,              // boolean       ignore_equalize,
					null,              // warp,       // FineXYCorr    warp,,
					null,              // warp,       // FineXYCorr    warp,,
					zoom_lev,          // int         zoom_level,
					zoom_lev,          // int         zoom_level,
					wh,                // int []      wh,
					wh,                // int []      wh,
+61 −91
Original line number Original line Diff line number Diff line
/**
/**
 **
 **
 ** OrthoMultiLMA - Fit multiple scenes orthographic using pair-wise affine
 ** OrthoMultiLMA - Fit multiple scenes orthographic using pair-wise affine
 **                tr4ansform matrices already calculated.
 **                transform matrices already calculated.
 **
 **
 ** Copyright (C) 2024 Elphel, Inc.
 ** Copyright (C) 2024 Elphel, Inc.
 **
 **
@@ -48,8 +48,6 @@ public class OrthoMultiLMA {
	private double []         parameters_vector = null;
	private double []         parameters_vector = null;
//	private double []         x_vector =        null; // not used. Save total weight
//	private double []         x_vector =        null; // not used. Save total weight
	private double []         y_vector =        null;
	private double []         y_vector =        null;
	private double [][]       tile_centers =    null;
	private double            weight = 0; // total weight
	private double            pure_weight; // weight of samples only as a fraction of total weight
	private double            pure_weight; // weight of samples only as a fraction of total weight
	private double []         weights; // normalized so sum is 1.0 for all - samples and extra regularization
	private double []         weights; // normalized so sum is 1.0 for all - samples and extra regularization
//	private double            pure_weight; // weight of samples only	
//	private double            pure_weight; // weight of samples only	
@@ -219,17 +217,18 @@ public class OrthoMultiLMA {
			OrthoMapsCollection maps_collection,
			OrthoMapsCollection maps_collection,
			String              orthoMapsCollection_path
			String              orthoMapsCollection_path
			) {
			) {
		int                 debugLevel = 1;
		int                 debugLevel =         2;
		boolean             move_only =      false;
		boolean             move_only =      false;
		double []           val_coord =      null; // 1 - valid, 0 - invalid, minimize coordinates errors
		double []           val_coord =      null; // 1 - valid, 0 - invalid, minimize coordinates errors
		boolean             ignore_affines = false;
        boolean             use_inv =        false;
        boolean             use_inv =        false;
        double              overlap_pow =    2.0; // match weight as overlap fraction to this power
        double              overlap_pow =    2.0; // match weight as overlap fraction to this power
        
        double              skew_pull =      1.0;
        double              skew_pull =      1.0;
        double              tilt_pull =      1.0;
        double              tilt_pull =      1.0;
        double              scale_pull =     0.1; // .0;
        double              scale_pull =     0.1; // .0;
		double              position_pull =  0.0001;
		double              position_pull =  0.0001;
		boolean             corr_avg= (skew_pull > 0) || (tilt_pull > 0) || (scale_pull > 0);
		boolean             corr_avg= (skew_pull > 0) || (tilt_pull > 0) || (scale_pull > 0);
		boolean             show_result_image = false;
		int [] indices = maps_collection.getScenesSelection(
		int [] indices = maps_collection.getScenesSelection(
				null, // boolean select_all,
				null, // boolean select_all,
				" to build a map"); // String purpose)
				" to build a map"); // String purpose)
@@ -247,6 +246,7 @@ public class OrthoMultiLMA {
				clt_parameters,   // CLTParameters       clt_parameters,
				clt_parameters,   // CLTParameters       clt_parameters,
				maps_collection,  // OrthoMapsCollection maps_collection,
				maps_collection,  // OrthoMapsCollection maps_collection,
				indices,          // int []              indices,
				indices,          // int []              indices,
				ignore_affines,  // boolean             ignore_affines,
				val_coord,        // double []           val_coord, // 1 - valid, 0 - invalid, minimize coordinates errors
				val_coord,        // double []           val_coord, // 1 - valid, 0 - invalid, minimize coordinates errors
				position_pull,    // double              position_pull,
				position_pull,    // double              position_pull,
				skew_pull,        // double              skew_pull,
				skew_pull,        // double              skew_pull,
@@ -273,26 +273,10 @@ public class OrthoMultiLMA {
        }
        }
        
        
        //Get and apply affines
        //Get and apply affines
    	//oml.updateAffines(maps_collection);
        oml.updateAffines(maps_collection);
        if (show_result_image) {
        	double [][][] affines = oml.getAffines();
        	double [][][] affines = oml.getAffines();
        	double [] fx = oml.getFx();
        	double [] fx = oml.getFx();
        // test
        /*
		PairwiseOrthoMatch match = maps_collection.ortho_maps[indices[0]].getMatch(maps_collection.ortho_maps[indices[1]].getName());
		double [][] match_affine = match.getAffine(); 
		
		double [] enuOffset = maps_collection.ortho_maps[indices[1]].enuOffsetTo(maps_collection.ortho_maps[indices[0]]);
		double [] rd = {enuOffset[0], -enuOffset[1]}; // {right,down} of the image 
        
		double [][] affine1a = PairwiseOrthoMatch.combineAffines( 
				affines[0],           // double [][] affine0,
				match_affine, //double [][] affine, // differential
				rd);                // double [] rd);
		PairwiseOrthoMatch test_match = new PairwiseOrthoMatch (
				affines[0], // double [][] affine0,
				affines[1], // double [][] affine1,
				rd); // double [] rd);
        */


        	int         zoom_level = -2;
        	int         zoom_level = -2;
        	int [] wh = new int[2];
        	int [] wh = new int[2];
@@ -328,21 +312,8 @@ public class OrthoMultiLMA {
        	roi.setOptions("label");
        	roi.setOptions("label");
        	imp_multi.setRoi(roi);
        	imp_multi.setRoi(roi);
        	imp_multi.show();
        	imp_multi.show();
		/*
        }
		double [][] affine = 		getAffineAndDerivatives(
				move_only,      //boolean       move_only,
				affines[0],     // double [][]   affine00,
				affines[1],     // double [][]   affine10,
				oml.offsets[0], // double []     rd,
				null);          // double [][][] derivs)
		PairwiseOrthoMatch pom = new PairwiseOrthoMatch (
				affines[0],
				affines[1],
				oml.offsets[0]);
		*/
		


		/*
        if (orthoMapsCollection_path != null) {
        if (orthoMapsCollection_path != null) {
        	try {
        	try {
        		maps_collection.writeOrthoMapsCollection(orthoMapsCollection_path);
        		maps_collection.writeOrthoMapsCollection(orthoMapsCollection_path);
@@ -354,7 +325,6 @@ public class OrthoMultiLMA {
        		System.out.println("Saved data to "+ orthoMapsCollection_path);
        		System.out.println("Saved data to "+ orthoMapsCollection_path);
        	}
        	}
        }
        }
		*/
        return 0;
        return 0;
	}
	}
	
	
@@ -373,6 +343,7 @@ public class OrthoMultiLMA {
			CLTParameters       clt_parameters,
			CLTParameters       clt_parameters,
			OrthoMapsCollection maps_collection,
			OrthoMapsCollection maps_collection,
			int []              indices,
			int []              indices,
			boolean             ignore_affines,
			double []           val_coord, // 1 - valid, 0 - invalid, minimize coordinates errors
			double []           val_coord, // 1 - valid, 0 - invalid, minimize coordinates errors
			double              position_pull,
			double              position_pull,
	        double              skew_pull,
	        double              skew_pull,
@@ -434,7 +405,7 @@ public class OrthoMultiLMA {
		weights =           new double [n62*num_pairs + 2 * num_scenes + (corr_avg ? 3:0)];
		weights =           new double [n62*num_pairs + 2 * num_scenes + (corr_avg ? 3:0)];
		parameters_vector = new double [n62*num_scenes]; // maybe will need move-only mode?
		parameters_vector = new double [n62*num_scenes]; // maybe will need move-only mode?
		for (int n = 0; n < num_scenes; n++) {
		for (int n = 0; n < num_scenes; n++) {
			double [][] affine = maps_collection.ortho_maps[indices[n]].getAffine();
			double [][] affine = ignore_affines? (new double[][] {{1,0,0},{0,1,0}}):maps_collection.ortho_maps[indices[n]].getAffine();
			System.arraycopy(affine[0], 0, parameters_vector, n62*n, n13);
			System.arraycopy(affine[0], 0, parameters_vector, n62*n, n13);
			System.arraycopy(affine[1], 0, parameters_vector, n62*n + n13, n13);
			System.arraycopy(affine[1], 0, parameters_vector, n62*n + n13, n13);
			y_vector[n62*num_pairs+2*n + 0] = affine[0][2]; 
			y_vector[n62*num_pairs+2*n + 0] = affine[0][2]; 
@@ -760,7 +731,7 @@ public class OrthoMultiLMA {
		return affine; // _alt;
		return affine; // _alt;
	}
	}
	
	
	private static int [][] createNonIntersectingPairs(int [][] pairs) {
	public static int [][] createNonIntersectingPairs(int [][] pairs) {
		ArrayList<Integer> pairs_list = new ArrayList<Integer>();
		ArrayList<Integer> pairs_list = new ArrayList<Integer>();
		for (int i = 0; i < pairs.length; i++) {
		for (int i = 0; i < pairs.length; i++) {
			pairs_list.add(i);
			pairs_list.add(i);
@@ -835,7 +806,7 @@ public class OrthoMultiLMA {
				System.out.println("LMA step "+iter+": {"+rslt[0]+","+rslt[1]+"} full RMS= "+good_or_bad_rms[0]+
				System.out.println("LMA step "+iter+": {"+rslt[0]+","+rslt[1]+"} full RMS= "+good_or_bad_rms[0]+
						" ("+initial_rms[0]+"), pure RMS="+good_or_bad_rms[1]+" ("+initial_rms[1]+") + lambda="+lambda);
						" ("+initial_rms[0]+"), pure RMS="+good_or_bad_rms[1]+" ("+initial_rms[1]+") + lambda="+lambda);
			}
			}
			if (rslt[1]) {
			if (rslt[1]) {// the place to put a breakpoint
				break;
				break;
			}
			}
			if (rslt[0]) { // good
			if (rslt[0]) { // good
@@ -1035,7 +1006,6 @@ public class OrthoMultiLMA {
		return rslt;
		return rslt;
	}
	}



	private double [][] getWJtJlambda(
	private double [][] getWJtJlambda(
			final double      lambda,
			final double      lambda,
			final double [][] jt)
			final double [][] jt)
@@ -1129,11 +1099,11 @@ public class OrthoMultiLMA {
		return last_rms;
		return last_rms;
		
		
	}
	}
	
	public double [] getInitialRms() {
	public double [] getInitialRms() {
		return initial_rms;
		return initial_rms;
	}	
	}	
	
	
	
	private double [] getFxDerivs(
	private double [] getFxDerivs(
			final double []   vector,
			final double []   vector,
			final double [][] jt, // should be null or initialized with [vector.length][]
			final double [][] jt, // should be null or initialized with [vector.length][]
Loading