120 lines
4.1 KiB
Java
120 lines
4.1 KiB
Java
package presenter.evaluation;
|
|
import jdk.nashorn.internal.scripts.JD;
|
|
import model.Line;
|
|
import org.opencv.core.*;
|
|
import org.opencv.highgui.*;
|
|
import org.opencv.imgproc.Imgproc;
|
|
import presenter.Presenter;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.DataBufferByte;
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.Observable;
|
|
|
|
/**
|
|
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
|
|
*
|
|
* @Author: Armin Wolf
|
|
* @Email: a_wolf28@uni-muenster.de
|
|
* @Date: 17.09.2017.
|
|
*/
|
|
public class PictureProcessor extends Observable{
|
|
|
|
private Mat image;
|
|
private Mat contour;
|
|
private Presenter presenter;
|
|
private File file;
|
|
private ArrayList<MatOfPoint> contours;
|
|
private double contourLength;
|
|
|
|
public PictureProcessor(Presenter presenter, File file) {
|
|
this.file = file;
|
|
this.presenter = presenter;
|
|
}
|
|
|
|
public void run(){
|
|
image = Highgui.imread(file.getAbsolutePath());
|
|
contour = process(image);
|
|
contourLength = image.width() * 0.3;
|
|
createInputData();
|
|
}
|
|
|
|
private Mat process(Mat image){
|
|
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.GaussianBlur(source, threshold, new Size(3,3),0,0);
|
|
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());
|
|
double minArea = 50;
|
|
double maxArea = 0.2 * image.cols() * image.rows();
|
|
for (int i=0;i<contours.size();i++) {
|
|
double acutualArea = Imgproc.contourArea(contours.get(i));
|
|
if (minArea < acutualArea && maxArea > acutualArea) {
|
|
Imgproc.drawContours(viscont, contours, i, new Scalar(255, 0, 0), 1);
|
|
}
|
|
}
|
|
Mat resized = new Mat();
|
|
Imgproc.resize(viscont, resized,new Size(560,560));
|
|
SwingUtilities.invokeLater(() -> {
|
|
JDialog dialog = new JDialog();
|
|
dialog.setSize(560,560);
|
|
JLabel label = new JLabel();
|
|
label.setSize(560,560);
|
|
label.setIcon(new ImageIcon(toBufferedImage(resized)));
|
|
dialog.add(label);
|
|
dialog.setVisible(true);
|
|
});
|
|
return source;
|
|
}
|
|
|
|
|
|
private BufferedImage toBufferedImage(Mat m) {
|
|
int type = BufferedImage.TYPE_BYTE_GRAY;
|
|
if (m.channels() > 1) {
|
|
type = BufferedImage.TYPE_3BYTE_BGR;
|
|
}
|
|
int bufferSize = m.channels() * m.cols() * m.rows();
|
|
byte[] b = new byte[bufferSize];
|
|
m.get(0, 0, b); // get all the pixels
|
|
BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
|
|
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
|
System.arraycopy(b, 0, targetPixels, 0, b.length);
|
|
return image;
|
|
|
|
}
|
|
|
|
private void createInputData(){
|
|
Thread t = new Thread(() -> {
|
|
double minArea = 50;
|
|
double maxArea = 0.2 * image.cols() * image.rows();
|
|
int id = 0;
|
|
for (int j=0;j<contours.size();j++){
|
|
Point[] p = contours.get(j).toArray();
|
|
double acutualArea = Imgproc.contourArea(contours.get(j));
|
|
if (minArea < acutualArea && maxArea > acutualArea) {
|
|
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();
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} |