136 lines
4.6 KiB
Java
136 lines
4.6 KiB
Java
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 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 = 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 currentPositionP1 = new Point(0, 0);
|
|
private Point currentPositionP2 = new Point(0, 0);
|
|
|
|
public Tracker() {}
|
|
|
|
public static void main(String[] args) {
|
|
Tracker tracker = new Tracker();
|
|
tracker.run();
|
|
}
|
|
|
|
private void run() {
|
|
VideoCapture capture = setUp();
|
|
|
|
if (!capture.isOpened()) {
|
|
System.out.println("Fehler: Die Webcam konnte nicht geöffnet werden.");
|
|
return;
|
|
}
|
|
|
|
Mat frame = new Mat();
|
|
Mat grayFrame = new Mat();
|
|
Mat thresholdFrame = new Mat();
|
|
|
|
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<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 {
|
|
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);
|
|
|
|
// 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<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));
|
|
}
|
|
}
|
|
|
|
return positions;
|
|
}
|
|
|
|
private void drawMarkers(Mat frame) {
|
|
// Größe der Quadrate
|
|
int size = 10;
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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);
|
|
|
|
if (!capture.isOpened()) {
|
|
System.out.println("Fehler: Die Webcam konnte nicht geöffnet werden.");
|
|
}
|
|
|
|
capture.set(Videoio.CAP_PROP_EXPOSURE, EXP);
|
|
capture.set(Videoio.CAP_PROP_FPS, FPS);
|
|
|
|
return capture;
|
|
}
|
|
}
|