Commit in CDMS/src/CDMS/ImageNavigatorII on MAIN
Stitcher.java+94-471.7 -> 1.8
Now reads from an input file, implements flat field averaging and is much faster.

CDMS/src/CDMS/ImageNavigatorII
Stitcher.java 1.7 -> 1.8
diff -u -r1.7 -r1.8
--- Stitcher.java	3 Aug 2010 22:18:16 -0000	1.7
+++ Stitcher.java	7 Aug 2010 18:29:20 -0000	1.8
@@ -6,8 +6,9 @@
 import java.util.*;
 
 /*
- * Looking in the $imagePath given, this program takes the autorun.txt file and the images of 
- * the form $imageFileName (replace the counting element with "#"), and stitches them 
+ * Looking in the $imagePath given by the input.txt file, this program takes the 
+ * autorun.txt file and the images of 
+ * the form $imageFileName (replace the counting element with "%"), and stitches them 
  * together into larger images.  Because an image containing all the stitched images 
  * would be far too large for most computers to handle, these are saved in images of 
  * width and height $tileW and $tileH, labeled "row_col.png."  It is recommended that 
@@ -17,6 +18,7 @@
  * File Structure:
  * 	>Image Set Folder
  * 		>info.txt [file created by Stitcher to transfer data to Tiler]
+ * 		>input.txt [lots of parameters you can set]
  * 		>gmapviewer.htm	[the website navigator file!]
  * 		>resources	[do not touch these files, they are used by the Google API for the navigator]
  * 		>stitched_images	[stitched images will be saved here]
@@ -28,16 +30,24 @@
 public class Stitcher {
     public static void main(String[] args) throws IOException {
     	Date startDate = new Date();
+    	//read input.txt and get values from args
+    	String[] inFile = getLines("./input.txt","\n");
+    	String imagePath = "/"+getArg(inFile, "image_path");
+    	String savePath = "/"+getArg(inFile, "save_path");
+    	String autorunFile = getArg(inFile, "autorun_file");
+    	String imageFileName = "/"+getArg(inFile, "image_name");
+    	double theta = 0; try {theta = Double.parseDouble(getArg(inFile,"theta")); } catch (Exception e) {}
+    	double pxSize = 0.001397; try {pxSize = Double.parseDouble(getArg(inFile,"pixel_size")); } catch (Exception e) {}
+    	String[] blanksList = getArg(inFile, "blanks").split(",");
+    	int[] blanks = new int[blanksList.length];
+    	for (int i=0; i<blanksList.length; i++) {
+    		blanks[i] = Integer.parseInt(blanksList[i]);
+    	}
+    	int tileW = 2048; try{tileW = Integer.parseInt(getArg(inFile, "tile_width"));} catch (Exception e) {}
+    	int tileH = 2048; try{tileH = Integer.parseInt(getArg(inFile, "tile_height"));} catch (Exception e) {}
+
 		//read in the file and create a list of command lines (split by SET_PART_REPEAT)(dont use arg 0)
-	    String imagePath = "/nfs/farm/g/ee/u15/cdms/CDMS_ImageNavigator/IZipG47_Side1_TES/";
-    	String savePath = "/nfs/farm/g/ee/u15/cdms/CDMS_ImageNavigatorII/IZipG47_Side1_TES/";
-	    String autorunFile = "Autorun_IZipG47_Side1_TES_29July10.txt";
-    	String imageFileName = "/"+"IZipG47_Side1_TES-#.png";
-        double theta = Math.atan(2/640);
-	    double pxSize = 1.397/1000;	//x75.3 magnification: 5.452um | x293 magnification: 1.397um
-	    int[] blanks = {4032,4040,7852,895,8313};
-    	
-		String[] lines = getLines(imagePath+"/"+autorunFile);
+		String[] lines = getLines(imagePath+"/"+autorunFile,"SET_PART_REPEAT");
 	    
 	    //split the arguments of each line into a list of arguments: number of repeats in row, corrected start X value
 	    //also, declares some relevant variables: minimum start x, pixel size, maximum number of repeats
@@ -61,11 +71,9 @@
 	    
 	    //declare variables 
 	    /*********************/
-    	BufferedImage image = ImageIO.read(new File(  imagePath+"/"+imageFileName.replace("#","1")  ));
+    	BufferedImage image = ImageIO.read(new File(  imagePath+"/"+imageFileName.replace("%","1")  ));
         int imgW = image.getWidth();
         int imgH = image.getHeight();
