Browse Source

Luppt

Fehlt noch:
Transformation und Collision
master
Paul-1108 5 days ago
parent
commit
545e803554

+ 49
- 38
.idea/workspace.xml View File

@@ -4,15 +4,12 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Funktionierender Puffer&#10;16.12.2024 15.06&#10;Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu">
<change afterPath="$PROJECT_DIR$/Background.png" afterDir="false" />
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Funktionierender Puffer&#10;Bild wird nicht korrekt gespeichert&#10;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/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/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" />
<change beforePath="$PROJECT_DIR$/src/main/java/Player.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/Player.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -33,41 +30,38 @@
&quot;associatedIndex&quot;: 8
}</component>
<component name="ProjectId" id="2p7Eq8ZJM8BEjLybOnAdhzZ82uv" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"ASKED_ADD_EXTERNAL_FILES": "true",
"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">{
&quot;keyToString&quot;: {
&quot;Application.GTCar.executor&quot;: &quot;Run&quot;,
&quot;Application.GraphicsProgram.executor&quot;: &quot;Run&quot;,
&quot;Application.Main.executor&quot;: &quot;Run&quot;,
&quot;Application.Tracker.executor&quot;: &quot;Run&quot;,
&quot;Downloaded.Files.Path.Enabled&quot;: &quot;false&quot;,
&quot;Repository.Attach.Annotations&quot;: &quot;false&quot;,
&quot;Repository.Attach.JavaDocs&quot;: &quot;false&quot;,
&quot;Repository.Attach.Sources&quot;: &quot;false&quot;,
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;com.codeium.enabled&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;master&quot;,
&quot;kotlin-language-version-configured&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Users/david/Documents/Uni/GTCar/opencv/build/java/opencv-4100.jar&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;project.structure.last.edited&quot;: &quot;Libraries&quot;,
&quot;project.structure.proportion&quot;: &quot;0.15&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.24022989&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
}
}]]></component>
}</component>
<component name="RunManager" selected="Application.Main">
<configuration name="GTCar" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="Tracker" />
@@ -87,7 +81,7 @@
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<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="VM_PARAMETERS" value="-Djava.library.path=&quot;C:\Users\paul3\OneDrive\Dokumente\Studium BME\Semester 3\Interaktion\opencv\build\java\x64&quot;" />
<method v="2">
<option name="Make" enabled="true" />
</method>
@@ -104,6 +98,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">
@@ -116,6 +118,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" />
@@ -165,7 +168,15 @@
<option name="project" value="LOCAL" />
<updated>1734358037207</updated>
</task>
<option name="localTasksCounter" value="7" />
<task id="LOCAL-00007" summary="Funktionierender Puffer&#10;Bild wird nicht korrekt gespeichert&#10;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">
@@ -183,13 +194,13 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<MESSAGE value="First Commit Test" />
<MESSAGE value="Version 20.11" />
<MESSAGE value="Version 23.11 (Grafics update)" />
<MESSAGE value="Version 27.11" />
<MESSAGE value="Version 27.11 (&quot;fertiger&quot; Tracker)" />
<MESSAGE value="Funktionierender Puffer&#10;16.12.2024 15.06&#10;Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" />
<option name="LAST_COMMIT_MESSAGE" value="Funktionierender Puffer&#10;16.12.2024 15.06&#10;Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" />
<MESSAGE value="Funktionierender Puffer&#10;Bild wird nicht korrekt gespeichert&#10;16.12.2024 16:48" />
<option name="LAST_COMMIT_MESSAGE" value="Funktionierender Puffer&#10;Bild wird nicht korrekt gespeichert&#10;16.12.2024 16:48" />
</component>
</project>

+ 1
- 1
src/main/java/CamPuffer.java View File

