Object Detection using Template

- Main Page
- Packages
- Classes
- Files

- File List

All Classes Namespaces Files Functions Variables
DetectorImpl.java
Go to the documentation of this file.
1 package cz.vutbr.feec.novak68.detector;
2
3 import ij.ImagePlus;
4 import ij.gui.Roi;
5
6 import java.awt.Color;
7 import java.awt.Rectangle;
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.EmptyStackException;
12 import java.util.List;
13
14 import com.rapidminer.example.Example;
15 import com.rapidminer.example.ExampleSet;
16
17 import cz.vutbr.feec.novak68.imageProcessor.ImageFacade;
18 import cz.vutbr.feec.novak68.rapidminer.MyProcess;
19 import cz.vutbr.feec.novak68.tools.Coordinate;
20 import cz.vutbr.feec.novak68.tools.VarianceComputer;
21
27 public class DetectorImpl implements Detector {
28 MyProcess process;
29 VarianceComputer var = new VarianceComputer();
30 private ImageFacade facade;
31 private double threshold = 0.745;
32 private double thresholdN = 0.20;
33 private boolean useThreshold = true; //Zapnout/vypnout threshold
34 private boolean useThresholdNegative = true; //Zapnout/vypnout threshold
35 private boolean useVariance = false;
36 // private final double scale = 3;
37 private double step = 0.2;
38 private double zoom = 0.60; //poměr výšky detekčního okna : testovacímu obrázku
39
40
47 public DetectorImpl (double threshold, double thresholdN, boolean useThreshold, boolean useThresholdNegative, boolean useVariance, double step, double zoom){
48 this.threshold = threshold;
49 this.thresholdN = thresholdN;
50 this.useThreshold = useThreshold;
51 this.useThresholdNegative = useThresholdNegative;
52 this.step = step;
53 this.zoom = zoom;
54 this.useVariance = useVariance;
55 }
56
57 @Override
58 public void detect(ImagePlus sourceImage) throws IOException {
59 process = new MyProcess(useVariance);
60
61 //width, cellsizeX, cellSizeY, numOfBins, threshold,filewriter
62 facade = new ImageFacade(sourceImage,zoom,step);
63 facade.extractAll(process, useVariance);
64 try{
65 process.runProcess();
66 }
67 catch(EmptyStackException e){
68 return;
69 }
70 ExampleSet es = process.getOutputExampleSet();
71 List<Coordinate> coordinates = new ArrayList<Coordinate>();
72 List<Coordinate> coordinatesN = new ArrayList<Coordinate>();
73 for (Example example : es) {
74 if (example.getValueAsString(es.getAttributes().getPredictedLabel()).equals("p") && (
75 (example.getValue(es.getAttributes().getConfidence("p")) >= (threshold)) || !useThreshold) ){
76 String[] strCoordinates = (example.getValueAsString(es.getAttributes().getRegular("coordinates")).split("_"));
77 Coordinate coordinate = new Coordinate(Integer.parseInt(strCoordinates[0]), Integer.parseInt(strCoordinates[1]),
78 Double.parseDouble(strCoordinates[2]));
79 // if (var.variance(coordinate,sourceImage) > 1660)
80 coordinates.add(coordinate);
81 }
82 if (example.getValueAsString(es.getAttributes().getPredictedLabel()).equals("n") && (
83 (example.getValue(es.getAttributes().getConfidence("n")) <= (thresholdN)) && useThresholdNegative) ){
84 String[] strCoordinates = (example.getValueAsString(es.getAttributes().getRegular("coordinates")).split("_"));
85
86 coordinatesN.add(new Coordinate(Integer.parseInt(strCoordinates[0]), Integer.parseInt(strCoordinates[1]),
87 Double.parseDouble(strCoordinates[2])));
88 }
89 }
90
91
92 for (Coordinate coordinate : coordinates) {
93 drawCross(sourceImage, coordinate,Color.YELLOW);
94 drawROI(sourceImage,coordinate,Color.RED);
95 }
96 for (Coordinate coordinate : coordinatesN) {
97 drawCross(sourceImage, coordinate,Color.WHITE);
98 drawROI(sourceImage,coordinate,Color.GREEN);
99 }
100
101 drawnumber(sourceImage, coordinates.size(), coordinatesN.size());
102 sourceImage.killRoi();
103
104 File dir = new File("data/detected/variance/" + String.valueOf(threshold).replace('.', '_'));
105 if (!dir.exists()){
106 dir.mkdirs();
107 }
108
109 // FileSaver saver = new FileSaver(sourceImage);
110 // if (sourceImage.getTitle().split("\\.")[1].equals("jpg")){
111 // saver.saveAsJpeg(dir.getPath() + "/" + sourceImage.getTitle());
112 // }
113 // else
114 // saver.saveAsPng(dir.getPath() + "/" + sourceImage.getTitle());
115 sourceImage.show();
116 }
117
123 private static void drawCross (ImagePlus sourceImage, Coordinate coordinate, Color color){
124 sourceImage.getProcessor().setColor(color);
125 sourceImage.getProcessor().setLineWidth(3);
126 sourceImage.getProcessor().drawLine(coordinate.center.x - 10, coordinate.center.y, coordinate.center.x + 10, coordinate.center.y);
127 sourceImage.getProcessor().drawLine(coordinate.center.x, coordinate.center.y - 10, coordinate.center.x , coordinate.center.y + 10);
128 }
134 private static void drawROI (ImagePlus sourceImage, Coordinate coordinate, Color color){
135 sourceImage.getProcessor().setColor(color);
136 sourceImage.getProcessor().setLineWidth(3);
137 sourceImage.getProcessor().draw(new Roi(new Rectangle(coordinate.leftTop, coordinate.dimension)));
138 }
144 private static void drawnumber(ImagePlus sourceImage, int positiveCount, int negativePcount){
145 float myint = sourceImage.getProcessor().getPixelValue(10, sourceImage.getHeight() - 10);
146 if (sourceImage.getProcessor().getPixelValue(10, sourceImage.getHeight() - 10) > 100 )
147 sourceImage.getProcessor().setColor(Color.BLUE);
148 else
149 sourceImage.getProcessor().setColor(Color.YELLOW);
150 sourceImage.getProcessor().drawString(String.format("Positive detected = %d", positiveCount + negativePcount), 10, sourceImage.getHeight() - 10);
151 }
152
153 }
cz.vutbr.feec.novak68.tools.Coordinate.center
final Point center
Definition: Coordinate.java:18
cz.vutbr.feec.novak68.tools.Coordinate.dimension
final Dimension dimension
Definition: Coordinate.java:19
cz.vutbr.feec.novak68.detector.DetectorImpl.detect
void detect(ImagePlus sourceImage)
Definition: DetectorImpl.java:58
cz.vutbr.feec.novak68.detector.DetectorImpl.facade
ImageFacade facade
Definition: DetectorImpl.java:30
cz.vutbr.feec.novak68.detector.Detector
Definition: Detector.java:12
cz.vutbr.feec.novak68.detector.DetectorImpl.threshold
double threshold
Definition: DetectorImpl.java:31
cz.vutbr.feec.novak68.rapidminer.MyProcess
Definition: MyProcess.java:30
cz.vutbr.feec.novak68.tools.Coordinate
Definition: Coordinate.java:15
cz.vutbr.feec.novak68.detector.DetectorImpl.useThreshold
boolean useThreshold
Definition: DetectorImpl.java:33
cz.vutbr.feec.novak68.tools.Coordinate.leftTop
final Point leftTop
Definition: Coordinate.java:16
cz.vutbr.feec.novak68.detector.DetectorImpl.step
double step
Definition: DetectorImpl.java:37
cz.vutbr.feec.novak68.detector.DetectorImpl.useThresholdNegative
boolean useThresholdNegative
Definition: DetectorImpl.java:34
cz.vutbr.feec.novak68.detector.DetectorImpl.DetectorImpl
DetectorImpl(double threshold, double thresholdN, boolean useThreshold, boolean useThresholdNegative, boolean useVariance, double step, double zoom)
Definition: DetectorImpl.java:47
cz.vutbr.feec.novak68.tools.VarianceComputer
Definition: VarianceComputer.java:9
cz.vutbr.feec.novak68.detector.DetectorImpl.thresholdN
double thresholdN
Definition: DetectorImpl.java:32
cz.vutbr.feec.novak68.detector.DetectorImpl.zoom
double zoom
Definition: DetectorImpl.java:38
cz.vutbr.feec.novak68.detector.DetectorImpl.drawROI
static void drawROI(ImagePlus sourceImage, Coordinate coordinate, Color color)
Definition: DetectorImpl.java:134
cz.vutbr.feec.novak68.imageProcessor.ImageFacade
Definition: ImageFacade.java:21
cz.vutbr.feec.novak68.detector.DetectorImpl.drawnumber
static void drawnumber(ImagePlus sourceImage, int positiveCount, int negativePcount)
Definition: DetectorImpl.java:144
cz.vutbr.feec.novak68.detector.DetectorImpl.drawCross
static void drawCross(ImagePlus sourceImage, Coordinate coordinate, Color color)
Definition: DetectorImpl.java:123
cz.vutbr.feec.novak68.detector.DetectorImpl
Definition: DetectorImpl.java:27
cz.vutbr.feec.novak68.detector.DetectorImpl.useVariance
boolean useVariance
Definition: DetectorImpl.java:35