-        int tileW = 2048;
-        int tileH = 2048;
         double dX = moveX.get(0)/pxSize;
         double dY = (ymins.get(0)-ymins.get(1))/pxSize;
         //find size of total stitched image
@@ -74,30 +82,43 @@
 	    /*********************/
 	    
 	    //make flat field matrix averaging matrix
-	    int[][][] ff = new int[imgW][imgH][3];
+	    System.out.println("Creating ff image...");
+	    double[][][] ff = new double[imgW][imgH][3];
 	    int rTot=0, gTot=0, bTot=0;
-	    for (int x=0; x<imgW; x++) {
-	    	for (int y=0; y<imgW; y++) {
-	    		int r=0, g=0, b=0;
-		    	for (int i=0;i<blanks.length; i++) {
-		    		image = ImageIO.read(new File(imagePath+"/"+imageFileName.replace("#",""+blanks[i])));
-    	            WritableRaster raster = image.getRaster();
-    	            r += raster.getSample(x, y, 0);
-    	            g += raster.getSample(x, y, 1);
-    	            b += raster.getSample(x, y, 2);
-		    	}
-		    	rTot += r;
-		    	gTot += g;
-		    	bTot += b;
-		    	ff[x][y][0] = Math.round(r/blanks.length);
-		    	ff[x][y][1] = Math.round(g/blanks.length);
-		    	ff[x][y][2] = Math.round(b/blanks.length);
+    	for (int i=0;i<blanks.length; i++) {
+			image = ImageIO.read(new File(imagePath+"/"+imageFileName.replace("%",""+blanks[i])));
+		    for (int x=0; x<imgW; x++) {
+		    	for (int y=0; y<imgH; y++) {
+		    		double r = ff[x][y][0];
+		    		double g = ff[x][y][1];
+		    		double b = ff[x][y][2];
+		    		double rAdd = image.getRaster().getSample(x, y, 0);
+		    		double gAdd = image.getRaster().getSample(x, y, 1);
+		    		double bAdd = image.getRaster().getSample(x, y, 2);
+	    	        r += rAdd/blanks.length;
+    	            g += gAdd/blanks.length;
+    	            b += bAdd/blanks.length;
+			    	rTot += rAdd;
+			    	gTot += gAdd;
+			    	bTot += bAdd;
+			    	ff[x][y][0] = r;
+			    	ff[x][y][1] = g;
+			    	ff[x][y][2] = b;
+			    }
 		    }
-	    }
+    	}
 	    double rAvg = rTot/(imgW*imgH*blanks.length);
 	    double gAvg = gTot/(imgW*imgH*blanks.length);
 	    double bAvg = bTot/(imgW*imgH*blanks.length);
-	    
+	    for (int x=0; x<imgW; x++) {
+	    	for (int y=0; y<imgH; y++) {
+	    		ff[x][y][0] = ff[x][y][0]/rAvg;
+	    		ff[x][y][1] = ff[x][y][1]/gAvg;
+	    		ff[x][y][2] = ff[x][y][2]/bAvg;
+	    	}
+	    }
+	    System.out.println("ff image file created...");
+    	
 	    //loop through all large tiles of stitched images
         for (int row=0;row<Math.ceil(totalH/tileH); row++) {
         	for (int col=0;col<Math.ceil(totalW/tileW); col++) {
@@ -124,8 +145,7 @@
         	    		lastImage++;
         	    		//if it is in the tile, make a square with the image tilted appropriately
         	        	if (drawX>minTileRenderX && drawX<maxTileRenderX && drawY>minTileRenderY && drawY<maxTileRenderY) {
-    	        			image = ImageIO.read(new File(imagePath+"/"+imageFileName.replace("#",""+lastImage)));
-            	            
+    	        			image = ImageIO.read(new File(imagePath+"/"+imageFileName.replace("%",""+lastImage)));
             	            WritableRaster imgRaster = image.getRaster();
 	            	        
             	            //write the image to toStore, averaging with any values already there (for each cell write to pixel)
@@ -138,14 +158,14 @@
 		            	                double sumG = toStoreRaster.getSample(drawXtoStore, drawYtoStore, 1);
 		            	                double sumB = toStoreRaster.getSample(drawXtoStore, drawYtoStore, 2);
 		            	                int z = layers[drawXtoStore][drawYtoStore];
-		            	                sumR = (z*sumR + imgRaster.getSample(x, y, 0))/(z+1);
-		            	                sumG = (z*sumG + imgRaster.getSample(x, y, 1))/(z+1);
-		            	                sumB = (z*sumB + imgRaster.getSample(x, y, 2))/(z+1);
+		            	                sumR = (z*sumR + imgRaster.getSample(x, y, 0)/ff[x][y][0])/(z+1);
+		            	                sumG = (z*sumG + imgRaster.getSample(x, y, 1)/ff[x][y][1])/(z+1);
+		            	                sumB = (z*sumB + imgRaster.getSample(x, y, 2)/ff[x][y][2])/(z+1);
 		            	                layers[drawXtoStore][drawYtoStore]++;
 		            	                if(sumR!=0 || sumG!=0 || sumB!=0) draw=true;
-	            	            		toStoreRaster.setSample(drawXtoStore, drawYtoStore, 0, sumR*rAvg/ff[x][y][0]);
-	            	            		toStoreRaster.setSample(drawXtoStore, drawYtoStore, 1, sumG*gAvg/ff[x][y][1]);
-	            	            		toStoreRaster.setSample(drawXtoStore, drawYtoStore, 2, sumB*bAvg/ff[x][y][2]);
+	            	            		toStoreRaster.setSample(drawXtoStore, drawYtoStore, 0, sumR);
+	            	            		toStoreRaster.setSample(drawXtoStore, drawYtoStore, 1, sumG);
+	            	            		toStoreRaster.setSample(drawXtoStore, drawYtoStore, 2, sumB);
 	            	                }
             	            	}
             	            }
@@ -161,14 +181,41 @@
     	System.out.println("\nRuntime: "+(endDate.getTime()-startDate.getTime())/1000/60);
     	//writes data needed for Tiler.java to info.txt
     	try {
-    	    BufferedWriter out = new BufferedWriter(new FileWriter(savePath+"/info.txt"));
-    	    out.write(Math.ceil(totalW/tileW)+"\n"+Math.ceil(totalH/tileH)+"\n"+tileW+"\n"+tileH+"\n"+savePath);
-    	    out.close();
+    	    BufferedWriter outprint = new BufferedWriter(new FileWriter(savePath+"/info.txt"));
+    	    outprint.write(Math.ceil(totalW/tileW)+"\n"+Math.ceil(totalH/tileH)+"\n"+tileW+"\n"+tileH+"\n"+savePath);
+    	    outprint.close();
     	} catch (IOException e) {System.out.println("Could not create info.txt:\n"+e);}
     }
     
