This commit is contained in:
Armin Wolf 2017-09-18 20:44:10 +02:00
parent e50c5b6a7c
commit a37c690000
9 changed files with 75 additions and 96 deletions

View File

@ -1,7 +1,11 @@
package model;
import sun.awt.image.ImageWatched;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
@ -12,7 +16,7 @@ import java.util.LinkedList;
*/
public class LineModel {
private LinkedList<Point> nodes;
private ArrayList<Point> nodes;
private LinkedList<Line> lines;
private Double xMinimum;
private Double xMaximum;
@ -20,7 +24,7 @@ public class LineModel {
private Double yMaximum;
public LineModel() {
nodes = new LinkedList<>();
nodes = new ArrayList<>();
lines = new LinkedList<>();
}
@ -52,10 +56,14 @@ public class LineModel {
this.lines.add(line);
}
public LinkedList<Point> getNodes() {
public ArrayList<Point> getNodes() {
return nodes;
}
public void setNodes(ArrayList<Point> nodes) {
this.nodes = nodes;
}
public LinkedList<Line> getLines() {
return lines;
}

View File

@ -1,16 +1,12 @@
package presenter;
import model.Interval;
import model.Line;
import model.LineModel;
import presenter.algorithms.util.IntersectionCounter;
import presenter.evaluation.EvaluateAlgorithms;
import presenter.evaluation.PictureProcessor;
import view.MainFrame;
import javax.swing.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
@ -156,31 +152,10 @@ public abstract class AbstractPresenter implements Observer {
public void setup() {
//Darstellung der Schnittpunkte in einer Tabelle
List<String> heading = new LinkedList<>();
List<List<String>> rows = new LinkedList<>();
heading.add("Geraden im Dualraum");
for (int j = 0; j < getModel().getLines().size() - 2; j++) {
LinkedList<String> rowEntry = new LinkedList<>();
Line p1 = getModel().getLines().get(j);
String sign = p1.getB() < 0 ? "" : "+";
rowEntry.add("f(x) = " +String.format("%.3f", p1.getM()) + "x "+ sign + String.format("%.3f",p1.getB()));
if (j + 1 < getModel().getLines().size()) {
Line p2 = getModel().getLines().get(j + 1);
sign = p2.getB() < 0 ? "" : "+";
rowEntry.add("f(x) = " +String.format("%.3f",p2.getM()) + "x" + sign + String.format("%.3f", p2.getB()));
}
if (j + 2 < getModel().getLines().size()) {
Line p3 = getModel().getLines().get(j + 2);
sign = p3.getB() < 0 ? "" : "+";
rowEntry.add("f(x) = " + String.format("%.3f",p3.getM()) + "x"+ sign + String.format("%.3f",p3.getB()));
}
rows.add(rowEntry);
}
getView().createTable(heading, rows);
getView().log("<hr>");
SwingUtilities.invokeLater(() -> {
getView().logSuccess("Der Import der Daten war Erfolgreich!");
getView().log("<hr>");
});
}

View File

@ -22,7 +22,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
private Presenter presenter;
private LinkedList<Line> set = new LinkedList<>();
private LinkedList<Point> intersections = new LinkedList<>();
private ArrayList<Point> intersections = new ArrayList<>();
private IntersectionCounter invCounter = new IntersectionCounter();
private int n;
private double quantileError;
@ -39,7 +39,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
private Double slope;
private Double yInterception;
public LeastMedianOfSquaresEstimator(LinkedList<Line> set, LinkedList<Point> intersections,
public LeastMedianOfSquaresEstimator(LinkedList<Line> set, ArrayList<Point> intersections,
Presenter presenter) {
this.set = set;
this.intersections = intersections;
@ -55,7 +55,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
this.presenter = presenter;
}
public LeastMedianOfSquaresEstimator(LinkedList<Line> set, LinkedList<Point> intersections) {
public LeastMedianOfSquaresEstimator(LinkedList<Line> set, ArrayList<Point> intersections) {
this(set, intersections, null);
}
@ -78,7 +78,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
intervals = new PriorityQueue<>(comparator);
intervals.add(new Interval(-100000, 100000));
heightsigmaMin = Double.MAX_VALUE;
LinkedList<Point> tmpIntersections = intersections;
ArrayList<Point> tmpIntersections = intersections;
//(3.) Apply the following steps as long as the exists active intervals
boolean active = true;
@ -265,11 +265,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
beta[i] = n - (alpha[i] + strictlyGreater);
strictlyGreater = 0;
}
//TEST der Alpha und Beta werte, siehe JUnit Test
//for (int i=0;i<alpha.length;i++){
// System.out.println("Alpha["+i+"]: "+alpha[i]+"\t Beta["+i+"]: "+beta[i]);
//}
//Test
//Teil II.
int i = 0;
@ -279,12 +275,8 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
while ((i < n && (Math.abs(beta[i] - alpha[j]) < kPlus))) {
i++;
}
//test
//if (i < n)
// System.out.println("i: "+i+", j:"+j+"\t "+Math.abs(beta[i] - alpha[j])+"\t kPlus: "+kPlus);
if (i >= n) {
//System.out.println("i: "+i+", j:"+j+". ungültig");
pslab.setActivity(false);
break;
} else {
@ -292,7 +284,6 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
Math.abs(umaxList.get(j) - umaxList.get(i)));
double error = 0.01;
if (((1 + error) * h) < heightsigmaMin) {
//System.out.println("h: "+ h +" ist kleiner als height(sigmaMin): "+heightsigmaMin);
pslab.setActivity(true);
return;
}
@ -382,11 +373,11 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
this.set = set;
}
public LinkedList<Point> getIntersections() {
public ArrayList<Point> getIntersections() {
return intersections;
}
public void setIntersections(LinkedList<Point> intersections) {
public void setIntersections(ArrayList<Point> intersections) {
this.intersections = intersections;
}

View File

@ -47,7 +47,7 @@ public class TheilSenEstimator extends Observable implements Algorithm {
private Double yInterception;
public TheilSenEstimator(LinkedList<Line> setOfLines, LinkedList<Point> setOfIntersections, Presenter presenter) {
public TheilSenEstimator(LinkedList<Line> setOfLines, ArrayList<Point> setOfIntersections, Presenter presenter) {
this.presenter = presenter;
this.setOfLines = new ArrayList<>(setOfLines);
this.setOfIntersections = new ArrayList<>(setOfIntersections);
@ -61,7 +61,7 @@ public class TheilSenEstimator extends Observable implements Algorithm {
this.k = Integer.valueOf((int) (N * 0.5)) - 1;
}
public TheilSenEstimator(LinkedList<Line> setOfLines, LinkedList<Point> setOfIntersections) {
public TheilSenEstimator(LinkedList<Line> setOfLines, ArrayList<Point> setOfIntersections) {
this(setOfLines, setOfIntersections, null);
}

View File

@ -2,6 +2,7 @@ package presenter.algorithms.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
@ -14,7 +15,7 @@ public class FastElementSelector {
* @param i
* @return
*/
public static Double randomizedSelect(ArrayList<Double> a, double i) {
public static Double randomizedSelect(List<Double> a, double i) {
int start = 0;
int end = a.size() - 1;
@ -48,7 +49,7 @@ public class FastElementSelector {
* @param end
* @return
*/
private static int randomizedPartition(ArrayList<Double> a, int start, int end) {
private static int randomizedPartition(List<Double> a, int start, int end) {
int i = 0;
Random random = new Random(System.currentTimeMillis());
@ -69,7 +70,7 @@ public class FastElementSelector {
* @param end
* @return
*/
private static int partition(ArrayList<Double> a, int start, int end) {
private static int partition(List<Double> a, int start, int end) {
Double x = a.get(end);
int i = start - 1;
for (int j = start; j <= end - 1; j++) {

View File

@ -32,8 +32,8 @@ public class EvaluateAlgorithms extends Observable {
private LinkedList<Line> rmL;
private LinkedList<Line> tsL;
private LinkedList<Point> lmsP;
private LinkedList<Point> tsP;
private ArrayList<Point> lmsP;
private ArrayList<Point> tsP;
private Thread lmsThread;
private Thread rmThread;
@ -92,8 +92,8 @@ public class EvaluateAlgorithms extends Observable {
rmL = new LinkedList<>(arrangement.getLines());
tsL = new LinkedList<>(arrangement.getLines());
lmsP = new LinkedList<>(arrangement.getNodes());
tsP = new LinkedList<>(arrangement.getNodes());
lmsP = new ArrayList<>(arrangement.getNodes());
tsP = new ArrayList<>(arrangement.getNodes());
}
public void run() throws InterruptedException {

View File

@ -1,27 +1,17 @@
package presenter.evaluation;
import jdk.nashorn.internal.scripts.JD;
import model.Line;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.core.*;
import org.opencv.highgui.*;
import org.opencv.imgproc.Imgproc;
import presenter.Presenter;
import presenter.algorithms.advanced.LeastMedianOfSquaresEstimator;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
/**
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
@ -33,9 +23,10 @@ import java.util.Observer;
public class PictureProcessor extends Observable{
private Mat image;
private Mat threshold;
private Mat contour;
private Presenter presenter;
private File file;
private ArrayList<MatOfPoint> contours;
public PictureProcessor(Presenter presenter, File file) {
this.file = file;
@ -43,24 +34,36 @@ public class PictureProcessor extends Observable{
}
public void run(){
String msg = "";
msg = SwingUtilities.isEventDispatchThread() ? "EDT" : "nicht EDT";
System.out.println(msg);
System.out.println("Welcome to OpenCV " + Core.VERSION);
image = Highgui.imread(file.getAbsolutePath());
threshold = process(image);
createInputData(threshold);
contour = process(image);
createInputData();
}
private Mat process(Mat image){
threshold = new Mat(image.width(), image.height(), CvType.CV_8UC1);
Mat threshold = new Mat(image.width(), image.height(), CvType.CV_8UC1);
Mat source = new Mat(image.width(), image.height(), CvType.CV_8UC1);
Imgproc.cvtColor(image, source, Imgproc.COLOR_BGR2GRAY);
Imgproc.adaptiveThreshold(source, threshold,255, Imgproc.ADAPTIVE_THRESH_MEAN_C,
Imgproc.THRESH_BINARY_INV, 11,2);
return threshold;
Imgproc.blur(source, threshold, new Size(3,3));
Imgproc.Canny(threshold, source,300,600,5,true);
//Konturen berechnen und filtern
contours = new ArrayList<>();
Imgproc.findContours(source, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0,0));
Mat viscont = new Mat(source.size(), source.type());
for (int i=0;i<contours.size();i++) {
if (Imgproc.contourArea(contours.get(i)) > 20) {
Imgproc.drawContours(viscont, contours, i, new Scalar(255, 255, 255), -1);
} else {
contours.remove(i);
}
}
SwingUtilities.invokeLater(() -> {
JDialog dialog = new JDialog();
dialog.setSize(1100,700);
dialog.add(new JLabel(new ImageIcon(toBufferedImage(viscont))));
dialog.setVisible(true);
});
return source;
}
@ -79,26 +82,26 @@ public class PictureProcessor extends Observable{
}
private void createInputData(Mat image){
private void createInputData(){
Thread t = new Thread(() -> {
int id = 0;
for (int i=0;i<image.rows();i++){
for (int j=0;j<image.cols();j++){
double[] colVal = image.get(i,j);
if (colVal[0] == 255d){
Line line = new Line(i,j);
line.setId(""+id++);
for (int j=0;j<contours.size();j++){
Point[] p = contours.get(j).toArray();
for (int i=0;i<p.length;i++) {
Line line = new Line(-1 * (500 - p[i].x), p[i].y);
line.setId("" + id++);
presenter.getModel().getLines().add(line);
}
}
}
setChanged();
String[] msg = {"import-picture"};
notifyObservers(msg);
});
t.start();
try {
t.join();
setChanged();
String[] msg = {"import-picture"};
notifyObservers(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}

View File

@ -20,6 +20,7 @@ import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.ArrayList;
import java.util.LinkedList;
/**
@ -54,7 +55,7 @@ public class DualityPanel extends JPanel {
this.setLayout(new BorderLayout());
}
public void setPrameters(LinkedList<Line> lines, LinkedList<Point> points, Double xmin, Double xmax, Double ymin, Double ymax) {
public void setPrameters(LinkedList<Line> lines, ArrayList<Point> points, Double xmin, Double xmax, Double ymin, Double ymax) {
this.lines = new LinkedList<>(lines);
this.points = new LinkedList<>(points);
this.domainMin = xmin;

View File

@ -32,7 +32,7 @@ public class LeastMedianOfSquaresEstimatorTest {
Double[] y = {18d, 26d, 30d, 40d, 70d};
LinkedList<Line> lines = new LinkedList<>();
LinkedList<Point> intersections = new LinkedList<>();
ArrayList<Point> intersections = new ArrayList<>();
for (int i = 0; i < 5; i++) {
lines.add(new Line(x[i], y[i]));