Commit f7f4a8dd authored by Andrey Filippov's avatar Andrey Filippov
Browse files

Pairwise equalization

parent 1671728b
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -157,6 +157,7 @@ public class ComboMatch {
//		boolean show_map_stats =      false;      
		boolean show_combo =          false; // true;
		boolean create_overlaps =     false;
		boolean equalize_overlaps =   false;
		boolean create_map =          false;
//		boolean show_combo_mask =     false; // generate image mas (where it is defined)_
//		boolean use_alt =             false;
@@ -232,6 +233,7 @@ public class ComboMatch {
//		gd.addCheckbox    ("Show statistics for ortho images", show_map_stats, "Generate and show statistics for ortho maps.");
		gd.addCheckbox    ("Show combo maps/stats",            show_combo, "Generate/save combo maps and stats.");
		gd.addCheckbox    ("Create overlap pairs",             create_overlaps, "Create scene pairs 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    ("Show combo image mask",            show_combo_mask, "Display combo binary image.");
//		gd.addCheckbox    ("Show altitude combo image", use_alt, "Load and process altitude maps.");
@@ -288,6 +290,7 @@ public class ComboMatch {
		
		show_combo =                gd.getNextBoolean();
		create_overlaps =           gd.getNextBoolean();
		equalize_overlaps =         gd.getNextBoolean();
		create_map =                gd.getNextBoolean();
		frac_remove =               gd.getNextNumber();
		metric_error=               gd.getNextNumber();
@@ -783,8 +786,15 @@ public class ComboMatch {
			boolean ok =maps_collection.getIntersectedPairs(
					clt_parameters, // CLTParameters    clt_parameters,
					orthoMapsCollection_path); // String orthoMapsCollection_path);
			if (!ok) return false;
			return ok; // Just exit, do not try other commands.  if (!ok) return false;
		}
		if (equalize_overlaps) {
			boolean ok =maps_collection.equalizeIntersectedPairs(
					clt_parameters, // CLTParameters    clt_parameters,
					orthoMapsCollection_path); // String orthoMapsCollection_path);
			return ok; // Just exit, do not try other commands.  if (!ok) return false;
		}
		
        if (process_correlation || render_match || pattern_match || test_multi_lma) {
//        	int [] gpu_pair;
        	if (gpu_spair == null) {
+213 −43
Original line number Diff line number Diff line
@@ -550,21 +550,10 @@ public class OrthoMapsCollection implements Serializable{
			boolean       show_centers,
			int           zoom_level,
			int []        origin){
		boolean show_2d_histogram = true; // false;
		int num_images =  (indices != null)? indices.length : ortho_maps.length;
		int []     wh = new int[2];
		double [][] centers = new double [(indices !=null)? indices.length: ortho_maps.length][];
		/*
		float [][] multi = renderMulti (
				OrthoMapsCollection.MODE_IMAGE,       // int           mode,    // 0 - regular image, 1 - altitudes, 2 - black/white mask
				indices,    // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
				bounds_to_indices, // boolean       bounds_to_indices,
				affines,    // double [][][] affines, // null or [indices.length][2][3]
				warp,       // FineXYCorr    warp,
				zoom_level, // int         zoom_level,
				wh,         // int []      wh,
				origin,     // int []      origin){ // maps[0] as a reference
				centers);   // double [][] centers)
		*/
		double [][] dmulti = renderMultiDouble (
				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)
@@ -579,10 +568,12 @@ public class OrthoMapsCollection implements Serializable{
				centers);          // double [][] centers)
		
		if ((eq_mode == 2) && (num_images == 2)) {
			boolean [] mask = new boolean [dmulti[0].length];
			boolean [] mask = null;
			if (show_2d_histogram) {
				mask = new boolean [dmulti[0].length];
				//			int hist_min = -140,hist_max=135; // 700,200
				//			int hist_min = -100,hist_max=100; // 700,200
			int hist_min = -250,hist_max=100; // 700,200
				int hist_min = -350, hist_max=350; // 700,200
				int hist_width= hist_max-hist_min;
				double [] hist = new double [hist_width*hist_width];
				for (int i = 0; i < dmulti[0].length; i++) if (!Double.isNaN(dmulti[0][i]) && !Double.isNaN(dmulti[1][i])){
@@ -599,8 +590,7 @@ public class OrthoMapsCollection implements Serializable{
						hist_width,
						hist_width,
						title+"hist_"+hist_min+"_"+hist_max);
			
			double [] dmulti1 = dmulti[1].clone();
			}
			double [] regression = PolynomialApproximation.getOrthoRegression(
					dmulti[0], // final double []  data_x,
					dmulti[1], // final double []  data_y,
@@ -610,6 +600,8 @@ public class OrthoMapsCollection implements Serializable{
					dmulti[1], // final double []  data_x,
					dmulti[0], // final double []  data_y,
					mask);     // final boolean [] mask)
			/*
			double [] dmulti1 = dmulti[1].clone();
			double [] regression00 = PolynomialApproximation.getOrthoRegression(
					dmulti[0], // final double []  data_x,
					dmulti[0], // final double []  data_y,
@@ -618,9 +610,11 @@ public class OrthoMapsCollection implements Serializable{
					dmulti[1], // final double []  data_x,
					dmulti[1], // final double []  data_y,
					mask);     // final boolean [] mask)
			*/
			PolynomialApproximation.applyRegression(
					dmulti1,       // final double [] data, // clone by caller
					dmulti[1], // dmulti1,       // final double [] data, // clone by caller
					inv_regression); // final double [] regression) {
			/*
			double [] regression01 = PolynomialApproximation.getOrthoRegression(
					dmulti[0], // final double []  data_x,
					dmulti1, // final double []  data_y,
@@ -652,6 +646,7 @@ public class OrthoMapsCollection implements Serializable{
			double [] regression_single = getYXRegression(
					dmulti[0], // final double []  data_x,
					dmulti[1]); // final double []  data_y,
			*/
			System.out.println();
		}
		
@@ -4134,6 +4129,181 @@ public class OrthoMapsCollection implements Serializable{
		return indices;
	}
	
	public boolean equalizeIntersectedPairs(
			CLTParameters    clt_parameters,
			String orthoMapsCollection_path) {
		boolean  use_inv = false;
		int [] indices = getScenesSelection(
				null, // boolean select_all,
				" to build a equalize intensities"); // String purpose)
		PairwiseOrthoMatch [][] matches = new PairwiseOrthoMatch[indices.length][indices.length];
		ArrayList<Point> pairs_list = new ArrayList<Point>();
		int num_new = 0;
		for (int i = 0; i < indices.length-1; i++) {
			int scene0 = indices[i]; 
			for (int j = i+1; j < indices.length; j++){
				int scene1 = indices[j];
				PairwiseOrthoMatch match = ortho_maps[scene0].getMatch(ortho_maps[scene1].getName());
				PairwiseOrthoMatch inv_match = use_inv? ortho_maps[scene1].getMatch(ortho_maps[scene0].getName()):null;
				if ((match != null) || (inv_match != null)){
					if (match == null) {
						double [] enuOffset = ortho_maps[scene0].enuOffsetTo(ortho_maps[scene1]);
						double [] rd = {enuOffset[0], -enuOffset[1]}; // {right,down} of the image 
						match = inv_match.getInverse(rd);
					}
					if (inv_match == null) {
						double [] enuOffset = ortho_maps[scene1].enuOffsetTo(ortho_maps[scene0]);
						double [] rd = {enuOffset[0], -enuOffset[1]}; // {right,down} of the image 
						inv_match = match.getInverse(rd);
					}
				}
				if (match != null) {
					if (!match.isSetEqualize2to1()) {
						num_new++;
					}
					matches[i][j] = match;
					matches[j][i] = inv_match;
					pairs_list.add(new Point(i,j)); // only once?
				}
			}
		}
	
		boolean      skip_exist    = clt_parameters.imp.pwise_skip_exist;   //
		boolean      save_each     = clt_parameters.imp.pwise_save_each;    // save state file after each match
		boolean      log_append    = clt_parameters.imp.pwise_log_append;   //
		String       log_path      = clt_parameters.imp.pwise_log_path;     // 
		int          debugLevel    = clt_parameters.imp.pwise_debug;        //

		GenericJTabbedDialog gd = new GenericJTabbedDialog("Pairwise Match Parameters",1200,1000);
		gd.addMessage("Number of scenes - "+indices.length+
				", number of pairs - "+pairs_list.size()+
				", number of new pairs - "+num_new);
	    gd.addCheckbox    ("Skip existing",              skip_exist, "Do not regenerate if match with same or higher resolution exists.");
   	    gd.addCheckbox    ("Save state after each pair", save_each, "Update state file after each match generation to mitigate possible crashes.");
	    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.");	
		gd.addNumericField("Pairwise match debug level",  debugLevel, 0,3,"","Debug level during Spiral search.");
 		gd.showDialog();
		if (gd.wasCanceled()) return false;
		skip_exist       = gd.getNextBoolean();
		save_each        = gd.getNextBoolean();
		log_append       = gd.getNextBoolean();
		log_path         = gd.getNextString();
		debugLevel = (int) gd.getNextNumber();
		if (log_append && (log_path != null)) { // assuming directory exists
			StringBuffer sb = new StringBuffer();
			sb.append(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime())+"\n");
			sb.append("num_scenes\t"+  indices.length+"\n");
			sb.append("num_pairs\t"+   pairs_list.size()+"\n");
			sb.append("num_new\t"+     num_new+"\n");
			sb.append(String.format("%4s\t%4s\t%17s\t%17s\t%6s\t%3s\t%6s\t%8s\t%4s\n",
					"scn1","scn2","timestamp1","timestamp2","ovrlp","zl","a","b","old"));
			CalibrationFileManagement.saveStringToFile (
					log_path,          //String path,
					sb.toString(), // data,
					true); // boolean append)
			if (debugLevel>-3) {
				System.out.print(sb.toString());
			}
		}
		
		for (Point p:pairs_list) {
			StringBuffer sb = new StringBuffer();
			PairwiseOrthoMatch match = matches[p.x][p.y];
			int scene0 = indices[p.x]; 
			int scene1 = indices[p.y];
			int [] indices_pair = {scene0,scene1}; 

			if (skip_exist && match.isSetEqualize2to1()) {
				sb.append(String.format("%4d\t%4d\t%s\t%s\t%6.4f\t%3d\t%6.4f\t%8.4f\tOLD\n",
						scene0, scene1, ortho_maps[scene0].getName(), ortho_maps[scene1].getName(),
						match.overlap,  match.zoom_lev, match.equalize1to0[0],match.equalize1to0[1]));
				
			} else {
				double [][][] affines = {{{1,0,0},{0,1,0}},match.getAffine()};
				int []     wh = new int[2];
				int [] origin = new int[2];			
				double [][] centers = new double [indices.length][];
				double [][] dmulti = renderMultiDouble (
						null,              // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
						indices_pair,           // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
						true, // boolean       bounds_to_indices,
						affines,           // affines,    // double [][][] affines, // null or [indices.length][2][3]
						null,              // double [][]   equalize,
						true,              // boolean       ignore_equalize,
						null,              // warp,       // FineXYCorr    warp,,
						match.zoom_lev,    // int         zoom_level,
						wh,                // int []      wh,
						origin,            // int []      origin){ // maps[0] as a reference
						centers);          // double [][] centers)

				double [] regression = PolynomialApproximation.getOrthoRegression(
						dmulti[0], // final double []  data_x,
						dmulti[1], // final double []  data_y,
						null);     // final boolean [] mask)
				double [] inv_regression = PolynomialApproximation.invertRegression(regression);
				PolynomialApproximation.applyRegression(
						dmulti[1], // dmulti1,       // final double [] data, // clone by caller
						inv_regression); // final double [] regression) {
				match.setEqualize2to1(inv_regression);
				if (save_each && (orthoMapsCollection_path != null)) {
					try {
						writeOrthoMapsCollection(orthoMapsCollection_path);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (debugLevel > -4) {
						System.out.println("Saved data to "+ orthoMapsCollection_path);
					}
				}
				sb.append(String.format("%4d\t%4d\t%s\t%s\t%6.4f\t%3d\t%6.4f\t%8.4f\tNEW\n",
						scene0, scene1, ortho_maps[scene0].getName(), ortho_maps[scene1].getName(),
						match.overlap,  match.zoom_lev, match.equalize1to0[0],match.equalize1to0[1]));
			}
			if (log_append && (log_path != null)) { // assuming directory exists
				CalibrationFileManagement.saveStringToFile (
						log_path,          //String path,
						sb.toString(), // data,
						true); // boolean append)
			}			
			if (debugLevel>-3) {
				System.out.print(sb.toString());
			}
			
		}
		
		if (orthoMapsCollection_path != null) {
			try {
				writeOrthoMapsCollection(orthoMapsCollection_path);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (debugLevel > -4) {
				System.out.println("Saved data to "+ orthoMapsCollection_path);
			}
		}
		StringBuffer sb = new StringBuffer();
		sb.append("\nSUMMARY\n");
		sb.append("processed\t"+(skip_exist?num_new:pairs_list.size()) +"\n");
		sb.append("new\t"+ num_new+"\n");
		sb.append("skipped\t"+(skip_exist?(pairs_list.size()-num_new):0)+"\n");
    	if (debugLevel > -3) {
    		System.out.print(sb.toString());
    	}
		if (log_append && (log_path != null)) { // assuming directory exists
			CalibrationFileManagement.saveStringToFile (
					log_path,          //String path,
					sb.toString(), // data,
					true); // boolean append)
        	if (debugLevel > -4) {
        		System.out.println("Appended log file "+ log_path);
        	}
		}		
		return true;
	}
	
	public boolean getIntersectedPairs(
			CLTParameters    clt_parameters,
			String orthoMapsCollection_path) {
+11 −0
Original line number Diff line number Diff line
@@ -33,6 +33,17 @@ public class PairwiseOrthoMatch implements Serializable {
		this.overlap = overlap;
	}
	
	public double [] getEqualize2to1() {
		return equalize1to0;
	}
	public boolean isSetEqualize2to1() {
		if (equalize1to0 == null) {
			return false;
		}
		return (equalize1to0[0] != 1.0) || (equalize1to0[1] != 0.0);
	}
	
	
	public void setEqualize2to1 (double [] equalize2to1) {
		this.equalize1to0 = equalize2to1;
	}