Commit 8a9605a5 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

Adding memory saving and loading from a file list

parent 6845c018
Loading
Loading
Loading
Loading
+138 −9
Original line number Diff line number Diff line
package com.elphel.imagej.orthomosaic;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

@@ -24,6 +31,8 @@ public class ComboMap {
	public static boolean FIX_VERT_Y = false; // true; // temporarily fix vertical Y coordinate bug (use -GCORR in the filename?)
	public static int gpu_width =  4096;
	public static int gpu_height =  4096;
	public static final String [] KEY_DIRS= {"rootDirectory", // from EyesisCorrectionParameters
			"sourceDirectory","linkedModels","videoDirectory","x3dDirectory","resultsDirectory"};
	public static final String ALT_SUFFIX = "-ALT";
	public final String               name;  // timestamp
	public String                     path;        // full path to the model directory (including /vXX?)
@@ -41,7 +50,7 @@ public class ComboMap {
	public double                     need_extra_zoom;
	HashMap <Integer, FloatImageData> images;
	
	public static void setGPUWIdthHeight(
	public static void setGPUWidthHeight(
			int width,
			int height) {
		gpu_width = width;
@@ -87,6 +96,83 @@ public class ComboMap {
		need_extra_zoom = FloatImageData.needZoomIn(orig_pix_meters);
		images = new HashMap <Integer, FloatImageData>();
	}
	
	public static ComboMap[] initializeComboMaps(
			String [] image_paths) { // full paths, including ext
		double [][] affine = {{1,0,0},{0,1,0}}; // maybe later calculate from mage_enuatr
		ComboMap[] combo_maps = new ComboMap[image_paths.length];
		for (int n = 0; n < combo_maps.length; n++) { 
			combo_maps[n] = new ComboMap(image_paths[n]);
			combo_maps[n].setAffine(affine);
		}
		return combo_maps;
	}
	
	public static ComboMap[] initializeComboMaps(
			String files_list) {
        List<String> lines;
        List<String> rel_files = new ArrayList<String>(); 
		Path seq_path = Paths.get(files_list);
		try {
			lines = Files.readAllLines(seq_path, StandardCharsets.UTF_8);
			//                lines.stream().forEach(System.out::println);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		Path base_path = seq_path.getParent();
		HashMap<String,String> dir_map = new HashMap<String,String>();
		for (String line:lines){
			if (line.split("#").length > 0) {
				String[] tokens = line.split("#")[0].trim().split("[\\s,;=]+");
				if ((tokens.length > 2) && (tokens[0].toUpperCase().equals("SET"))) {
					parse_set:
					{
						for (String dir_name:KEY_DIRS) if (dir_name.equals(tokens[1])) {
							dir_map.put(dir_name,tokens[2]);
							System.out.println("Parsed SET: "+tokens[1]+" in line: "+line);
							break parse_set;
						}
						System.out.println("*********** Unknown SET: "+tokens[1]+" in line: "+line);
					}
				} else if ((tokens.length == 1) && (tokens[0].length() > 0)) {
					rel_files.add(tokens[0]);
				}
			}
		}
        if (dir_map.containsKey("rootDirectory")) {
        	base_path=base_path.resolve(Paths.get(dir_map.get("rootDirectory")));
			File base_dir = new File(base_path.toString());
//			if (!base_dir.exists()) {
//				base_dir.mkdirs();
//			}
        }
        String sourceDirectory = base_path.toString();
        // set sourceDirectory: 
        if (dir_map.containsKey("sourceDirectory")) {
        	sourceDirectory=(base_path.resolve(Paths.get(dir_map.get("sourceDirectory")))).toString();
        }
		Path source_path = Paths.get(sourceDirectory);
		File source_dir = new File(source_path.toString());
		String [] paths = new String[rel_files.size()];
		for (int i = 0; i < paths.length; i++) {
			paths[i] = (source_path.resolve(Paths.get(rel_files.get(i)))).toString();
		}
		return initializeComboMaps(paths);
	}
	
	
	
	public static String[] getNames(
			ComboMap[] maps) {
		String [] names = new String [maps.length];
		for (int n = 0; n < maps.length; n++) {
			names[n] = maps[n].getName();
		}
		return names;
	}

	
	public String getName() {
		return name;
	}
@@ -96,7 +182,50 @@ public class ComboMap {
	public double [][] getAffine(){
		return affine;
	}
	public boolean readImageData() {

	public void removeImageData() { // to save memory
		orig_image=null;
	}
	
	public void removeAltData() { // to save memory
		alt_image = null;
	}
	
	public void removeZoomLevel(int zoom_level) { // either to save memory or remove to later replace
		images.remove(zoom_level);
	}
	
	public int [] getZoomLevels() {
		Integer [] ks = (Integer[]) images.keySet().toArray();
		int [] zoom_levels = new int[ks.length];
		for (int i = 0; i < ks.length; i++) {
			zoom_levels[i] = ks[i];
		}
		return zoom_levels;
	}
	
	public void removeAllButZoomLevel(int zoom_level) { // either to save memory or remove to later replace
		int [] zoom_levels = getZoomLevels();
		for (int zl:zoom_levels) if (zl !=zoom_level) {
			images.remove(zl);
		}
	}

	
	public FloatImageData getImageData() {
		if (orig_image == null) {
			readImageData();
		}
		return orig_image;
	}
	public FloatImageData getAltData() {
		if (alt_image == null) {
			readAltData();
		}
		return alt_image;
	}
	
	private boolean readImageData() {
		ImagePlus imp = new ImagePlus(path);
		int width = imp.getWidth();
		int height = imp.getHeight();
@@ -115,7 +244,7 @@ public class ComboMap {
		return true;
	}

	public boolean readAltData() { // assuming the same scale as main image
	private boolean readAltData() { // assuming the same scale as main image
		ImagePlus imp = new ImagePlus(getAltPath(path));
		int width = imp.getWidth();
		int height = imp.getHeight();
@@ -129,6 +258,7 @@ public class ComboMap {
	}
	
	public ImagePlus getOriginalImage(boolean show_markers) {
		FloatImageData orig_image = getImageData();
		if (orig_image != null) {
			String full_name = path.substring(path.lastIndexOf(Prefs.getFileSeparator()) + 1);
			ImagePlus imp = ShowDoubleFloatArrays.makeArrays(
@@ -174,6 +304,7 @@ public class ComboMap {
	 * @return double [4][2] array [corner number]{x,y} 
	 */
	public double [][] get4SourceCornersMeters(){
		FloatImageData orig_image = getImageData();
		double width_meters = orig_image.width * orig_pix_meters;
		double height_meters = orig_image.height * orig_pix_meters;
		return new double[][] { // CW from TL
@@ -323,10 +454,8 @@ public class ComboMap {
		return iaffine;
	}
	
	public void removeZoomLevel(int zoom_level) { // either to save memory or remove to later replace
		images.remove(zoom_level);
	}
	public boolean downScaleForGPU (int zoom_level){
		FloatImageData orig_image = getImageData();
		if (orig_image == null) {
			System.out.println("Original image is null");
			return false;
@@ -546,9 +675,9 @@ public class ComboMap {
			double [][] src_bounds=maps[nmap].getBoundsMeters (true);
			final double [] src_center = {-src_bounds[0][0],-src_bounds[1][0]}; // x,y center offset in the source image
			final double [][] affine = maps[nmap].affine;
			final int src_width = use_alt? maps[nmap].alt_image.width: maps[nmap].orig_image.width;
			final int src_height = use_alt? maps[nmap].alt_image.height : maps[nmap].orig_image.height;
			final float [] src_img = use_alt? maps[nmap].alt_image.data : maps[nmap].orig_image.data;
			final int src_width = use_alt? maps[nmap].getAltData().width: maps[nmap].getImageData().width;
			final int src_height = use_alt? maps[nmap].getAltData().height : maps[nmap].getImageData().height;
			final float [] src_img = use_alt? maps[nmap].getAltData().data : maps[nmap].getImageData().data;
			final Thread[] threads = ImageDtt.newThreadArray();
			final AtomicInteger ai = new AtomicInteger(0);
			for (int ithread = 0; ithread < threads.length; ithread++) {
+32 −87
Original line number Diff line number Diff line
@@ -43,35 +43,6 @@ public class ComboMatch {
			 GPUTileProcessor gpu_tile_processor, // initialized by the caller
			 final int        debugLevel) {
		GPU_TILE_PROCESSOR = gpu_tile_processor;
		/*
/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877409_353265/1697877409_353265-RECT-PIX0.01-FLAT_CLN-VERT-GEO.tiff
/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877409_353265/1697877409_353265-RECT-PIX0.01-FLAT_CLN-VERT-GEO-ALT.tiff

/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877410_420287/1697877410_420287-RECT-PIX0.01-FLAT_CLN-VERT-GEO.tiff
/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877410_420287/1697877410_420287-RECT-PIX0.01-FLAT_CLN-VERT-GEO-ALT.tiff


/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877412_004148/1697877412_004148-RECT-PIX0.01-FLAT_CLN-VERT-GEO.tiff
/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877412_004148/1697877412_004148-RECT-PIX0.01-FLAT_CLN-VERT-GEO-ALT.tiff


/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877413_137859/1697877413_137859-RECT-PIX0.01-FLAT_CLN-VERT-GEO.tiff
/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877413_137859/1697877413_137859-RECT-PIX0.01-FLAT_CLN-VERT-GEO-ALT.tiff


/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877414_404948/1697877414_404948-RECT-PIX0.01-FLAT_CLN-VERT-GEO.tiff
/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877414_404948/1697877414_404948-RECT-PIX0.01-FLAT_CLN-VERT-GEO-ALT.tiff

/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877415_521986/1697877415_521986-RECT-PIX0.01-FLAT_CLN-VERT-GEO.tiff
/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877415_521986/1697877415_521986-RECT-PIX0.01-FLAT_CLN-VERT-GEO-ALT.tiff

		 */
		/*
		String [] image_paths_pre = {
				"/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877410_420287/1697877410_420287-RECT-PIX0.01-FLAT_CLN-VERT-GEO",
				"/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877412_004148/1697877412_004148-RECT-PIX0.01-FLAT_CLN-VERT-GEO"
		};
		*/
		String [] image_paths_pre_0 = {
//				"/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877410_420287/1697877410_420287-RECT-PIX0.01-FLAT_CLN-VERT-GEO",
//				"/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/1697877412_004148/1697877412_004148-RECT-PIX0.01-FLAT_CLN-VERT-GEO",
@@ -92,7 +63,9 @@ public class ComboMatch {
				"/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-c/1697877414_404948/1697877414_404948-RECT-PIX0.01-FLAT_CLN-VERT-GCORR-GEO",
				"/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-c/1697877415_521986/1697877415_521986-RECT-PIX0.01-FLAT_CLN-VERT-GCORR-GEO"
		};
		// find -L /media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/ -type f -name "*-GCORR-GEO.tiff" | sort  > GCORR-GEO.list

		String files_list_path = "/media/elphel/SSD3-4GB/lwir16-proc/berdich3/linked/linked_1697875868-1697879449-b/maps_01.list";
		
		double [][][] image_enuatr = {{{0,0,0},{0,0,0}},{{0,0,0},{0,0,0}}};
		int gpu_width=  clt_parameters.imp.rln_gpu_width; // 3008;
@@ -101,9 +74,10 @@ public class ComboMatch {
		boolean use_alt = false;
		boolean show_centers = true;
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Set image pair",1200,800);
		for (int n = 0; n < image_paths_pre.length; n++) {
			gd.addStringField ("Image path "+n,  image_paths_pre[n], 180, "Image "+n+" full path w/o ext");		
		}
		gd.addStringField ("Image list full path ",  files_list_path, 180, "Image listfull path.");
//		for (int n = 0; n < image_paths_pre.length; n++) {
//			gd.addStringField ("Image path "+n,  image_paths_pre[n], 180, "Image "+n+" full path w/o ext");		
//		}
//		gd.addStringField ("First image path",  image_paths_pre[0], 180, "First image full path w/o ext");		
//		gd.addStringField ("Second image path", image_paths_pre[1], 180, "Second image full path w/o ext");		
		for (int n = 0; n < image_enuatr.length; n++) {
@@ -126,9 +100,10 @@ public class ComboMatch {

		gd.showDialog();
		if (gd.wasCanceled()) return false;
		for (int n = 0; n < image_paths_pre.length; n++) {
			image_paths_pre[n] = gd.getNextString();
		}		
		files_list_path = gd.getNextString();
//		for (int n = 0; n < image_paths_pre.length; n++) {
//			image_paths_pre[n] = gd.getNextString();
//		}		
//		image_paths_pre[0] = gd.getNextString();
//		image_paths_pre[1] = gd.getNextString();
		for (int n = 0; n < image_enuatr.length; n++) {
@@ -143,23 +118,19 @@ public class ComboMatch {
		gpu_width =           (int) gd.getNextNumber();
		gpu_height =          (int) gd.getNextNumber();

		ComboMap.setGPUWIdthHeight(gpu_width,gpu_height);
		ComboMap.setGPUWidthHeight(gpu_width,gpu_height);
		
		show_centers =              gd.getNextBoolean();
		use_alt =                   gd.getNextBoolean();
		
		ComboMap[] combo_maps = new ComboMap[image_paths_pre.length];
		String [] map_names = new String[combo_maps.length];
		for (int n = 0; n < combo_maps.length; n++) { 
			combo_maps[n] = new ComboMap(image_paths_pre[n]+".tiff");
			combo_maps[n].readImageData();
			if (use_alt) {
				combo_maps[n].readAltData();
			}
			double [][] affine = {{1,0,0},{0,1,0}}; // maybe later calculate from mage_enuatr
			combo_maps[n].setAffine(affine);
			map_names[n] = combo_maps[n].getName();
		String [] full_paths = new String[image_paths_pre.length];
		for (int n = 0; n < full_paths.length; n++) { 
			full_paths[n] = image_paths_pre[n]+".tiff";
		}		
//		ComboMap[] combo_maps = ComboMap.initializeComboMaps(
//				full_paths); // String [] image_paths);
		ComboMap[] combo_maps = ComboMap.initializeComboMaps(
				files_list_path); // String [] image_paths);
		String [] map_names = ComboMap.getNames(combo_maps);
		
		int [] origin = new int[2];
		ImagePlus imp_img = 	ComboMap.renderMulti (
@@ -170,44 +141,18 @@ public class ComboMatch {
				zoom_lev,          // int         zoom_level,
				origin);           // int []      origin){
		imp_img.show();

		/*
		int []     wh = new int[2];
		double [][] centers = new double [combo_maps.length][];

		float [][] multi = ComboMap.renderMulti (
				false,      // boolean     use_alt,
				combo_maps, // ComboMap [] maps,
				zoom_lev,   // int         zoom_level,
				wh,         // int []      wh,
				origin,    // int []      origin){ // maps[0] as a reference
				centers); // double [][] centers)
		
        ShowDoubleFloatArrays.showArrays( 
        		multi,
        		wh[0],
        		wh[1],
        		true,
        		"multi_"+zoom_lev,
        		map_names);
		ImagePlus imp_alt = null;
        if (use_alt) {
        	float [][] multi_alt = ComboMap.renderMulti (
        	imp_alt =ComboMap.renderMulti (
    				"multi_alt_"+zoom_lev, // String      title,
    				true,             // boolean     use_alt,
    				show_centers,      // boolean     show_centers,
    				combo_maps,        // ComboMap [] maps,
    				zoom_lev,          // int         zoom_level,
        			wh,         // int []      wh,
    				origin,    // int []      origin){ // maps[0] as a reference
    				centers); // double [][] centers)
        	ShowDoubleFloatArrays.showArrays( 
        			multi_alt,
        			wh[0],
        			wh[1],
        			true,
        			"multi_alt_"+zoom_lev,
        			map_names);
    				origin);           // int []      origin){
    		imp_alt.show();
        }
        */
        // which piar to compare
        // which pair to compare
		int [] gpu_pair = {1,2};
        
		float [][] gpu_pair_img = new  float [2][];