</list> | </list> | ||||
</option> | </option> | ||||
</component> | </component> | ||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK"> | |||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK"> | |||||
<output url="file://$PROJECT_DIR$/out" /> | <output url="file://$PROJECT_DIR$/out" /> | ||||
</component> | </component> | ||||
</project> | </project> |
<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 04.12 "vollständiges Programm mit Fehlern""> | |||||
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Version 20.11"> | |||||
<change afterPath="$PROJECT_DIR$/src/main/java/CamPuffer.java" afterDir="false" /> | |||||
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> | |||||
<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/GameLogic.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GameLogic.java" afterDir="false" /> | |||||
<change beforePath="$PROJECT_DIR$/src/main/java/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GameLoop.java" afterDir="false" /> | <change beforePath="$PROJECT_DIR$/src/main/java/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GameLoop.java" afterDir="false" /> | ||||
<change beforePath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" afterDir="false" /> | <change beforePath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" afterDir="false" /> | ||||
<change beforePath="$PROJECT_DIR$/src/main/java/Player.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/Player.java" 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/ImgAnalyzer.java" afterDir="false" /> | |||||
</list> | </list> | ||||
<option name="SHOW_DIALOG" value="false" /> | <option name="SHOW_DIALOG" value="false" /> | ||||
<option name="HIGHLIGHT_CONFLICTS" value="true" /> | <option name="HIGHLIGHT_CONFLICTS" value="true" /> | ||||
<option name="hideEmptyMiddlePackages" value="true" /> | <option name="hideEmptyMiddlePackages" value="true" /> | ||||
<option name="showLibraryContents" value="true" /> | <option name="showLibraryContents" value="true" /> | ||||
</component> | </component> | ||||
<component name="PropertiesComponent"><![CDATA[{ | |||||
"keyToString": { | |||||
"Application.GTCar.executor": "Run", | |||||
"Application.GraphicsProgram.executor": "Run", | |||||
"Application.Main.executor": "Run", | |||||
"Application.Tracker.executor": "Run", | |||||
"Downloaded.Files.Path.Enabled": "false", | |||||
"Repository.Attach.Annotations": "false", | |||||
"Repository.Attach.JavaDocs": "false", | |||||
"Repository.Attach.Sources": "false", | |||||
"RunOnceActivity.OpenProjectViewOnStart": "true", | |||||
"RunOnceActivity.ShowReadmeOnStart": "true", | |||||
"com.codeium.enabled": "true", | |||||
"git-widget-placeholder": "master", | |||||
"kotlin-language-version-configured": "true", | |||||
"last_opened_file_path": "C:/Users/david/Documents/Uni/GTCar/opencv/build/java/opencv-4100.jar", | |||||
"node.js.detected.package.eslint": "true", | |||||
"node.js.detected.package.tslint": "true", | |||||
"node.js.selected.package.eslint": "(autodetect)", | |||||
"node.js.selected.package.tslint": "(autodetect)", | |||||
"nodejs_package_manager_path": "npm", | |||||
"project.structure.last.edited": "Libraries", | |||||
"project.structure.proportion": "0.15", | |||||
"project.structure.side.proportion": "0.24022989", | |||||
"vue.rearranger.settings.migration": "true" | |||||
<component name="PropertiesComponent">{ | |||||
"keyToString": { | |||||
"Application.GraphicsProgram.executor": "Run", | |||||
"Application.Main.executor": "Run", | |||||
"Application.Tracker.executor": "Run", | |||||
"Downloaded.Files.Path.Enabled": "false", | |||||
"Repository.Attach.Annotations": "false", | |||||
"Repository.Attach.JavaDocs": "false", | |||||
"Repository.Attach.Sources": "false", | |||||
"RunOnceActivity.OpenProjectViewOnStart": "true", | |||||
"RunOnceActivity.ShowReadmeOnStart": "true", | |||||
"com.codeium.enabled": "true", | |||||
"git-widget-placeholder": "master", | |||||
"kotlin-language-version-configured": "true", | |||||
"last_opened_file_path": "C:/Users/paul3/OneDrive/Dokumente/Studium BME/Semester 3/Interaktion/opencv/build/java/opencv-4100.jar", | |||||
"node.js.detected.package.eslint": "true", | |||||
"node.js.detected.package.tslint": "true", | |||||
"node.js.selected.package.eslint": "(autodetect)", | |||||
"node.js.selected.package.tslint": "(autodetect)", | |||||
"nodejs_package_manager_path": "npm", | |||||
"project.structure.last.edited": "Libraries", | |||||
"project.structure.proportion": "0.15", | |||||
"project.structure.side.proportion": "0.24022989", | |||||
"vue.rearranger.settings.migration": "true" | |||||
} | } | ||||
}]]></component> | |||||
}</component> | |||||
<component name="RunManager" selected="Application.Main"> | <component name="RunManager" selected="Application.Main"> | ||||
<configuration name="GTCar" type="Application" factoryName="Application"> | |||||
<option name="MAIN_CLASS_NAME" value="Tracker" /> | |||||
<module name="GTCar_V2_maven" /> | |||||
<option name="VM_PARAMETERS" value="-Djava.library.path=C:\Users\david\Documents\Uni\GTCar\opencv\build\java\x64" /> | |||||
<method v="2"> | |||||
<option name="Make" enabled="true" /> | |||||
</method> | |||||
</configuration> | |||||
<configuration name="GraphicsProgram" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> | |||||
<option name="MAIN_CLASS_NAME" value="GraphicsProgram" /> | |||||
<configuration name="ImgAnalyzer" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> | |||||
<option name="MAIN_CLASS_NAME" value="ImgAnalyzer" /> | |||||
<module name="GTCar_V2_maven" /> | <module name="GTCar_V2_maven" /> | ||||
<option name="PROGRAM_PARAMETERS" value="-Djava.library.path="C:\Users\paul3\OneDrive\Dokumente\Studium BME\Semester 3\Interaktion\opencv\build\java\x64"" /> | |||||
<method v="2"> | <method v="2"> | ||||
<option name="Make" enabled="true" /> | <option name="Make" enabled="true" /> | ||||
</method> | </method> | ||||
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> | <configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> | ||||
<option name="MAIN_CLASS_NAME" value="Main" /> | <option name="MAIN_CLASS_NAME" value="Main" /> | ||||
<module name="GTCar_V2_maven" /> | <module name="GTCar_V2_maven" /> | ||||
<option name="VM_PARAMETERS" value="-Djava.library.path=C:\Users\david\Documents\Uni\GTCar\opencv\build\java\x64" /> | |||||
<option name="PROGRAM_PARAMETERS" value="-Djava.library.path="C:\Users\paul3\OneDrive\Dokumente\Studium BME\Semester 3\Interaktion\opencv\build\java\x64"" /> | |||||
<method v="2"> | <method v="2"> | ||||
<option name="Make" enabled="true" /> | <option name="Make" enabled="true" /> | ||||
</method> | </method> | ||||
</configuration> | </configuration> | ||||
<list> | <list> | ||||
<item itemvalue="Application.GTCar" /> | |||||
<item itemvalue="Application.GraphicsProgram" /> | |||||
<item itemvalue="Application.Main" /> | <item itemvalue="Application.Main" /> | ||||
<item itemvalue="Application.ImgAnalyzer" /> | |||||
</list> | </list> | ||||
<recent_temporary> | <recent_temporary> | ||||
<list> | <list> | ||||
<item itemvalue="Application.Main" /> | <item itemvalue="Application.Main" /> | ||||
<item itemvalue="Application.GraphicsProgram" /> | |||||
<item itemvalue="Application.ImgAnalyzer" /> | |||||
</list> | </list> | ||||
</recent_temporary> | </recent_temporary> | ||||
</component> | </component> | ||||
<component name="SharedIndexes"> | |||||
<attachedChunks> | |||||
<set> | |||||
<option value="bundled-jdk-9f38398b9061-39b83d9b5494-intellij.indexing.shared.core-IU-241.15989.150" /> | |||||
<option value="bundled-js-predefined-1d06a55b98c1-91d5c284f522-JavaScript-IU-241.15989.150" /> | |||||
</set> | |||||
</attachedChunks> | |||||
</component> | |||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> | <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> | ||||
<component name="TaskManager"> | <component name="TaskManager"> | ||||
<task active="true" id="Default" summary="Default task"> | <task active="true" id="Default" summary="Default task"> | ||||
<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="20534000" /> | |||||
<workItem from="1733327375447" duration="951000" /> | |||||
<workItem from="1734091457440" duration="10746000" /> | |||||
<workItem from="1732113837570" duration="5187000" /> | |||||
<workItem from="1734302243298" duration="10745000" /> | |||||
</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>1732118799936</updated> | <updated>1732118799936</updated> | ||||
</task> | </task> | ||||
<task id="LOCAL-00003" summary="Version 23.11 (Grafics update)"> | |||||
<option name="closed" value="true" /> | |||||
<created>1732368525825</created> | |||||
<option name="number" value="00003" /> | |||||
<option name="presentableId" value="LOCAL-00003" /> | |||||
<option name="project" value="LOCAL" /> | |||||
<updated>1732368525826</updated> | |||||
</task> | |||||
<task id="LOCAL-00004" summary="Version 27.11"> | |||||
<option name="closed" value="true" /> | |||||
<created>1732703104268</created> | |||||
<option name="number" value="00004" /> | |||||
<option name="presentableId" value="LOCAL-00004" /> | |||||
<option name="project" value="LOCAL" /> | |||||
<updated>1732703104268</updated> | |||||
</task> | |||||
<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" /> | |||||
<option name="localTasksCounter" value="3" /> | |||||
<servers /> | <servers /> | ||||
</component> | </component> | ||||
<component name="TypeScriptGeneratedFilesManager"> | <component name="TypeScriptGeneratedFilesManager"> | ||||
<component name="VcsManagerConfiguration"> | <component name="VcsManagerConfiguration"> | ||||
<MESSAGE value="First Commit Test" /> | <MESSAGE value="First Commit Test" /> | ||||
<MESSAGE value="Version 20.11" /> | <MESSAGE value="Version 20.11" /> | ||||
<MESSAGE value="Version 23.11 (Grafics update)" /> | |||||
<MESSAGE value="Version 27.11" /> | |||||
<MESSAGE value="Version 27.11 ("fertiger" Tracker)" /> | |||||
<option name="LAST_COMMIT_MESSAGE" value="Version 27.11 ("fertiger" Tracker)" /> | |||||
<option name="LAST_COMMIT_MESSAGE" value="Version 20.11" /> | |||||
</component> | </component> | ||||
</project> | </project> |
import org.opencv.core.*; | |||||
import org.opencv.videoio.VideoCapture; | |||||
import org.opencv.imgproc.Imgproc; | |||||
import org.opencv.highgui.HighGui; | |||||
import org.opencv.videoio.Videoio; | |||||
import java.util.LinkedList; | |||||
public class CamPuffer implements Runnable { | |||||
static { | |||||
try { | |||||
System.load("C:\\Users\\paul3\\OneDrive\\Dokumente\\Studium BME\\Semester 3\\Interaktion\\GTCar V3\\opencv\\build\\java\\x64\\opencv_java4100.dll"); | |||||
System.out.println("OpenCV manuell geladen!"); | |||||
} catch (UnsatisfiedLinkError e) { | |||||
System.err.println("Fehler beim Laden der OpenCV-Bibliothek: " + e.getMessage()); | |||||
} | |||||
} | |||||
private final int CAM; // Kamera-ID (0 für Webcam, 1 für andere Cams) | |||||
private final int EXP; // Belichtungszeit | |||||
private final int FPS; | |||||
private final int THV; | |||||
private volatile boolean running = true; | |||||
private final Object lock = new Object(); // Lock für Thread-Sicherheit | |||||
private Mat currentMat = new Mat(); | |||||
private final Rect roi = new Rect(200, 150, 240, 180); // Region of Interest | |||||
private boolean hasStartedCapturing = false; | |||||
private final LinkedList<Mat> frameBuffer = new LinkedList<>(); | |||||
private final int MAX_BUFFER_SIZE = 10; // Maximale Anzahl an Bildern im Puffer | |||||
public CamPuffer(int CAM, int EXP, int FPS, int THV) { | |||||
this.CAM = CAM; | |||||
this.EXP = EXP; | |||||
this.FPS = FPS; | |||||
this.THV = THV; | |||||
} | |||||
@Override | |||||
public void run() { | |||||
VideoCapture capture = setUp(); | |||||
if (!capture.isOpened()) { | |||||
System.out.println("Fehler: Die Webcam konnte nicht geöffnet werden."); | |||||
return; | |||||
} | |||||
Mat frame = new Mat(); | |||||
hasStartedCapturing = true; | |||||
long lastGcTime = System.currentTimeMillis(); | |||||
try { | |||||
while (running) { | |||||
long startTime = System.currentTimeMillis(); | |||||
capture.read(frame); // Bild einlesen | |||||
if (frame.empty()) { | |||||
System.out.println("Fehler: Kein Bild von der Kamera erhalten."); | |||||
break; | |||||
} | |||||
// Bildverarbeitung hier | |||||
// Debug-Ausgabe | |||||
long elapsedTime = System.currentTimeMillis() - startTime; | |||||
System.out.println("Verarbeitungszeit für das Bild: " + elapsedTime + " ms"); | |||||
// Speicherbereinigung alle 1000 ms (1 Sekunde) | |||||
if (System.currentTimeMillis() - lastGcTime > 1000) { | |||||
System.gc(); // Triggern des Garbage Collectors | |||||
lastGcTime = System.currentTimeMillis(); | |||||
System.out.println("Garbage Collection durchgeführt."); | |||||
} | |||||
} | |||||
} finally { | |||||
// Matrizen explizit freigeben | |||||
frame.release(); // Falls `frame` in der Schleife freigegeben wurde, hier nochmal | |||||
capture.release(); // Kamera-Ressourcen freigeben | |||||
} | |||||
} | |||||
public boolean isHasStartedCapturing() { | |||||
return hasStartedCapturing; | |||||
} | |||||
private void updateCurrentMat(Mat img) { | |||||
synchronized (lock) { | |||||
// Puffergröße überwachen | |||||
if (frameBuffer.size() >= MAX_BUFFER_SIZE) { | |||||
Mat oldFrame = frameBuffer.poll(); // Entfernt das älteste Bild im Puffer | |||||
if (oldFrame != null) { | |||||
oldFrame.release(); // Verhindert das Anhäufen von nicht freigegebenen Mat-Objekten | |||||
} | |||||
} | |||||
// Neues Bild in den Puffer hinzufügen | |||||
frameBuffer.add(img.clone()); | |||||
} | |||||
} | |||||
public Mat getCurrentMat() { | |||||
synchronized (lock) { | |||||
return currentMat.clone(); // Rückgabe einer Kopie, um Speicherprobleme zu vermeiden | |||||
} | |||||
} | |||||
public void stop() { | |||||
running = false; | |||||
} | |||||
private VideoCapture setUp() { | |||||
System.out.println("Laden von OpenCV"); | |||||
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); | |||||
System.out.println("Kamera geöffnet und eingerichtet"); | |||||
return capture; | |||||
} | |||||
} |
import processing.core.PApplet; | import processing.core.PApplet; | ||||
import java.awt.Point; | import java.awt.Point; | ||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
public class GameLoop { | public class GameLoop { | ||||
int activePlayer = 0; | |||||
Tracker tracker; | |||||
ImgAnalyzer tracker; | |||||
GraphicsProgram graphics; | GraphicsProgram graphics; | ||||
player1 = new Player(0, "192.168.1.1", 9000); | player1 = new Player(0, "192.168.1.1", 9000); | ||||
player2 = new Player(1, "192.168.1.2", 9000); | player2 = new Player(1, "192.168.1.2", 9000); | ||||
tracker = new Tracker(); | |||||
tracker = new ImgAnalyzer(); | |||||
// Starte Tracker in einem separaten Thread | // Starte Tracker in einem separaten Thread | ||||
Thread trackerThread = new Thread(tracker::run); | Thread trackerThread = new Thread(tracker::run); | ||||
trackerThread.start(); | trackerThread.start(); | ||||
Point p2 = tracker.getP2(); | Point p2 = tracker.getP2(); | ||||
//System.out.println("Player 2 is at " + p2.x + " and " + p2.y); | //System.out.println("Player 2 is at " + p2.x + " and " + p2.y); | ||||
player1.setKoords(p1.x, p1.y); | |||||
player2.setKoords(p2.x, p2.y); | |||||
player1.setKoords((int)p1.x, (int)p1.y); | |||||
player2.setKoords((int)p2.x, (int)p2.y); | |||||
if(gl.doCheckExtern(player2.getTrail(), p1)){ //Check Position von P1 mit Spur von P2 | |||||
/*if(gl.doCheckExtern(player2.getTrail(), p1)){ //Check Position von P1 mit Spur von P2 | |||||
System.out.println("Spieler 1 hat verloren"); | System.out.println("Spieler 1 hat verloren"); | ||||
break; | break; | ||||
} | } | ||||
if(gl.doCheckExtern(player1.getTrail(), p2)){ //Check Position von P2 mit Spur von P1 | if(gl.doCheckExtern(player1.getTrail(), p2)){ //Check Position von P2 mit Spur von P1 | ||||
System.out.println("Spieler 2 hat verloren"); | System.out.println("Spieler 2 hat verloren"); | ||||
break; | break; | ||||
} | |||||
} */ | |||||
draw(); | |||||
//draw(); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
public void settings() { | public void settings() { | ||||
fullScreen(); | |||||
//fullScreen(); | |||||
} | } | ||||
public void setup(){ | public void setup(){ | ||||
while(!trail.isEmpty()) { | while(!trail.isEmpty()) { | ||||
int[] koordinaten = trail.poll(); | int[] koordinaten = trail.poll(); | ||||
int x = koordinaten[0]; | |||||
int y = koordinaten[1]; | |||||
System.out.println(); | |||||
int x = 1; | |||||
int y = 1; | |||||
if(playerID == 0){ | if(playerID == 0){ | ||||
image(imgBlue, x, y, particleSize, particleSize); | image(imgBlue, x, y, particleSize, particleSize); |
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.List; | |||||
import java.awt.Point; | |||||
public class ImgAnalyzer { | |||||
private final int CAM = 0; // Kamera-ID (0 für Webcam, 1 für andere Cams) | |||||
private final int EXP = -9; // Belichtungszeit (idealerweise: 0) | |||||
private final int FPS = 20; // FPS | |||||
private final int THV = 252; // Threshold | |||||
private Point currentPositionP1 = new Point(0, 0); | |||||
private Point currentPositionP2 = new Point(0, 0); | |||||
private CamPuffer camPuffer; | |||||
public ImgAnalyzer() { | |||||
camPuffer = new CamPuffer(CAM, EXP, FPS, THV); | |||||
Thread pufferThread = new Thread(camPuffer); | |||||
pufferThread.start(); | |||||
} | |||||
public static void main(String[] args) { | |||||
ImgAnalyzer tracker = new ImgAnalyzer(); | |||||
tracker.run(); | |||||
} | |||||
public void run() { | |||||
boolean hasStartedCapturing = false; | |||||
while (!hasStartedCapturing) { | |||||
if (camPuffer.getCurrentMat() != null) { | |||||
break; | |||||
} | |||||
} | |||||
Mat currentImg = null; | |||||
Mat grayFrame = new Mat(); | |||||
Mat thresholdFrame = new Mat(); | |||||
while (true) { | |||||
try { | |||||
currentImg = camPuffer.getCurrentMat(); | |||||
//HighGui.imshow("Threshold Image", currentImg); | |||||
// ROI anwenden | |||||
//Mat roiFrame = new Mat(downsizedFrame, roi); | |||||
// In Graustufen konvertieren | |||||
Imgproc.cvtColor(currentImg, grayFrame, Imgproc.COLOR_BGR2GRAY); | |||||
// Threshold anwenden | |||||
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); | |||||
} catch (Exception e) { | |||||
} | |||||
if (currentImg != null) { | |||||
List<Point> positions = trackPositions(thresholdFrame); | |||||
if (positions.size() >= 2) { | |||||
currentPositionP1 = positions.get(0); | |||||
currentPositionP2 = positions.get(1); | |||||
//System.out.println("currentPositionP1: " + currentPositionP1 + " currentPositionP2: " + currentPositionP2); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
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)); | |||||
} | |||||
} | |||||
//Check | |||||
return positions; | |||||
} | |||||
public synchronized Point getP1() { | |||||
return currentPositionP1; | |||||
} | |||||
public Point getP2() { | |||||
return currentPositionP2; | |||||
} | |||||
} |
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.List; | |||||
import java.awt.Point; | |||||
public class Tracker implements Runnable { | |||||
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; // Threshold | |||||
private volatile boolean running = true; // Steuerung des Tracker-Threads | |||||
private volatile Point currentPositionP1 = new Point(0, 0); | |||||
private volatile Point currentPositionP2 = new Point(0, 0); | |||||
public Tracker() { | |||||
} | |||||
public static void main(String[] args) { | |||||
Tracker tracker = new Tracker(); | |||||
Thread trackerThread = new Thread(tracker); | |||||
trackerThread.start(); | |||||
// Beispiel: Positionen regelmäßig abfragen | |||||
/* | |||||
while (true) { | |||||
Point p1 = tracker.getP1(); | |||||
Point p2 = tracker.getP2(); | |||||
//System.out.println("P2 - X: " + p2.x + " Y: " + p2.y); | |||||
try { | |||||
Thread.sleep(1000); // Warte 1 Sekunde | |||||
} catch (InterruptedException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
*/ | |||||
} | |||||
@Override | |||||
public 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 (running) { | |||||
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); | |||||
//Check | |||||
if (positions.size() >= 2) { | |||||
setCurrentPositionP1(positions.get(0)); | |||||
//System.out.println(currentPositionP1); | |||||
//Check. currentPosition wird aktualisiert | |||||
setCurrentPositionP2(positions.get(1)); | |||||
//Check | |||||
//System.out.println("P1 - X: " + currentPositionP1.x + " Y: " + currentPositionP1.y + | |||||
// " P2 - X: " + currentPositionP2.x + " Y: " + currentPositionP2.y); | |||||
} | |||||
HighGui.imshow("GTCar", frame); | |||||
if (HighGui.waitKey(1) == 27) { // ESC-Taste zum Beenden | |||||
stop(); | |||||
} | |||||
} else { | |||||
System.out.println("Fehler: Das Kamerabild konnte nicht gelesen werden."); | |||||
break; | |||||
} | |||||
} | |||||
capture.release(); | |||||
} | |||||
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)); | |||||
} | |||||
} | |||||
//Check | |||||
return positions; | |||||
} | |||||
public synchronized Point getP1() { | |||||
//ab hier ist Punkt[0,0] | |||||
return currentPositionP1; | |||||
} | |||||
public synchronized Point getP2() { | |||||
return currentPositionP2; | |||||
} | |||||
private synchronized void setCurrentPositionP1(Point p1) { | |||||
//p1 ist richtig | |||||
currentPositionP1 = p1; | |||||
} | |||||
private synchronized void setCurrentPositionP2(Point p2) { | |||||
currentPositionP2 = p2; | |||||
} | |||||
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; | |||||
} | |||||
public void stop() { | |||||
running = false; | |||||
} | |||||
} |