Bild Übergabe Check Noch offen: Merge mit Bildanalyse und Zeichnen 16.12.2024 21:35master
@@ -4,12 +4,10 @@ | |||
<option name="autoReloadType" value="SELECTIVE" /> | |||
</component> | |||
<component name="ChangeListManager"> | |||
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Funktionierender Puffer 16.12.2024 15.06 Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu"> | |||
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> | |||
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Funktionierender Puffer Bild wird nicht korrekt gespeichert 16.12.2024 16:48"> | |||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> | |||
<change beforePath="$PROJECT_DIR$/src/main/java/CamPuffer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/CamPuffer.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/ImgAnalyzer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ImgAnalyzer.java" afterDir="false" /> | |||
</list> | |||
<option name="SHOW_DIALOG" value="false" /> | |||
@@ -99,6 +97,14 @@ | |||
</list> | |||
</recent_temporary> | |||
</component> | |||
<component name="SharedIndexes"> | |||
<attachedChunks> | |||
<set> | |||
<option value="bundled-jdk-9823dce3aa75-28b599e66164-intellij.indexing.shared.core-IU-242.23726.103" /> | |||
<option value="bundled-js-predefined-d6986cc7102b-5c90d61e3bab-JavaScript-IU-242.23726.103" /> | |||
</set> | |||
</attachedChunks> | |||
</component> | |||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> | |||
<component name="TaskManager"> | |||
<task active="true" id="Default" summary="Default task"> | |||
@@ -111,6 +117,7 @@ | |||
<workItem from="1732113837570" duration="20534000" /> | |||
<workItem from="1733327375447" duration="951000" /> | |||
<workItem from="1734091457440" duration="10746000" /> | |||
<workItem from="1734364915397" duration="9202000" /> | |||
</task> | |||
<task id="LOCAL-00001" summary="First Commit Test"> | |||
<option name="closed" value="true" /> | |||
@@ -160,7 +167,15 @@ | |||
<option name="project" value="LOCAL" /> | |||
<updated>1734358037207</updated> | |||
</task> | |||
<option name="localTasksCounter" value="7" /> | |||
<task id="LOCAL-00007" summary="Funktionierender Puffer Bild wird nicht korrekt gespeichert 16.12.2024 16:48"> | |||
<option name="closed" value="true" /> | |||
<created>1734364131101</created> | |||
<option name="number" value="00007" /> | |||
<option name="presentableId" value="LOCAL-00007" /> | |||
<option name="project" value="LOCAL" /> | |||
<updated>1734364131101</updated> | |||
</task> | |||
<option name="localTasksCounter" value="8" /> | |||
<servers /> | |||
</component> | |||
<component name="TypeScriptGeneratedFilesManager"> | |||
@@ -184,6 +199,7 @@ | |||
<MESSAGE value="Version 27.11" /> | |||
<MESSAGE value="Version 27.11 ("fertiger" Tracker)" /> | |||
<MESSAGE value="Funktionierender Puffer 16.12.2024 15.06 Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" /> | |||
<option name="LAST_COMMIT_MESSAGE" value="Funktionierender Puffer 16.12.2024 15.06 Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" /> | |||
<MESSAGE value="Funktionierender Puffer Bild wird nicht korrekt gespeichert 16.12.2024 16:48" /> | |||
<option name="LAST_COMMIT_MESSAGE" value="Funktionierender Puffer Bild wird nicht korrekt gespeichert 16.12.2024 16:48" /> | |||
</component> | |||
</project> |
@@ -1,4 +1,5 @@ | |||
import org.opencv.core.*; | |||
import org.opencv.imgcodecs.Imgcodecs; | |||
import org.opencv.videoio.VideoCapture; | |||
import org.opencv.imgproc.Imgproc; | |||
import org.opencv.highgui.HighGui; | |||
@@ -50,6 +51,8 @@ public class CamPuffer implements Runnable { | |||
} | |||
Mat frame = new Mat(); | |||
capture.read(frame); | |||
hasStartedCapturing = true; | |||
long lastGcTime = System.currentTimeMillis(); | |||
@@ -58,19 +61,15 @@ public class CamPuffer implements Runnable { | |||
while (running) { | |||
//long startTime = System.currentTimeMillis(); | |||
capture.read(frame); // Bild einlesen | |||
currentMat = frame; | |||
if (frame.empty()) { | |||
System.out.println("Fehler: Kein Bild von der Kamera erhalten."); | |||
break; | |||
} | |||
// Bildverarbeitung hier | |||
updateCurrentMat(frame); | |||
// 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 | |||
@@ -80,26 +79,29 @@ public class CamPuffer implements Runnable { | |||
} | |||
} finally { | |||
// Matrizen explizit freigeben | |||
frame.release(); // Falls `frame` in der Schleife freigegeben wurde, hier nochmal | |||
capture.release(); // Kamera-Ressourcen freigeben | |||
frame.release(); | |||
capture.release(); | |||
} | |||
} | |||
private void updateCurrentMat(Mat img) { | |||
// 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 | |||
public void updateCurrentMat(Mat img) { | |||
Mat frameCopy = img.clone(); | |||
synchronized (frameBuffer) { | |||
if (frameBuffer.size() >= MAX_BUFFER_SIZE) { | |||
frameBuffer.poll().release(); // Ältesten Frame entfernen | |||
} | |||
// Neues Bild in den Puffer hinzufügen | |||
frameBuffer.add(img.clone()); | |||
currentMat = img; | |||
frameBuffer.add(frameCopy); // Neuen Frame hinzufügen | |||
currentMat = frameCopy; | |||
} | |||
} | |||
public Mat getCurrentMat() { | |||
return currentMat; | |||
synchronized (frameBuffer) { | |||
if (!frameBuffer.isEmpty()) { | |||
return frameBuffer.pollLast().clone(); // Rückgabe des neuesten Frames | |||
} | |||
} | |||
return new Mat(); // Rückgabe eines leeren Frames, falls der Puffer leer ist | |||
} | |||
@@ -6,7 +6,13 @@ import java.util.List; | |||
public class GameLoop { | |||
int activePlayer = 0; | |||
private final int CAM = 1; // 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 = 245; // Threshold | |||
ImgAnalyzer tracker; | |||
CamPuffer camPuffer; | |||
GraphicsProgram graphics; | |||
@@ -19,8 +25,11 @@ public class GameLoop { | |||
player1 = new Player(0, "192.168.1.1", 9000); | |||
player2 = new Player(1, "192.168.1.2", 9000); | |||
tracker = new ImgAnalyzer(); | |||
// Starte Tracker in einem separaten Thread | |||
camPuffer = new CamPuffer(CAM, EXP, FPS, THV); | |||
Thread pufferThread = new Thread(camPuffer); | |||
pufferThread.start(); | |||
tracker = new ImgAnalyzer(camPuffer, THV); | |||
Thread trackerThread = new Thread(tracker::run); | |||
trackerThread.start(); | |||
@@ -32,17 +41,12 @@ public class GameLoop { | |||
gl = new GameLogic(); | |||
String[] args_ = {"GameLogic"}; | |||
PApplet.runSketch(args_, gl); */ | |||
try { | |||
Thread.sleep(5000); | |||
}catch (Exception e){ | |||
} | |||
run(); | |||
} | |||
private void run(){ | |||
/*while(true){ | |||
while(true){ | |||
//communicate(); | |||
Point p1 = tracker.getP1(); | |||
@@ -50,7 +54,7 @@ public class GameLoop { | |||
Point p2 = tracker.getP2(); | |||
//System.out.println("Player 2 is at " + p2.x + " and " + p2.y); | |||
player1.setKoords(p1.x, p1.y); | |||
/*player1.setKoords(p1.x, p1.y); | |||
player2.setKoords(p2.x, p2.y); | |||
if(gl.doCheckExtern(player2.getTrail(), p1)){ //Check Position von P1 mit Spur von P2 | |||
@@ -61,10 +65,10 @@ public class GameLoop { | |||
if(gl.doCheckExtern(player1.getTrail(), p2)){ //Check Position von P2 mit Spur von P1 | |||
System.out.println("Spieler 2 hat verloren"); | |||
break; | |||
} | |||
} */ | |||
draw(); | |||
} */ | |||
//draw(); | |||
} | |||
} | |||
private void communicate() { |
@@ -11,64 +11,70 @@ import java.awt.Point; | |||
public class ImgAnalyzer { | |||
private final int CAM = 1; // 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 = 245; // Threshold | |||
private final int THV; // Threshold | |||
private Point currentPositionP1 = new Point(0, 0); | |||
private Point currentPositionP2 = new Point(0, 0); | |||
private CamPuffer camPuffer = new CamPuffer(CAM, EXP, FPS, THV);; | |||
private final CamPuffer camPuffer; | |||
public ImgAnalyzer() { | |||
Thread pufferThread = new Thread(camPuffer); | |||
pufferThread.start(); | |||
public ImgAnalyzer(CamPuffer cP, int threshold) { | |||
THV = threshold; | |||
camPuffer = cP; | |||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |||
} | |||
public static void main(String[] args) { | |||
ImgAnalyzer tracker = new ImgAnalyzer(); | |||
tracker.run(); | |||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |||
} | |||
public void run() { | |||
boolean hasStartedCapturing = false; | |||
while (!hasStartedCapturing) { | |||
if (camPuffer.getCurrentMat() != null) { | |||
break; | |||
while (camPuffer.getCurrentMat().elemSize() == 0) { | |||
System.out.println("Warten auf das erste Bild..."); | |||
try { | |||
Thread.sleep(3000); // Kurze Pause, um CPU-Belastung zu vermeiden | |||
} catch (InterruptedException e) { | |||
Thread.currentThread().interrupt(); | |||
return; | |||
} | |||
} | |||
Mat currentImg = null; | |||
Mat currentImg = new Mat(); | |||
Mat grayFrame = new Mat(); | |||
Mat thresholdFrame = new Mat(); | |||
int debug = 0; | |||
while (true) { | |||
try { | |||
currentImg = camPuffer.getCurrentMat(); | |||
try { | |||
currentImg = camPuffer.getCurrentMat(); | |||
// In Graustufen konvertieren | |||
Imgproc.cvtColor(currentImg, grayFrame, Imgproc.COLOR_BGR2GRAY); | |||
if(currentImg.elemSize() != 0) { | |||
// Threshold anwenden | |||
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); | |||
// In Graustufen konvertieren | |||
Imgproc.cvtColor(currentImg, grayFrame, Imgproc.COLOR_BGR2GRAY); | |||
} catch (Exception e) { | |||
// Threshold anwenden | |||
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); | |||
} | |||
//Ausgabe des Bildes zum Prüfen | |||
HighGui.imshow("ThunfischHighGui", thresholdFrame); | |||
HighGui.waitKey(1); | |||
//Ende Bildausgabe */ | |||
} | |||
if (currentImg != null) { | |||
List<Point> positions = trackPositions(thresholdFrame); | |||
} catch (Exception e) { | |||
if (positions.size() >= 2) { | |||
currentPositionP1 = positions.get(0); | |||
currentPositionP2 = positions.get(1); | |||
System.out.println("currentPositionP1: " + currentPositionP1 + " currentPositionP2: " + currentPositionP2); | |||
} | |||
} | |||
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); | |||
} | |||
} | |||
} | |||
} | |||