Version 13.12 Fehler: getP1 und getP2 geben 0,0 zurück obwohl bis dahin noch richtige Koordinaten in currentPositionP1 -P2 gespeichert sind
This commit is contained in:
parent
739a7e129c
commit
b05c2a8319
30
.idea/workspace.xml
generated
30
.idea/workspace.xml
generated
@ -4,11 +4,12 @@
|
|||||||
<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 ("fertiger" Tracker)">
|
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Version 04.12 "vollständiges Programm mit Fehlern"">
|
||||||
<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/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/Tracker.java" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
@ -25,7 +26,6 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="Git.Settings">
|
<component name="Git.Settings">
|
||||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||||
<option name="UPDATE_TYPE" value="REBASE" />
|
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectColorInfo">{
|
<component name="ProjectColorInfo">{
|
||||||
"associatedIndex": 8
|
"associatedIndex": 8
|
||||||
@ -39,7 +39,6 @@
|
|||||||
<component name="PropertiesComponent"><![CDATA[{
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"Application.GTCar.executor": "Run",
|
"Application.GTCar.executor": "Run",
|
||||||
"Application.GameLogic.executor": "Run",
|
|
||||||
"Application.GraphicsProgram.executor": "Run",
|
"Application.GraphicsProgram.executor": "Run",
|
||||||
"Application.Main.executor": "Run",
|
"Application.Main.executor": "Run",
|
||||||
"Application.Tracker.executor": "Run",
|
"Application.Tracker.executor": "Run",
|
||||||
@ -52,7 +51,7 @@
|
|||||||
"com.codeium.enabled": "true",
|
"com.codeium.enabled": "true",
|
||||||
"git-widget-placeholder": "master",
|
"git-widget-placeholder": "master",
|
||||||
"kotlin-language-version-configured": "true",
|
"kotlin-language-version-configured": "true",
|
||||||
"last_opened_file_path": "C:/Users/david/Documents/Uni/GTCar/processing-4.3/core/library/core.jar",
|
"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.eslint": "true",
|
||||||
"node.js.detected.package.tslint": "true",
|
"node.js.detected.package.tslint": "true",
|
||||||
"node.js.selected.package.eslint": "(autodetect)",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
@ -73,8 +72,8 @@
|
|||||||
<option name="Make" enabled="true" />
|
<option name="Make" enabled="true" />
|
||||||
</method>
|
</method>
|
||||||
</configuration>
|
</configuration>
|
||||||
<configuration name="GameLogic" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
|
<configuration name="GraphicsProgram" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
|
||||||
<option name="MAIN_CLASS_NAME" value="GameLogic" />
|
<option name="MAIN_CLASS_NAME" value="GraphicsProgram" />
|
||||||
<module name="GTCar_V2_maven" />
|
<module name="GTCar_V2_maven" />
|
||||||
<method v="2">
|
<method v="2">
|
||||||
<option name="Make" enabled="true" />
|
<option name="Make" enabled="true" />
|
||||||
@ -83,14 +82,20 @@
|
|||||||
<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" />
|
||||||
<method v="2">
|
<method v="2">
|
||||||
<option name="Make" enabled="true" />
|
<option name="Make" enabled="true" />
|
||||||
</method>
|
</method>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
<list>
|
||||||
|
<item itemvalue="Application.GTCar" />
|
||||||
|
<item itemvalue="Application.GraphicsProgram" />
|
||||||
|
<item itemvalue="Application.Main" />
|
||||||
|
</list>
|
||||||
<recent_temporary>
|
<recent_temporary>
|
||||||
<list>
|
<list>
|
||||||
<item itemvalue="Application.Main" />
|
<item itemvalue="Application.Main" />
|
||||||
<item itemvalue="Application.GameLogic" />
|
<item itemvalue="Application.GraphicsProgram" />
|
||||||
</list>
|
</list>
|
||||||
</recent_temporary>
|
</recent_temporary>
|
||||||
</component>
|
</component>
|
||||||
@ -105,6 +110,7 @@
|
|||||||
<workItem from="1732108906083" duration="758000" />
|
<workItem from="1732108906083" duration="758000" />
|
||||||
<workItem from="1732113837570" duration="20534000" />
|
<workItem from="1732113837570" duration="20534000" />
|
||||||
<workItem from="1733327375447" duration="951000" />
|
<workItem from="1733327375447" duration="951000" />
|
||||||
|
<workItem from="1734091457440" duration="10746000" />
|
||||||
</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" />
|
||||||
@ -171,14 +177,4 @@
|
|||||||
<MESSAGE value="Version 27.11 ("fertiger" Tracker)" />
|
<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 27.11 ("fertiger" Tracker)" />
|
||||||
</component>
|
</component>
|
||||||
<component name="XDebuggerManager">
|
|
||||||
<breakpoint-manager>
|
|
||||||
<breakpoints>
|
|
||||||
<breakpoint enabled="true" type="java-exception">
|
|
||||||
<properties class="java.lang.NullPointerException" package="java.lang" />
|
|
||||||
<option name="timeStamp" value="1" />
|
|
||||||
</breakpoint>
|
|
||||||
</breakpoints>
|
|
||||||
</breakpoint-manager>
|
|
||||||
</component>
|
|
||||||
</project>
|
</project>
|
@ -1,5 +1,4 @@
|
|||||||
import processing.core.PApplet;
|
import processing.core.PApplet;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
@ -10,30 +9,45 @@ public class GameLogic extends PApplet {
|
|||||||
PApplet.main("GameLogic");
|
PApplet.main("GameLogic");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Queue<int[]> trail = new LinkedList<>();
|
||||||
|
private Point testPoint = new Point(200, 200);
|
||||||
|
|
||||||
public void settings() {
|
public void settings() {
|
||||||
size(800, 600);
|
size(800, 600);
|
||||||
noLoop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean doCheck(Queue<int[]> trail, Point point){
|
public void setup() {
|
||||||
|
noLoop(); // Zeichnen nur bei Bedarf
|
||||||
|
fillTrail(); // Beispielpfad erstellen
|
||||||
|
//boolean collision = doCheck(trail, testPoint);
|
||||||
|
System.out.println("Collision detected: ");
|
||||||
|
}
|
||||||
|
|
||||||
int particleSize = trail.size()+5;
|
private void fillTrail() {
|
||||||
|
// Beispielhafte Füllung der Spur
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
trail.add(new int[]{50 + i * 5, 300}); // Punkte entlang der x-Achse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
background(255, 255, 255);
|
public boolean doCheckExtern(Queue<int[]> trail, Point point){
|
||||||
|
return doCheck(trail, point);
|
||||||
|
}
|
||||||
|
|
||||||
for (int[] koordinates : new LinkedList<>(trail)) {
|
public boolean doCheck(Queue<int[]> trail, Point point) {
|
||||||
int x = koordinates[0];
|
int particleSize = 10;
|
||||||
int y = koordinates[1];
|
|
||||||
fill(0,0,0);
|
// Zeichne den Pfad auf das Canvas
|
||||||
|
background(255); // Weißer Hintergrund
|
||||||
|
for (int[] coordinates : new LinkedList<>(trail)) {
|
||||||
|
int x = coordinates[0];
|
||||||
|
int y = coordinates[1];
|
||||||
|
fill(0); // Schwarz
|
||||||
rect(x, y, particleSize, particleSize);
|
rect(x, y, particleSize, particleSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
int c = get(point.x, point.y);
|
// Überprüfe die Pixelwerte am Testpunkt
|
||||||
if (c==0){
|
int c = get(point.x, point.y); // Farbwert abrufen
|
||||||
return true; //Kollision erkannt
|
return (c == color(0)); // Schwarz bedeutet Kollision
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,30 +18,47 @@ public class GameLoop {
|
|||||||
public GameLoop(){
|
public GameLoop(){
|
||||||
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();
|
|
||||||
|
|
||||||
graphics = new GraphicsProgram();
|
tracker = new Tracker();
|
||||||
|
// Starte Tracker in einem separaten Thread
|
||||||
|
Thread trackerThread = new Thread(tracker::run);
|
||||||
|
trackerThread.start();
|
||||||
|
|
||||||
|
graphics = new GraphicsProgram(player1, player2);
|
||||||
String[] args = {"GraphicsProgram"};
|
String[] args = {"GraphicsProgram"};
|
||||||
PApplet.runSketch(args, graphics);
|
PApplet.runSketch(args, graphics);
|
||||||
|
|
||||||
|
|
||||||
gl = new GameLogic();
|
gl = new GameLogic();
|
||||||
String[] args_ = {"GameLogic"};
|
String[] args_ = {"GameLogic"};
|
||||||
PApplet.runSketch(args_, gl);
|
PApplet.runSketch(args_, gl);
|
||||||
|
try {
|
||||||
|
Thread.sleep(5000);
|
||||||
|
}catch (Exception e){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
run();
|
run();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void run(){
|
private void run(){
|
||||||
while(true){
|
while(true){
|
||||||
communicate();
|
//communicate();
|
||||||
|
|
||||||
List<Point> positions = track();
|
Point p1 = tracker.getP1();
|
||||||
|
//System.out.println("Player 1 is at " + p1.x + " and " + p1.y);
|
||||||
|
Point p2 = tracker.getP2();
|
||||||
|
//System.out.println("Player 2 is at " + p2.x + " and " + p2.y);
|
||||||
|
|
||||||
if(gl.doCheck(player2.getTrail(), positions.get(0))){ //Check Position von P1 mit Spur von P2
|
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
|
||||||
System.out.println("Spieler 1 hat verloren");
|
System.out.println("Spieler 1 hat verloren");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(gl.doCheck(player1.getTrail(), positions.get(1))){ //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;
|
||||||
}
|
}
|
||||||
@ -54,9 +71,10 @@ public class GameLoop {
|
|||||||
//offen für Auto Kommunikation
|
//offen für Auto Kommunikation
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Point> track(){
|
/*private List<Point> track(){
|
||||||
Point p1 = tracker.getP1();
|
Point p1 = tracker.getP1();
|
||||||
Point p2 = tracker.getP2();
|
Point p2 = tracker.getP2();
|
||||||
|
System.out.println(p1.x);
|
||||||
|
|
||||||
player1.setKoords(p1.x, p1.y);
|
player1.setKoords(p1.x, p1.y);
|
||||||
player2.setKoords(p2.x, p2.y);
|
player2.setKoords(p2.x, p2.y);
|
||||||
@ -65,10 +83,9 @@ public class GameLoop {
|
|||||||
output.add(0, p1);
|
output.add(0, p1);
|
||||||
output.add(1, p2);
|
output.add(1, p2);
|
||||||
return output;
|
return output;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
private void draw(){
|
private void draw(){
|
||||||
graphics.drawTrail(player1.getTrail(), 0);
|
|
||||||
graphics.drawTrail(player2.getTrail(), 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,27 +9,52 @@ public class GraphicsProgram extends PApplet {
|
|||||||
PImage imgBlue;
|
PImage imgBlue;
|
||||||
PImage imgRed;
|
PImage imgRed;
|
||||||
|
|
||||||
|
Player player1;
|
||||||
|
Player player2;
|
||||||
|
|
||||||
|
private final int TRAIL_LENGTH = 30;
|
||||||
|
|
||||||
|
public GraphicsProgram(Player player1, Player player2) {
|
||||||
|
this.player1 = player1; // Beispiel: Initialgröße setzen
|
||||||
|
this.player2 = player2;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
PApplet.main("GraficsProgram");
|
PApplet.main("GraphicsProgram");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void settings() {
|
public void settings() {
|
||||||
size(800, 600);
|
fullScreen();
|
||||||
imgBlue = loadImage("TrailBlue");
|
|
||||||
imgRed = loadImage("TrailRed");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setup(){
|
||||||
|
imgBlue = loadImage("TrailBlue.png");
|
||||||
|
imgRed = loadImage("TrailRed.png");
|
||||||
|
background(0);
|
||||||
|
}
|
||||||
|
|
||||||
public void drawTrail(Queue<int[]> trail, int playerID){
|
public void draw(){
|
||||||
|
drawPlayer(0);
|
||||||
|
drawPlayer(1);
|
||||||
|
}
|
||||||
|
public void drawPlayer(int playerID){
|
||||||
|
|
||||||
int particleSize = 5;
|
int particleSize = 5;
|
||||||
|
|
||||||
|
Queue<int[]> trail;
|
||||||
|
|
||||||
background(255);
|
if(playerID == 0){
|
||||||
|
trail = player1.getTrail();
|
||||||
|
} else{
|
||||||
|
trail = player2.getTrail();
|
||||||
|
}
|
||||||
|
|
||||||
for (int[] koordinates : new LinkedList<>(trail)) {
|
while(!trail.isEmpty()) {
|
||||||
int x = koordinates[0];
|
int[] koordinaten = trail.poll();
|
||||||
int y = koordinates[1];
|
int x = koordinaten[0];
|
||||||
|
int y = koordinaten[1];
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
if(playerID == 0){
|
if(playerID == 0){
|
||||||
image(imgBlue, x, y, particleSize, particleSize);
|
image(imgBlue, x, y, particleSize, particleSize);
|
||||||
|
@ -6,6 +6,8 @@ public class Player {
|
|||||||
private final int ID;
|
private final int ID;
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
|
|
||||||
|
private String name = "Thunfisch";
|
||||||
Queue<int[]> trail = new LinkedList<>();
|
Queue<int[]> trail = new LinkedList<>();
|
||||||
|
|
||||||
ArduinoCommunication arduinoCommunicator;
|
ArduinoCommunication arduinoCommunicator;
|
||||||
@ -37,11 +39,11 @@ public class Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addToTrail(int x, int y) {
|
private void addToTrail(int x, int y) {
|
||||||
this.trail.add(new int[]{x, y});
|
trail.add(new int[]{x, y});
|
||||||
|
|
||||||
if(trail.size() > TRAIL_LENGTH){
|
if(trail.size() > TRAIL_LENGTH){
|
||||||
trail.poll();
|
trail.poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Queue<int[]> getTrail(){
|
public Queue<int[]> getTrail(){
|
||||||
|
@ -5,32 +5,48 @@ 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 java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Queue;
|
import java.awt.Point;
|
||||||
import java.util.Comparator;
|
|
||||||
|
|
||||||
|
public class Tracker implements Runnable {
|
||||||
public class Tracker {
|
|
||||||
|
|
||||||
private final int CAM = 1; // 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 = 0; // 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 final int THV = 250; // Threshold
|
||||||
|
|
||||||
private Point currentPositionP1 = new Point(0, 0);
|
private volatile boolean running = true; // Steuerung des Tracker-Threads
|
||||||
private Point currentPositionP2 = new Point(0, 0);
|
|
||||||
|
|
||||||
public Tracker() {}
|
private volatile Point currentPositionP1 = new Point(0, 0);
|
||||||
|
private volatile 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();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
private void run() {
|
@Override
|
||||||
|
public void run() {
|
||||||
VideoCapture capture = setUp();
|
VideoCapture capture = setUp();
|
||||||
|
|
||||||
if (!capture.isOpened()) {
|
if (!capture.isOpened()) {
|
||||||
@ -42,35 +58,39 @@ public class Tracker {
|
|||||||
Mat grayFrame = new Mat();
|
Mat grayFrame = new Mat();
|
||||||
Mat thresholdFrame = new Mat();
|
Mat thresholdFrame = new Mat();
|
||||||
|
|
||||||
while (true) {
|
while (running) {
|
||||||
if (capture.read(frame)) {
|
if (capture.read(frame)) {
|
||||||
|
|
||||||
|
|
||||||
// Konvertiere in Graustufen und wende Threshold an
|
// Konvertiere in Graustufen und wende Threshold an
|
||||||
Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
|
Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
|
||||||
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY);
|
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY);
|
||||||
|
|
||||||
// Tracke die Positionen der beiden größten Objekte
|
// Tracke die Positionen der beiden größten Objekte
|
||||||
List<Point> positions = trackPositions(thresholdFrame);
|
List<Point> positions = trackPositions(thresholdFrame);
|
||||||
|
//Check
|
||||||
|
|
||||||
if (positions.size() >= 2) {
|
if (positions.size() >= 2) {
|
||||||
currentPositionP1 = positions.get(0);
|
setCurrentPositionP1(positions.get(0));
|
||||||
currentPositionP2 = positions.get(1);
|
//System.out.println(currentPositionP1);
|
||||||
|
//Check. currentPosition wird aktualisiert
|
||||||
System.out.println("P1 - X: " + currentPositionP1.x + " Y: " + currentPositionP1.y + " P2 - X: " + currentPositionP2.x + " Y: " + currentPositionP2.y);
|
setCurrentPositionP2(positions.get(1));
|
||||||
|
//Check
|
||||||
|
|
||||||
|
//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);
|
||||||
|
|
||||||
if (HighGui.waitKey(1) == 27) {
|
if (HighGui.waitKey(1) == 27) { // ESC-Taste zum Beenden
|
||||||
break;
|
stop();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Fehler: Das Kamerabild konnte nicht gelesen werden.");
|
System.out.println("Fehler: Das Kamerabild konnte nicht gelesen werden.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
capture.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Point> trackPositions(Mat thresholdFrame) {
|
private List<Point> trackPositions(Mat thresholdFrame) {
|
||||||
@ -83,7 +103,6 @@ public class Tracker {
|
|||||||
// Sortiere Konturen nach Fläche (absteigend)
|
// Sortiere Konturen nach Fläche (absteigend)
|
||||||
contours.sort((c1, c2) -> Double.compare(Imgproc.contourArea(c2), Imgproc.contourArea(c1)));
|
contours.sort((c1, c2) -> Double.compare(Imgproc.contourArea(c2), Imgproc.contourArea(c1)));
|
||||||
|
|
||||||
|
|
||||||
// Speichere die Positionen der zwei größten Konturen
|
// Speichere die Positionen der zwei größten Konturen
|
||||||
List<Point> positions = new ArrayList<>();
|
List<Point> positions = new ArrayList<>();
|
||||||
|
|
||||||
@ -95,37 +114,27 @@ public class Tracker {
|
|||||||
positions.add(new Point(cx, cy));
|
positions.add(new Point(cx, cy));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Check
|
||||||
return positions;
|
return positions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getP1(){
|
public synchronized Point getP1() {
|
||||||
|
//ab hier ist Punkt[0,0]
|
||||||
return currentPositionP1;
|
return currentPositionP1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getP2(){
|
public synchronized Point getP2() {
|
||||||
return currentPositionP2;
|
return currentPositionP2;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawMarkers(Mat frame) {
|
private synchronized void setCurrentPositionP1(Point p1) {
|
||||||
// Größe der Quadrate
|
//p1 ist richtig
|
||||||
int size = 10;
|
currentPositionP1 = p1;
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 synchronized void setCurrentPositionP2(Point p2) {
|
||||||
|
currentPositionP2 = p2;
|
||||||
|
}
|
||||||
|
|
||||||
private VideoCapture setUp() {
|
private VideoCapture setUp() {
|
||||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||||
@ -140,4 +149,8 @@ public class Tracker {
|
|||||||
|
|
||||||
return capture;
|
return capture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user