@@ -11,7 +11,7 @@ public class CamPuffer implements Runnable {

static {
try {
System.load("C:\\Users\\david\\Documents\\Uni\\GTCar\\opencv\\build\\java\\x64\\opencv_java4100.dll");
System.load("C:\\Users\\paul3\\OneDrive\\Dokumente\\Studium BME\\Semester 3\\Interaktion\\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());

+ 1
- 1
src/main/java/GameLoop.java View File

@@ -6,7 +6,7 @@ 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 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 = 245; // Threshold

+ 50
- 21
src/main/java/GraphicsProgram.java View File

@@ -2,11 +2,17 @@ import processing.core.PApplet;
import processing.core.PImage;

import java.util.LinkedList;
import java.util.Queue;

public class GraphicsProgram extends PApplet {

private Queue<int[]> playerTrails = new LinkedList<>();
private LinkedList<int[]> player1Trail = new LinkedList<>(); // Liste für Spieler 1
private LinkedList<int[]> player2Trail = new LinkedList<>(); // Liste für Spieler 2

private final int TRAIL_LENGTH = 15;
private int trailCounter = 17;

private int distanceTh = 100;


PImage img1;
PImage img2;
@@ -31,33 +37,56 @@ public class GraphicsProgram extends PApplet {
public void draw() {
image(backgroundImage, 0, 0); // Hintergrund zeichnen

strokeWeight(20); // Linienstärke festlegen

int particleSizeP1 = 30;
int particleSizeP2 = 30;
// Linien für Spieler 1 zeichnen
stroke(0, 0, 255); // Blau
drawTrail(player1Trail);

// Linien für Spieler 2 zeichnen
stroke(255, 0, 0); // Rot
drawTrail(player2Trail);
}

// Kopie von trailPlayer1 erstellen und durch die Kopie iterieren
for (int[] koordinates : new LinkedList<>(playerTrails)) {
int x1 = koordinates[0];
int y1 = koordinates[1];
int x2 = koordinates[2];
int y2 = koordinates[3];
// Methode, um Linien für eine gegebene Trail-Liste zu zeichnen
private void drawTrail(LinkedList<int[]> trail) {
if (trail.size() < 2) return; // Mindestens 2 Punkte nötig, um Linien zu zeichnen

fill(0, 0, 255);
rect(x1, y1, particleSizeP1, particleSizeP1);
//image(img1, x1, y1, particleSizeP1, particleSizeP1);
for (int i = 0; i < trail.size() - 1; i++) {
int[] current = trail.get(i);
int[] next = trail.get(i + 1);

fill(255, 0, 0);
rect(x2, y2, particleSizeP2, particleSizeP2);
//image(img2, x2, y2, particleSizeP2, particleSizeP2);
// Null-Check für die Punkte
if (current != null && next != null) {
if(current[0]-next[0]> -distanceTh && current[0]-next[0]< distanceTh && current[1]-next[1]> -distanceTh && current[1]-next[1]< distanceTh) {
line(current[0], current[1], next[0], next[1]);
}
}
}
}


// Methode zum Aktualisieren der Trails für beide Spieler
public void updateTrail(int x1, int y1, int x2, int y2) {
if (x1 >= 0 && y1 >= 0) {
addToTrail(player1Trail, x1, y1);
}
if (x2 >= 0 && y2 >= 0) {
addToTrail(player2Trail, x2, y2);
}

}

public void updateTrail(Queue<int[]> trails){
this.playerTrails = trails;
// Methode zum Hinzufügen eines Punktes zur Trail-Liste und Begrenzung auf 20 Einträge
private void addToTrail(LinkedList<int[]> trail, int x, int y) {
if(trailCounter % 21 == 0) {
if (x >= 0 && y >= 0) {
trail.add(new int[]{x, y});
if (trail.size() > TRAIL_LENGTH) {
trail.removeFirst();
}
}
}
trailCounter++;
}
}

}

+ 42
- 17
src/main/java/ImgAnalyzer.java View File

@@ -6,11 +6,8 @@ import org.opencv.imgproc.Moments;
import org.opencv.videoio.Videoio;
import processing.core.PApplet;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.awt.Point;
import java.util.Queue;

public class ImgAnalyzer {

@@ -19,7 +16,7 @@ public class ImgAnalyzer {
private Point currentPositionP1 = new Point(0, 0);
private Point currentPositionP2 = new Point(0, 0);
Queue<int[]> trails = new LinkedList<>();
private final int TRAIL_LENGTH = 200;
private final int TRAIL_LENGTH = 20;

private final CamPuffer camPuffer;
private GraphicsProgram playerGraphics;
@@ -88,11 +85,21 @@ public class ImgAnalyzer {
if(trails.size() > TRAIL_LENGTH){
trails.poll();
}
System.out.println(trails.size());
playerGraphics.updateTrail(trails);
playerGraphics.draw();

playerGraphics.updateTrail(currentPositionP1.x, currentPositionP1.y, currentPositionP2.x, currentPositionP2.y);
//playerGraphics.draw();
}

/*org.opencv.core.Point pp1 = new org.opencv.core.Point(currentPositionP1.x, currentPositionP1.y);
Imgproc.circle(currentImg, pp1, 10, new Scalar(255,0,0), -1);

org.opencv.core.Point pp2 = new org.opencv.core.Point(currentPositionP2.x, currentPositionP2.y);
Imgproc.circle(currentImg, pp2, 10, new Scalar(0,0,255), -1);

if(currentImg.elemSize() != 0) {

HighGui.imshow("el thunfisch", currentImg);

} */
}
}

@@ -103,24 +110,42 @@ public class ImgAnalyzer {
// 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
// Liste für die Positionen und zugehörige Pixelanzahl
List<Point> positions = new ArrayList<>();
List<Double> whitePixelCounts = new ArrayList<>();

for (int i = 0; i < Math.min(contours.size(), 2); i++) {
Moments moments = Imgproc.moments(contours.get(i));
for (MatOfPoint contour : contours) {
Moments moments = Imgproc.moments(contour);
if (moments.get_m00() != 0) {
// Berechne den Mittelpunkt der Kontur
int cx = (int) (moments.get_m10() / moments.get_m00());
int cy = (int) (moments.get_m01() / moments.get_m00());

// Berechne die Anzahl der weißen Pixel in der Kontur
Mat mask = Mat.zeros(thresholdFrame.size(), CvType.CV_8UC1);
Imgproc.drawContours(mask, List.of(contour), 0, new Scalar(255), -1); // Fülle die Kontur mit Weiß
double whitePixels = Core.sumElems(mask).val[0] / 255.0; // Summe der Pixelwerte durch 255 (1 Pixel = 255)

// Speichere Position und Pixelanzahl
positions.add(new Point(cx, cy));
whitePixelCounts.add(whitePixels);
}
}
//Check
return positions;

// Sortiere die Positionen basierend auf der Anzahl weißer Pixel (absteigend)
List<Point> sortedPositions = new ArrayList<>();
while (!whitePixelCounts.isEmpty()) {
int maxIndex = whitePixelCounts.indexOf(Collections.max(whitePixelCounts));
sortedPositions.add(positions.get(maxIndex));
positions.remove(maxIndex);
whitePixelCounts.remove(maxIndex);
}

// Gib die Positionen der zwei größten Konturen zurück
return sortedPositions.subList(0, Math.min(sortedPositions.size(), 2));
}


public synchronized Point getP1() {
return currentPositionP1;
}

Loading…
Cancel
Save