Skip to content

Commit 6ec80ff

Browse files
committed
Refactored ImageProcessor
1 parent 8213c5d commit 6ec80ff

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

ImageProcessor.java

+36-30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import javax.media.jai.JAI;
66

77
public class ImageProcessor{
8+
9+
ProcessList process;
810

911
/**
1012
*
@@ -16,43 +18,47 @@ public static void main(String args[]){
1618
System.exit(1);
1719
}
1820

19-
//Build up the processor
20-
ProcessList processor = new ProcessList();
21-
processor.add(new MorphologicalCloseProcess());
22-
processor.add(new GrayConvertProcess());
23-
processor.add(new MonoscaleProcess());
24-
SobelDetectionProcess sobel = new SobelDetectionProcess();
25-
processor.add(sobel);
26-
27-
//the third parameter is a placeholder until I can try something
28-
//different
29-
HoughTransformer trans = new HoughTransformer(200, .70, 100.0);
21+
ImageProcessor process = new ImageProcessor();
22+
3023

3124
for(int n = 0; n < args.length; n++){
32-
String inputFilename = args[n];
33-
String outputFilename = inputFilename.substring(0, inputFilename.lastIndexOf('.')) + "PR.jpg";
34-
RenderedImage inputImg;
35-
3625
try{
37-
inputImg = ImageIO.read(new File(inputFilename));
26+
process.categorizeImage(args[n]);
3827
}catch(IOException e){
39-
System.err.println("Error reading " + outputFilename + ": "
28+
System.err.println("Error reading " + args[n] + ": "
4029
+ e.getMessage());
41-
continue;
4230
}
43-
44-
RenderedImage outputImg = processor.process(inputImg);
45-
Category cat = trans.process(outputImg);
31+
}
32+
}
33+
34+
public ImageProcessor(){
35+
//build up the image process
36+
process = new ProcessList();
37+
process.add(new MorphologicalCloseProcess());
38+
process.add(new GrayConvertProcess());
39+
process.add(new MonoscaleProcess());
40+
SobelDetectionProcess sobel = new SobelDetectionProcess();
41+
process.add(sobel);
42+
}
43+
44+
public Category categorizeImage(String filename)
45+
throws IOException{
4646

47-
//if undefined, try processing again, without the Sobel operator
48-
if(cat == Category.UNDEFINED){
49-
processor.remove(sobel);
50-
outputImg = processor.process(inputImg);
51-
cat = trans.process(outputImg);
52-
processor.add(sobel);
53-
}
54-
55-
System.out.println("Input file " + inputFilename + ": " + cat);
47+
RenderedImage inputImg = ImageIO.read(new File(filename));
48+
49+
HoughTransformer trans = new HoughTransformer(250, .70, 100.0);
50+
RenderedImage outputImg = process.process(inputImg);
51+
Category cat = trans.process(outputImg);
52+
53+
//if undefined, try processing again, without the Sobel operator
54+
if(cat == Category.UNDEFINED){
55+
ImageProcess sobel = process.get(process.size() - 1);
56+
process.remove(process.size() - 1);
57+
cat = trans.process(process.process(inputImg));
58+
process.add(sobel);
5659
}
60+
61+
System.out.println("Input file " + filename + ": " + cat);
62+
return cat;
5763
}
5864
}

0 commit comments

Comments
 (0)