Browse Source

Version 27.11 ("neuer" Tracker)

master
David Moeckel 3 weeks ago
parent
commit
c5e0dec777
2 changed files with 87 additions and 42 deletions
  1. 13
    4
      .idea/workspace.xml
  2. 74
    38
      src/main/java/Tracker.java

+ 13
- 4
.idea/workspace.xml View File

<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Version 27.11">
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Version 27.11 (&quot;fertiger&quot; Tracker)">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/Tracker.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/Tracker.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/Tracker.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/Tracker.java" afterDir="false" />
</list> </list>
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1732108904989</updated> <updated>1732108904989</updated>
<workItem from="1732108906083" duration="758000" /> <workItem from="1732108906083" duration="758000" />
<workItem from="1732113837570" duration="11452000" />
<workItem from="1732113837570" duration="20534000" />
</task> </task>
<task id="LOCAL-00001" summary="First Commit Test"> <task id="LOCAL-00001" summary="First Commit Test">
<option name="closed" value="true" /> <option name="closed" value="true" />
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1732703104268</updated> <updated>1732703104268</updated>
</task> </task>
<option name="localTasksCounter" value="5" />
<task id="LOCAL-00005" summary="Version 27.11 (&quot;fertiger&quot; Tracker)">
<option name="closed" value="true" />
<created>1732706211714</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1732706211714</updated>
</task>
<option name="localTasksCounter" value="6" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
<MESSAGE value="Version 20.11" /> <MESSAGE value="Version 20.11" />
<MESSAGE value="Version 23.11 (Grafics update)" /> <MESSAGE value="Version 23.11 (Grafics update)" />
<MESSAGE value="Version 27.11" /> <MESSAGE value="Version 27.11" />
<option name="LAST_COMMIT_MESSAGE" value="Version 27.11" />
<MESSAGE value="Version 27.11 (&quot;fertiger&quot; Tracker)" />
<option name="LAST_COMMIT_MESSAGE" value="Version 27.11 (&quot;fertiger&quot; Tracker)" />
</component> </component>
</project> </project>

+ 74
- 38
src/main/java/Tracker.java View File

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.videoio.VideoCapture;
import org.opencv.highgui.HighGui; import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Imgproc;
import org.opencv.imgproc.Moments; import org.opencv.imgproc.Moments;
import org.opencv.videoio.Videoio; import org.opencv.videoio.Videoio;
import processing.core.PApplet;



import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.Comparator;



public class Tracker { 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 tracker = new Tracker();
tracker.run(); tracker.run();
} }


private void run(){
private void run() {
VideoCapture capture = setUp(); VideoCapture capture = setUp();


if (!capture.isOpened()) { if (!capture.isOpened()) {
} }


Mat frame = new Mat(); 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) { if (HighGui.waitKey(1) == 27) {
break; break;
} }
}else{
} else {
System.out.println("Fehler: Das Kamerabild konnte nicht gelesen werden."); System.out.println("Fehler: Das Kamerabild konnte nicht gelesen werden.");
break; 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() { private VideoCapture setUp() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture capture = new VideoCapture(CAM); VideoCapture capture = new VideoCapture(CAM);

Loading…
Cancel
Save