|
|
@@ -1,34 +1,36 @@ |
|
|
|
import org.opencv.core.Core; |
|
|
|
import org.opencv.core.Mat; |
|
|
|
import org.opencv.core.Point; |
|
|
|
import org.opencv.core.Size; |
|
|
|
import org.opencv.core.*; |
|
|
|
import org.opencv.videoio.VideoCapture; |
|
|
|
import org.opencv.highgui.HighGui; |
|
|
|
import org.opencv.imgproc.Imgproc; |
|
|
|
import org.opencv.imgproc.Moments; |
|
|
|
import org.opencv.videoio.Videoio; |
|
|
|
import processing.core.PApplet; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Queue; |
|
|
|
import java.util.Comparator; |
|
|
|
|
|
|
|
|
|
|
|
public class Tracker { |
|
|
|
|
|
|
|
private final int CAM = 0; // Kamera-ID (0 für Webcam, 1 für andere Cams) |
|
|
|
private final int EXP = -7; // Belichtungszeit (idealerweise: 0) |
|
|
|
private final int FPS = 60; // FPS |
|
|
|
private final int CAM = 1; // Kamera-ID (0 für Webcam, 1 für andere Cams) |
|
|
|
private final int EXP = 0; // Belichtungszeit (idealerweise: 0) |
|
|
|
private final int FPS = 60; // FPS |
|
|
|
private final int THV = 250; |
|
|
|
|
|
|
|
private Point currentPosition; |
|
|
|
public Tracker(){ |
|
|
|
private Point currentPositionP1 = new Point(0, 0); |
|
|
|
private Point currentPositionP2 = new Point(0, 0); |
|
|
|
|
|
|
|
} |
|
|
|
public Tracker() {} |
|
|
|
|
|
|
|
public static void main(String[] args){ |
|
|
|
public static void main(String[] args) { |
|
|
|
Tracker tracker = new Tracker(); |
|
|
|
tracker.run(); |
|
|
|
} |
|
|
|
|
|
|
|
private void run(){ |
|
|
|
private void run() { |
|
|
|
VideoCapture capture = setUp(); |
|
|
|
|
|
|
|
if (!capture.isOpened()) { |
|
|
@@ -37,52 +39,86 @@ public class Tracker { |
|
|
|
} |
|
|
|
|
|
|
|
Mat frame = new Mat(); |
|
|
|
Mat grayFrame = new Mat(); |
|
|
|
Mat thresholdFrame = new Mat(); |
|
|
|
|
|
|
|
while (true){ |
|
|
|
if (capture.read(frame)){ |
|
|
|
HighGui.imshow("GTCar", frame); |
|
|
|
while (true) { |
|
|
|
if (capture.read(frame)) { |
|
|
|
|
|
|
|
|
|
|
|
// Konvertiere in Graustufen und wende Threshold an |
|
|
|
Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY); |
|
|
|
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); |
|
|
|
|
|
|
|
currentPosition = trackPosition(frame); |
|
|
|
// Tracke die Positionen der beiden größten Objekte |
|
|
|
List<Point> positions = trackPositions(thresholdFrame); |
|
|
|
|
|
|
|
if (positions.size() >= 2) { |
|
|
|
currentPositionP1 = positions.get(0); |
|
|
|
currentPositionP2 = positions.get(1); |
|
|
|
|
|
|
|
System.out.println("P1 - X: " + currentPositionP1.x + " Y: " + currentPositionP1.y + " P2 - X: " + currentPositionP2.x + " Y: " + currentPositionP2.y); |
|
|
|
|
|
|
|
} |
|
|
|
drawMarkers(frame); |
|
|
|
HighGui.imshow("GTCar", frame); |
|
|
|
|
|
|
|
if (HighGui.waitKey(1) == 27) { |
|
|
|
break; |
|
|
|
} |
|
|
|
}else{ |
|
|
|
} else { |
|
|
|
System.out.println("Fehler: Das Kamerabild konnte nicht gelesen werden."); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private List<Point> trackPositions(Mat thresholdFrame) { |
|
|
|
List<MatOfPoint> contours = new ArrayList<>(); |
|
|
|
Mat hierarchy = new Mat(); |
|
|
|
|
|
|
|
} |
|
|
|
// Finde Konturen |
|
|
|
Imgproc.findContours(thresholdFrame, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); |
|
|
|
|
|
|
|
public int getX(){ |
|
|
|
return (int) currentPosition.x; |
|
|
|
} |
|
|
|
// Sortiere Konturen nach Fläche (absteigend) |
|
|
|
contours.sort((c1, c2) -> Double.compare(Imgproc.contourArea(c2), Imgproc.contourArea(c1))); |
|
|
|
|
|
|
|
public int getY(){ |
|
|
|
return (int) currentPosition.y; |
|
|
|
} |
|
|
|
|
|
|
|
public Point getPosition(){ |
|
|
|
return currentPosition; |
|
|
|
} |
|
|
|
// Speichere die Positionen der zwei größten Konturen |
|
|
|
List<Point> positions = new ArrayList<>(); |
|
|
|
|
|
|
|
for (int i = 0; i < Math.min(contours.size(), 2); i++) { |
|
|
|
Moments moments = Imgproc.moments(contours.get(i)); |
|
|
|
if (moments.get_m00() != 0) { |
|
|
|
int cx = (int) (moments.get_m10() / moments.get_m00()); |
|
|
|
int cy = (int) (moments.get_m01() / moments.get_m00()); |
|
|
|
positions.add(new Point(cx, cy)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private Point trackPosition(Mat thresholdFrame) { |
|
|
|
// Berechne die Momente des binären Bildes |
|
|
|
Moments moments = Imgproc.moments(thresholdFrame); |
|
|
|
return positions; |
|
|
|
} |
|
|
|
|
|
|
|
private void drawMarkers(Mat frame) { |
|
|
|
// Größe der Quadrate |
|
|
|
int size = 10; |
|
|
|
|
|
|
|
// Prüfen, ob m00 (Fläche) nicht null ist, um Division durch Null zu vermeiden |
|
|
|
if (moments.get_m00() != 0) { |
|
|
|
int cx = (int) (moments.get_m10() / moments.get_m00()); |
|
|
|
int cy = (int) (moments.get_m01() / moments.get_m00()); |
|
|
|
return new Point(cx, cy); |
|
|
|
} else { |
|
|
|
// Blaues Quadrat an currentPositionP1 |
|
|
|
if (currentPositionP1 != null) { |
|
|
|
Point topLeftP1 = new Point(currentPositionP1.x - size, currentPositionP1.y - size); |
|
|
|
Point bottomRightP1 = new Point(currentPositionP1.x + size, currentPositionP1.y + size); |
|
|
|
Imgproc.rectangle(frame, topLeftP1, bottomRightP1, new Scalar(255, 0, 0), -1); // Blau, gefüllt |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
// Rotes Quadrat an currentPositionP2 |
|
|
|
if (currentPositionP2 != null) { |
|
|
|
Point topLeftP2 = new Point(currentPositionP2.x - size, currentPositionP2.y - size); |
|
|
|
Point bottomRightP2 = new Point(currentPositionP2.x + size, currentPositionP2.y + size); |
|
|
|
Imgproc.rectangle(frame, topLeftP2, bottomRightP2, new Scalar(0, 0, 255), -1); // Rot, gefüllt |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private VideoCapture setUp() { |
|
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); |
|
|
|
VideoCapture capture = new VideoCapture(CAM); |