+    //takes an input of lines, searches through them for the desired argument name, and returns its value
+    public static String getArg(String[] lines, String argument) {
+    	String arg = null;
+    	for (int line=0; line<lines.length; line++) {
+    		String[] lineText = lines[line].replaceAll(" ", "").split("\t");
+    		int i=0;
+    		boolean next = false;
+    		while (true) {
+    			String token = "";
+    			try {
+    				token = lineText[i];
+    			} catch (Exception e) { break; }
+    			if (token.indexOf('#')!=-1) {
+    				token = token.substring(0,token.indexOf('#'));
+    				break;
+    			}
+    			if (token.equals( argument) ) next = true;
+    			else if (!token.equals("") && next==true) {
+    				arg = token;
+    				break;
+    			}
+    			i++;
+    		}
+    	}
+    	return arg;
+    }
+    
     //takes an input path and returns a list of values split by the SET_PART-REPEAT
-    public static String[] getLines(String path) {
+    public static String[] getLines(String path, String splitPhrase) {
     	try {
 	    	BufferedReader in = new BufferedReader(new FileReader(path));
 		    String str;
@@ -177,9 +224,9 @@
 		        text += "\n"+str;
 		    }
 		    in.close();
-		    String[] lines = text.split("SET_PART_REPEAT");
+		    String[] lines = text.split(splitPhrase);
 		    return lines;
-    	} catch (IOException e) {System.out.println("File could not be read"); return null;}
+    	} catch (IOException e) {System.out.println("File could not be read:\n"+e); return null;}
     }
     
 }
CVSspam 0.2.8