Skip to content

Commit

Permalink
- Updated binary fiducial generator application to use new code
Browse files Browse the repository at this point in the history
- abstracted generic code even more
  • Loading branch information
lessthanoptimal committed Jun 29, 2015
1 parent 866cc9a commit 7880ecb
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 105 deletions.
38 changes: 25 additions & 13 deletions applications/src/boofcv/app/BaseFiducialSquareEPS.java
Expand Up @@ -309,8 +309,19 @@ private void autoSelectGridSize(double fiducialWidthUnit, double whiteBorderUnit
* @param documentTitle Title of the document
*/
private void generateDocument(double fiducialWidthUnit, String documentTitle) {
printHeader(documentTitle,fiducialWidthUnit);
printPatternDefinitions(fiducialWidthUnit);
printHeader(documentTitle, fiducialWidthUnit);
printPatternDefinitions();

if (printInfo) {
for (int i = 0; i < totalPatterns(); i++) {
String patternName = getPatternName(i);
out.print(" /" + getDisplayDef(i) + "\n" +
"{\n" +
" /Times-Roman findfont\n" + "7 scalefont setfont b1 " + (fiducialTotalWidth - 10) +
" moveto (" + patternName + " " + fiducialWidthUnit + " " + unit.abbreviation + ") show\n" +
"} def\n");
}
}

// draws the black border around the fiducial
out.print(" /drawBorder\n"+
Expand All @@ -335,14 +346,10 @@ private void generateDocument(double fiducialWidthUnit, String documentTitle) {
}

/**
* Define EPS functions for each pattern:
* {@link #getImageName} -> renders the pattern
* {@link #getDisplayName} -> displays information above fiducial
*
*
* @param fiducialWidthUnit Size of the fiducial
* Creates definitions which will render the pattern. Each pattern's difintion will have the name returned
* by {@link #getPatternPrintDef(int)}
*/
protected abstract void printPatternDefinitions(double fiducialWidthUnit);
protected abstract void printPatternDefinitions();

/**
* Returns the total number of unqiue patterns
Expand All @@ -351,11 +358,16 @@ private void generateDocument(double fiducialWidthUnit, String documentTitle) {

protected abstract void addPattern( String name );

protected String getImageName( int num ) {
/**
* Human readable pattern name. Will be printed on document
*/
protected abstract String getPatternName( int num );

protected String getPatternPrintDef(int num) {
return String.format("drawImage%03d",num);
}

protected String getDisplayName( int num ) {
protected String getDisplayDef(int num) {
return String.format("displayInfo%03d",num);
}

Expand Down Expand Up @@ -410,12 +422,12 @@ private void insertFiducial(int row, int col ) {

// print out encoding information for convenience
if(printInfo) {
out.println(" " + getDisplayName(imageNum));
out.println(" " + getDisplayDef(imageNum));
}

out.println("% Center then draw the image");
out.println(" b1 b1 translate");
out.println(" "+getImageName(imageNum));
out.println(" "+getPatternPrintDef(imageNum));
out.println("% Undo translations");
out.println(" -1 b1 mul -1 b1 mul translate");
out.println(" -1 originX mul -1 originY mul translate");
Expand Down
23 changes: 17 additions & 6 deletions applications/src/boofcv/app/CommandParserFiducialSquare.java
Expand Up @@ -46,11 +46,17 @@ public class CommandParserFiducialSquare {

public List<String> patternNames = new ArrayList<String>();

public List<String> exampleNames = new ArrayList<String>();

public CommandParserFiducialSquare(String nameOfPatterns) {
this.nameOfPatterns = nameOfPatterns;
}

public void printHelp() {

String n0 = exampleNames.get(0);
String n1 = exampleNames.get(1);

System.out.println("./application <optional flags> <fiducial width> <"+nameOfPatterns+" 0> ... <"+nameOfPatterns+" N-1>");
System.out.println();
System.out.println("Optional Flags");
Expand All @@ -69,12 +75,12 @@ public void printHelp() {
System.out.println(" example: -PageSize=letter");
System.out.println();
System.out.println("Examples:");
System.out.println("./application -PrintInfo -OutputFile=fiducial_ke.eps 10 ke.png");
System.out.println(" 10cm fiducial using 'ke.png' as the pattern with it's size and info");
System.out.println("./application -Grid=fill -Units=inch -PageSize=letter 2.5 ke.png");
System.out.println(" 2.5 inch fiducial, filling letter sized paper with grid, 'ke.png' as the pattern");
System.out.println("./application -Grid=fill -Units=inch -PageSize=letter 2.5 ke.png dog.png");
System.out.println(" same as the previous, but alternates between ke and dog patterns");
System.out.println("./application -PrintInfo -OutputFile=fiducial.eps 10 "+n0);
System.out.println(" 10cm fiducial using '"+n0+"' as the pattern with it's size and info");
System.out.println("./application -Grid=fill -Units=inch -PageSize=letter 2.5 "+n0);
System.out.println(" 2.5 inch fiducial, filling letter sized paper with grid, '"+n0+"' as the pattern");
System.out.println("./application -Grid=fill -Units=inch -PageSize=letter 2.5 "+n0+" "+n1);
System.out.println(" same as the previous, but alternates between "+n0+" and "+n1+" patterns");
}

public int parseFlags( String []args) {
Expand Down Expand Up @@ -225,4 +231,9 @@ public static String[] split( String arg ) {
}
throw new IllegalArgumentException("Couldn't find ',' in "+arg);
}

public void setExampleNames( String name0 , String name1 ) {
exampleNames.add(name0);
exampleNames.add(name1);
}
}
145 changes: 68 additions & 77 deletions applications/src/boofcv/app/CreateFiducialSquareBinaryEPS.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2014, Peter Abeles. All Rights Reserved.
* Copyright (c) 2011-2015, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand All @@ -18,20 +18,77 @@

package boofcv.app;

import java.io.FileNotFoundException;
import org.ddogleg.struct.GrowQueue_I32;

import java.io.IOException;
import java.io.PrintStream;

/**
* Outputs an EPS document describing a binary square fiducial that encodes the specified number
*
* @author Peter Abeles
*/
public class CreateFiducialSquareBinaryEPS {
public class CreateFiducialSquareBinaryEPS extends BaseFiducialSquareEPS {

// list of the fiducial ID's it will print
GrowQueue_I32 numbers = new GrowQueue_I32();

@Override
protected void printPatternDefinitions() {

out.print(
" /sl "+(innerWidth/4)+" def\n" +
" /w0 0 def\n" +
" /w1 { w0 sl add} def\n" +
" /w2 { w1 sl add} def\n" +
" /w3 { w2 sl add} def\n");
out.print(" /box {newpath moveto sl 0 rlineto 0 sl rlineto sl neg 0 rlineto closepath fill} def\n");

for( int i = 0; i < numbers.size(); i++ ) {
int patternNumber = numbers.get(i);

out.print(" /"+getPatternPrintDef(i)+" {\n"+
"% Block corner used to identify orientation\n" +
" 0 0 box\n");
for (int j = 0; j < 12; j++) {
if( (patternNumber & (1<<j)) != 0 ) {
box(out,j);
}
}
out.print("} def\n");
}
}

@Override
protected int totalPatterns() {
return numbers.size();
}

@Override
protected void addPattern(String name) {
numbers.add( Integer.parseInt(name));
}

@Override
protected String getPatternName(int num) {
return ""+numbers.get(num);
}

public static int number = 284;
public static double width = 10;
@Override
public String defaultOutputFileName() {
if( numbers.size() == 1 )
return "Fiducial"+numbers.get(0)+".eps";
else
return "BinaryFiducials.eps";
}

public static double CM_TO_POINTS = 72.0/2.54;
@Override
public String selectEpsName() {
if( numbers.size() == 1 )
return ""+numbers.get(0);
else
return numbers.get(0)+" and more";
}

private static void box( PrintStream out , int bit ) {
if( bit < 2 )
Expand All @@ -52,79 +109,13 @@ else if( bit < 12 )
out.print(" "+wx+" "+wy+" box\n");
}

public static void main(String[] args) throws FileNotFoundException {
public static void main(String[] args) throws IOException {

if( args.length == 2 ) {
width = Double.parseDouble(args[0]);
number = Integer.parseInt(args[1]);
}
number &= 0x0FFF;
CommandParserFiducialSquare parser = new CommandParserFiducialSquare("number");

System.out.println("Target width "+width+" (cm) number = "+number);
String fileName = String.format("square%04d.eps",number);
parser.setExampleNames("284","845");
parser.execute(args,new CreateFiducialSquareBinaryEPS());
}

// print out the selected number in binary for debugging purposes
for (int i = 0; i < 12; i++) {
if( (number & (1<<i)) != 0 ) {
System.out.print("1");
} else {
System.out.print("0");
}
}
System.out.println();

PrintStream out = new PrintStream(fileName);

double targetLength = width*CM_TO_POINTS;
double whiteBorder = targetLength/4.0;
double blackBorder = targetLength/4.0;
double innerWidth = targetLength/2.0;
double squareLength = innerWidth/4;
double pageLength = targetLength+whiteBorder*2;

out.println("%!PS-Adobe-3.0 EPSF-3.0\n" +
"%%Creator: BoofCV\n" +
"%%Title: Binary Fiducial #"+number+" w="+width+"cm\n" +
"%%DocumentData: Clean7Bit\n" +
"%%Origin: 0 0\n" +
// "%%BoundingBox: xmin ymin xmax ymax\n" +
"%%BoundingBox: 0 0 "+pageLength+" "+pageLength+"\n" +
"%%LanguageLevel: 3\n" +
"%%Pages: 1\n" +
"%%Page: 1 1\n" +
" /sl "+squareLength+" def\n" +
" /pl "+pageLength+" def\n" +
" /wb "+whiteBorder+" def\n" +
" /bb "+blackBorder+" def\n" +
" /b0 "+whiteBorder+" def\n" +
" /b1 { wb bb add} def\n" +
" /b2 { b1 "+innerWidth+" add} def\n" +
" /b3 { b2 bb add} def\n" +
" /w0 b1 def\n" +
" /w1 { w0 sl add} def\n" +
" /w2 { w1 sl add} def\n" +
" /w3 { w2 sl add} def\n" +
// " /pagewidth "+targetLength+" def\n" +
" /box {newpath moveto sl 0 rlineto 0 sl rlineto sl neg 0 rlineto closepath fill} def\n" +
"% bottom top left right borders..\n" +
" newpath b0 b0 moveto b0 b3 lineto b1 b3 lineto b1 b0 lineto closepath fill\n" +
" newpath b1 b2 moveto b1 b3 lineto b3 b3 lineto b3 b2 lineto closepath fill\n" +
" newpath b1 b0 moveto b1 b1 lineto b3 b1 lineto b2 b0 lineto closepath fill\n" +
" newpath b2 b0 moveto b2 b3 lineto b3 b3 lineto b3 b0 lineto closepath fill\n" +
"% Block corner used to identify orientation\n" +
" b1 b1 box\n" +
"% information bits\n");

for (int i = 0; i < 12; i++) {
if( (number & (1<<i)) != 0 ) {
box(out,i);
}
}
// print out encoding information for convenience
out.print(" /Times-Roman findfont\n" +
"7 scalefont setfont w0 "+(pageLength-10)+" moveto (# "+number+" "+width+" cm) show\n");
out.print(" showpage\n" +
"%%EOF\n");

}
}
18 changes: 9 additions & 9 deletions applications/src/boofcv/app/CreateFiducialSquareImageEPS.java
Expand Up @@ -41,7 +41,7 @@ public class CreateFiducialSquareImageEPS extends BaseFiducialSquareEPS {
List<String> imagePaths = new ArrayList<String>();

@Override
protected void printPatternDefinitions(double fiducialWidthUnit) {
protected void printPatternDefinitions() {
for( int i = 0; i < imagePaths.size(); i++ ) {
String imageName = new File(imagePaths.get(i)).getName();
ImageUInt8 image = UtilImageIO.loadImage(imagePaths.get(i), ImageUInt8.class);
Expand All @@ -67,18 +67,11 @@ protected void printPatternDefinitions(double fiducialWidthUnit) {
ShowImages.showWindow(VisualizeBinaryData.renderBinary(binary, false, null), "Binary Image");

out.println();
out.print(" /"+getImageName(i)+" {\n" +
out.print(" /"+getPatternPrintDef(i)+" {\n" +
" "+binary.width+" " + binary.height + " 1 [" + scale + " 0 0 " + scale + " 0 0]\n" +
" {<"+binaryToHex(binary)+">} image\n" +
"} def\n");
out.println();
if(printInfo) {
out.print(" /"+getDisplayName(i)+"\n" +
"{\n" +
" /Times-Roman findfont\n" + "7 scalefont setfont b1 " + (fiducialTotalWidth - 10) +
" moveto (" + imageName + " " + fiducialWidthUnit + " "+unit.abbreviation+") show\n"+
"} def\n" );
}
}
}

Expand All @@ -92,6 +85,12 @@ protected void addPattern(String name) {
this.imagePaths.add(name);
}

@Override
protected String getPatternName(int num) {
String n = new File(imagePaths.get(num)).getName();
return n.substring(0,n.length()-4);
}

@Override
public String defaultOutputFileName() {
String inputPath = imagePaths.get(0);
Expand Down Expand Up @@ -119,6 +118,7 @@ public static void main(String[] args) throws IOException {

CommandParserFiducialSquare parser = new CommandParserFiducialSquare("image path");

parser.setExampleNames("ke.png","dog.png");
parser.execute(args,new CreateFiducialSquareImageEPS());
}
}

0 comments on commit 7880ecb

Please sign in to comment.