Commit in CDMS/src/CDMS/swiatlow/ImageMapperII on MAIN
ImageMapperII.java+193added 1.1
ImageMapperIIRun.java+22added 1.1
+215
2 added files
First version of ImageMapperII. New software that takes in a .jpg (or any common image format) of a mask, and outputs a .txt file of commands for the OGP imager to image the "interesting" regions of the mask. Interesting means that we'll never take an image of a blank area (unless it's on purpose, for field leveling, etc).

Note that reading in a high-res image (currently using 200 px/ inch when importing from the pdf of the mask) requires a large (~750 mb+) heap size for running. -Xmx1024mb as a flag on the execution of the program will fix this problem. (Obviously this means it will only run on machines with >> 1 g of memory)

CDMS/src/CDMS/swiatlow/ImageMapperII
ImageMapperII.java added at 1.1
diff -N ImageMapperII.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ImageMapperII.java	24 Feb 2011 06:03:18 -0000	1.1
@@ -0,0 +1,193 @@
+package CDMS.swiatlow.ImageMapperII;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Formatter;
+
+import javax.imageio.ImageIO;
+
+import java.awt.Color;
+import java.awt.image.*;
+
+public class ImageMapperII {
+	
+	private static final int INTERESTING_CUT = 1;
+	
+	private static final double pixWidth = 45.0;
+	private static final double pixHeight = 33.0;
+	
+	private static final double mmPerPixel = .00810282; // high res
+	
+	private double imageWidth;
+	private double imageHeight;
+	
+	private int nPixWidth;
+	private int nPixHeight;
+	
+	private boolean[][] flag;
+	
+	private BufferedImage image;
+	
+	private BufferedWriter output;
+	
+	private int debug;
+	
+	public ImageMapperII(String imageFile, String outFile, int debugFlag) throws IOException{
+		//initialize variables, say hello
+		
+		System.out.println("Welcome to CDMS ImageMapperII!\nI'll create a image map file from the inputted" +
+				" image file you gave me: " + imageFile + "\nThe output will go to: " + outFile);
+		
+		debug = debugFlag;
+		
+		if(debug==1) System.out.println("Debug mode is on!");
+		
+		FileWriter fw = new FileWriter(outFile);
+        output = new BufferedWriter(fw);
+		
+		image = ImageIO.read(new File( imageFile ));
+		
+		imageWidth = image.getWidth();
+		imageHeight = image.getHeight();
+		
+		nPixWidth = (int) Math.ceil(imageWidth / pixWidth);
+		nPixHeight = (int) Math.ceil(imageHeight / pixHeight);
+		
+		flag = new boolean[nPixWidth][nPixHeight];
+		if(debug==1){
+			System.out.println("Length of flag boolean: " + flag.length);
+			System.out.println("Length of flag[0] boolean: " + flag[0].length);
+			System.out.println("nPixWidth = " + nPixWidth + " and nPixHeight = " + nPixHeight);
+		}
+		
+	}
+	
+	public void doAll() throws IOException{
+		writeFlags();
+		writeOut();
+	}
+	
+	private int[] getSubCoords(int i, int j){
+		// return the coordinates of the subimage we take as we grid
+		// normally just a box of size defined by the 620x460 camera pixel grid
+		// sometimes, at the edges, have to define subimages only up to the edges
+		int[] out = new int[2];
+		
+		if(i < nPixWidth-1 && j < nPixHeight -1){ // normal areas
+			out[0] = (int) pixWidth;
+			out[1] = (int) pixHeight;
+		}
+		else if(i < nPixWidth -1 && j == nPixHeight -1){ // at the bottom
+			out[0] = (int) pixWidth;
+			out[1] = (int) (imageHeight - pixHeight*j);
+		}
+		else if(i == nPixWidth-1 && j < nPixHeight -1){// at the right
+			out[0] = (int) (imageWidth - pixWidth*i);
+			out[1] = (int) pixHeight;
+		}
+		else{ // at the bottom right
+			if(debug==1) System.out.println("At the bottom right: should only be here once. i = " + i +" j = " + j);
+			out[0] = (int) (imageWidth - pixWidth*i);
+			out[1] = (int) (imageHeight - pixHeight*j);
+		}
+		return out;
+	}
+	
+	private void writeFlags(){
+		// loop over tiles and mark if they're interesting
+		for(int i = 0; i < nPixWidth; i++){
+			for(int j =0; j < nPixHeight; j++){
+				int[] boxCoords = getSubCoords(i,j);
+				BufferedImage sub = image.getSubimage((int) pixWidth*i, (int) pixHeight*j,
+						boxCoords[0], boxCoords[1]);
+				
+				if(isInteresting(sub))
+					flag[i][j] = true;
+				else
+					flag[i][j] = false;
+				
+			}//end for j
+		}// end for i
+	}
+	
+	private void writeOut() throws IOException{
+	    if(debug==0){
+        	output.write("BEGIN_RUN\n");
+        	output.write("MM\n");
+        	output.write("SET_DATUM\n");
+        	output.write("c:\\Partrtn\\IZipDatum_PeaceSide1_x2_5.RTN\n");
+        }
+		
+	    //loop over flag list; if set to true, then output
+		for(int i = 0; i < nPixWidth; i++){
+			for(int j =0; j < nPixHeight; j++){
+				if(flag[i][j]){
+					double[] converted = convertCoords(pixWidth*i, pixHeight*j);
+					
+					Formatter fmt1 = new Formatter();
+					Formatter fmt2 = new Formatter();
+					fmt1.format("%.6f", converted[0]);
+					fmt2.format("%.6f", converted[1]); 
+
+					if(debug==0) output.write("c:\\Partrtn\\IZipOriginTakePicture_x2_5.RTN,"+fmt1+","+fmt2+",0,0,0,0\n");
+					else if(debug==1) output.write(fmt1+","+fmt2+"\n");
+					
+					// with >13k lines, buffer gets filled; take small performance hit and flush every time
+					output.flush();
+				}//end if
+			}// end for j
+		}// end for i
+		
+        if(debug==0) output.write("END_RUN\n");
+        output.close();
+	}
+	
+	private double[] convertCoords(double xc, double yc){
+		
+		double halfOffsetX = pixWidth/2;
+		double halfOffsetY = pixHeight/2;
+		
+		//convert to center of image instead of corner
+		xc += halfOffsetX;
+		yc += halfOffsetY;
+		
+		// convert to 0,0 centered cartesian coordinate system from 0,0 in upper left
+		xc -= imageWidth/2;
+		yc = (yc-imageHeight/2)*(-1);
+		
+		// convert to mm from pixels
+		xc *= mmPerPixel;
+		yc *= mmPerPixel;
+		
+		
+		double[] out = new double[2];
+		out[0] = xc;
+		out[1] = yc;
+		
+		return out;
+		
+	}
+	
+	private boolean isInteresting(BufferedImage sub){
+		int subWidth = sub.getWidth();
+		int subHeight = sub.getHeight();
+		int nColorPix = 0;
+		
+		// if we see a non-white pixel, make note of it
+		for(int m = 0; m < subWidth; m++){
+			for(int n = 0; n < subHeight; n++){
+				int pix = sub.getRGB(m, n);
+				if(pix != Color.WHITE.getRGB())
+					nColorPix++;
+			}
+			
+		}
+
+		// if there are enough non-white pixels, it's interesting
+		if(nColorPix > INTERESTING_CUT)
+			return true;
+		return false;
+	}
+}

CDMS/src/CDMS/swiatlow/ImageMapperII
ImageMapperIIRun.java added at 1.1
diff -N ImageMapperIIRun.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ImageMapperIIRun.java	24 Feb 2011 06:03:18 -0000	1.1
@@ -0,0 +1,22 @@
+package CDMS.swiatlow.ImageMapperII;
+
+import java.io.IOException;
+
+public class ImageMapperIIRun {
+
+	/**
+	 * @param args
+	 */
+	public static void main(String[] args) {
+
+		try {
+			ImageMapperII mapper = new ImageMapperII("/Users/maxinion/Documents/Work/CDMS/imagingII/qets2-hires.jpg",
+					"/Users/maxinion/Documents/Work/CDMS/imagingII/out2-23/qets2-out.txt",0);
+			mapper.doAll();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		
+	}
+
+}
CVSspam 0.2.8