diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 1926fe1..fa69fca 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,7 +4,7 @@
-
+
@@ -77,7 +77,7 @@
1732108904989
-
+
@@ -111,7 +111,15 @@
1732703104268
-
+
+
+ 1732706211714
+
+
+
+ 1732706211714
+
+
@@ -133,6 +141,7 @@
-
+
+
\ No newline at end of file
diff --git a/src/main/java/Tracker.java b/src/main/java/Tracker.java
index d2f1ab5..a7c45b1 100644
--- a/src/main/java/Tracker.java
+++ b/src/main/java/Tracker.java
@@ -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)){
+ 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);
+
+ // Tracke die Positionen der beiden größten Objekte
+ List 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);
- currentPosition = trackPosition(frame);
-
if (HighGui.waitKey(1) == 27) {
break;
}
- }else{
+ } else {
System.out.println("Fehler: Das Kamerabild konnte nicht gelesen werden.");
break;
}
}
-
-
}
- public int getX(){
- return (int) currentPosition.x;
+ private List trackPositions(Mat thresholdFrame) {
+ List contours = new ArrayList<>();
+ Mat hierarchy = new Mat();
+
+ // Finde Konturen
+ Imgproc.findContours(thresholdFrame, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
+
+ // Sortiere Konturen nach Fläche (absteigend)
+ contours.sort((c1, c2) -> Double.compare(Imgproc.contourArea(c2), Imgproc.contourArea(c1)));
+
+
+ // Speichere die Positionen der zwei größten Konturen
+ List 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));
+ }
+ }
+
+ return positions;
}
- public int getY(){
- return (int) currentPosition.y;
- }
+ private void drawMarkers(Mat frame) {
+ // Größe der Quadrate
+ int size = 10;
- public Point getPosition(){
- return currentPosition;
- }
+ // 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
+ }
-
- private Point trackPosition(Mat thresholdFrame) {
- // Berechne die Momente des binären Bildes
- Moments moments = Imgproc.moments(thresholdFrame);
-
- // 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 {
-
- 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);