Version 27.11 ("neuer" Tracker)
This commit is contained in:
parent
c2b5dc7e96
commit
c5e0dec777
17
.idea/workspace.xml
generated
17
.idea/workspace.xml
generated
@ -4,7 +4,7 @@
|
|||||||
<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 ("fertiger" 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>
|
||||||
@ -77,7 +77,7 @@
|
|||||||
<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" />
|
||||||
@ -111,7 +111,15 @@
|
|||||||
<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 ("fertiger" 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">
|
||||||
@ -133,6 +141,7 @@
|
|||||||
<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 ("fertiger" Tracker)" />
|
||||||
|
<option name="LAST_COMMIT_MESSAGE" value="Version 27.11 ("fertiger" Tracker)" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -1,34 +1,36 @@
|
|||||||
import org.opencv.core.Core;
|
import org.opencv.core.*;
|
||||||
import org.opencv.core.Mat;
|
|
||||||
import org.opencv.core.Point;
|
|
||||||
import org.opencv.core.Size;
|
|
||||||
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 CAM = 1; // Kamera-ID (0 für Webcam, 1 für andere Cams)
|
||||||
private final int EXP = -7; // Belichtungszeit (idealerweise: 0)
|
private final int EXP = 0; // Belichtungszeit (idealerweise: 0)
|
||||||
private final int FPS = 60; // FPS
|
private final int FPS = 60; // FPS
|
||||||
|
private final int THV = 250;
|
||||||
|
|
||||||
private Point currentPosition;
|
private Point currentPositionP1 = new Point(0, 0);
|
||||||
public Tracker(){
|
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()) {
|
||||||
@ -37,52 +39,86 @@ public class Tracker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Mat frame = new Mat();
|
Mat frame = new Mat();
|
||||||
|
Mat grayFrame = new Mat();
|
||||||
|
Mat thresholdFrame = new Mat();
|
||||||
|
|
||||||
while (true){
|
while (true) {
|
||||||
if (capture.read(frame)){
|
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);
|
HighGui.imshow("GTCar", frame);
|
||||||
|
|
||||||
currentPosition = trackPosition(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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getX(){
|
private List<Point> trackPositions(Mat thresholdFrame) {
|
||||||
return (int) currentPosition.x;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getY(){
|
private void drawMarkers(Mat frame) {
|
||||||
return (int) currentPosition.y;
|
// Größe der Quadrate
|
||||||
}
|
int size = 10;
|
||||||
|
|
||||||
public Point getPosition(){
|
// Blaues Quadrat an currentPositionP1
|
||||||
return currentPosition;
|
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
|
||||||
private Point trackPosition(Mat thresholdFrame) {
|
if (currentPositionP2 != null) {
|
||||||
// Berechne die Momente des binären Bildes
|
Point topLeftP2 = new Point(currentPositionP2.x - size, currentPositionP2.y - size);
|
||||||
Moments moments = Imgproc.moments(thresholdFrame);
|
Point bottomRightP2 = new Point(currentPositionP2.x + size, currentPositionP2.y + size);
|
||||||
|
Imgproc.rectangle(frame, topLeftP2, bottomRightP2, new Scalar(0, 0, 255), -1); // Rot, gefüllt
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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…
x
Reference in New Issue
Block a user