Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

skriptsose2020:code-beispiele_aus_den_vorlesungnsvideos_sose2020

Code-Beispiele aus den Vorlesungsvideos SoSe2020

Woche 2: Rekapitulation Woche 1 und Start in die Woche 2

Strings

Strings

int counter = 1; // counts how many times draw() has run
void setup() {
}
 
void draw() {
  String word1 = "Hallo";
  String word2 = "Person";
  System.out.println(word1 + " " + word2 + " " + counter); // alternative to println()
  counter++;
}

Konstanten

Konstanten

int pos = 0; // coordinates of the circle center
final int SIZE_CIRCLE = 100; // circle diameter
 
void setup() {
  size(600, 400);
}
 
void draw() {
  background(200);
  circle(pos, pos, SIZE_CIRCLE);
  pos++;
}


Woche 2: Arrays

Code

Code

int punkteStud1 = 11;
int punkteStud2 = 9;
int punkteStud3 = 8;
int punkteStud4 = 12;
//...
 
//Arrays anlegen
//Variante 1
int[] punkteStudVar1 = new int[4];
punkteStudVar1[0] = 11;
punkteStudVar1[1] = 9;
punkteStudVar1[2] = 8;
punkteStudVar1[3] = 12;
 
//Variante 2
int[] punkteStudVar2 = {11,9,8,12};
 
//Werte ändern und auslesen
punkteStudVar2[2] = 10;
 
punkteStudVar1[2] = punkteStudVar2[2];
 
//Länge von Arrays
//!Wichtig! Arrays fangen bei 0 an
int arrayLange = punkteStudVar2.length;
println(arrayLaenge);
 
//Zweidimensionale Arrays
int[][] StudentenHA = new int[4][2];
StudentenHA[0][0] = 11;
StudentenHA[1][0] = 9;
StudentenHA[2][0] = 8;
StudentenHA[3][0] = 12;
 
StudentenHA[0][1] = 8;
StudentenHA[1][1] = 13;
StudentenHA[2][1] = 15;
StudentenHA[3][1] = 9;


Woche 2: Schleifen

while und for

while und for

void setup(){
 
  int x=0;
  int y=-10;
  while (x>y){
    y++;
    print (y+", ");
  }
 
  println();
 
 
  for (int i = 0; i<20; i++){
    print (i +", ");
  }
 
}
 
void draw(){
}

Beispiel mit Arrays

Beispiel mit Arrays

int[] intArray0 = {0,1,2,3,4,5,6,7,8,9}; //Deklaration und initialisierung von intArray0, alle Werte sind fest.
 
int[] intArray2 = new int[10];//Deklaration und initialisierung von intArray2
 
void setup(){
 
 
  intArray2[9] = 23;
 
  println("Länge von intArray0: "+intArray0.length);
  println("Länge von intArray2: "+intArray2.length);
 
 
 
 
 
  print("intArray0:["); // Gebe "intArray0:[" aus (ohne Zeilenumbruch)
 
  int counter = 0; // Lege Variable counter zum durchgehen des Arrays-0 an
 
  while (counter < intArray0.length){ //Solange counter kleiner ist als die Arraylänge tue:
 
    print(intArray0[counter]+", "); //Gebe den Arrayeintrag an der Stelle "counter" aus.
    counter++; //Erhoehe counter um 1.
  }
 
  println("]");
 
 
  print("intArray2:[");
  for ( int i = 0; i < intArray2.length; i++){
    print(intArray2[i]+", ");
  }
  println("]");
 
 
}
 
 
void  draw(){
}

Beispiel mit zweidimensionalen Arrays

Beispiel mit zweidimensionalen Arrays

int[][] intArray0 = {{0,1,2,3,4,5,6,7,8,9,10},{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},
{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},
{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9}}; 
//Deklaration und initialisierung von intArray0
 
int[][] intArray2 = new int[10][10];//Deklaration und initialisierung von intArray2
 
void setup(){
 
   //setze einige werte
  intArray2[9][2] = 92;  
  intArray2[9][9] = 99;
  intArray2[1][1] = 11;
 
  //gebe längen der arrays aus 
  println("Länge von intArray0: "+intArray0.length);
  println("Länge von intArray2: "+intArray2.length);
  println();
  println();
 
 
  /*Gebe das gesamte 2D Array intArray0 aus.*/
 
  int zeile = 0; // Lege Variable counter zum durchgehen des Arrays-0 an
  while (zeile < intArray0.length){ //Solange counter kleiner ist als die Arraylänge tue:
 
    int spalte= 0;
    while (spalte< intArray0[zeile].length){
       print(intArray0[zeile][spalte] + " "); //Gebe den Arrayeintrag an der stelle [zeile][spalte] aus.
       spalte++; //Erhoehe spalte um 1.
    }
 
    println();
    zeile++; //Erhoehe zeile um 1.
  }
 
 
  //Zeilenumbrüche für die übersichtlichkeit
  println();
  println();
 
 
 
  /*Gebe das gesamte 2D Array intArray2 aus.*/
 
  for ( int z = 0; z < intArray2.length; z++){ // z gibt die zeile an.
 
    for (int s = 0; s <intArray2[z].length; s++){ //s gibt die Spalte an.
      print(intArray2[z][s] + "\t"); // Gebe den Eintrag [z][s] aus.
    }
 
    println();
  }
 
 
}
 
 
void  draw(){
 
}


Woche 2: Methoden

Beispeiele von Methoden

Beispeiele von Methoden

void setup() {
  boolean result = isEven(10);
}
 
void draw() {
  int[] pointerCoords= getCoords();
  println(pointerCoords[0] + " " + pointerCoords[1]);
}
 
// Returns the x and y of the mouse 
int[] getCoords() {
  int[] coords = {mouseX, mouseY};
  return coords;
}
 
// Determines if the given number if even. Returns true if even, false otherwise.
boolean isEven(int number) {
  if (number%2==0) {
    return true;
  } else {
    return false;
  }
}
 
// Returns true if the first number in array if greater than the second number, false otherwise. The array should have the length = 2 
boolean isGreater(int[] numbers) {
  if (numbers[0] > numbers[1]) {
    return true;
  } else {
    return false;
  }
}
 
// Calculates the sum of two numbers
int sumNumbers(int number1, int number2) {
  int result = number1 + number2;
  return result;
}
 
// Prints a greeting according to the person's index
void greet(int index) {
  println("Hallo Person " + index);
}
 
// Prints a greeting
void greet() {
  println("Hallo Welt");
}


Woche 3: Steuerung eines zweirädigen Roboters

Über globale Variablen

Über globale Variablen

float posX = 300; // x coordinate of the circle center
float posY = 200; // y coordinate of the circle center
float angle = PI/4; // current direction of the robot
final int RADIUS = 50; // radius of the robot
 
 
void setup() {
  size(600, 400);
}
 
void draw() {
  background(200);
  show();
  move(4);
  turn(0.05);
}
 
// Turns the robot by the given angle value
void turn(float angleDiff) {
  angle = angle + angleDiff;
}
 
// Moves the robot forward by the given distance
void move(float distance) {
  posX = posX + distance * cos(angle);
  posY = posY + distance * sin(angle);
}
 
// Draws the robot
void show() {
  circle(posX, posY, RADIUS*2); 
  line(posX, posY, posX + RADIUS * cos(angle), posY + RADIUS * sin(angle));
}


Woche 3: Klassen

Haupt-Sketch

Haupt-Sketch

Robot robot1; // large robot
Robot robot2; // small robot
 
void setup() {
  size(600, 400);
  robot1 = new Robot(300, 200, PI/4, 50); 
  robot2 = new Robot(500, 200, PI, 20);
}
 
void draw() {
  background(200);
  robot1.show();
  robot2.show();
  robot1.move(4);
  robot1.turn(0.05);
  robot2.move(2);
  float distance = robot1.calculateDistance(robot2);
  if (distance <= 0) { 
  // dies ist anders als im Vorlesungs-Video, weil die Radienlängen in der Methode calculateDistance() abgezogen werden
    robot1.turn(PI);
    robot2.turn(PI);
  }
}

Klasse Robot

Klasse Robot

class Robot {
 
  // 1. Attribute
  float posX; // x coordinate of the circle center
  float posY; // y coordinate of the circle center
  float angle; // current direction of the robot
  int radius; // radius of the robot
 
 
  // 2. Konstruktor, x: x coordinate of the center, y: y coordinate of the center, direction: where the roboter is looking towards, size: radius of the robot
  Robot(float posX, float posY, float direction, int size) {
    this.posX = posX;
    this.posY = posY;
    this.angle = direction;
    this.radius = size;
  }
 
  // 3. Methoden
  // Turns the robot by the given angle value
  void turn(float angleDiff) {
    this.angle = this.angle + angleDiff;
  }
 
  // Moves the robot forward by the given distance
  void move(float distance) {
    posX = posX + distance * cos(angle);
    posY = posY + distance * sin(angle);
  }
 
  // Draws the robot
  void show() {
    circle(posX, posY, radius*2); 
    line(posX, posY, posX + radius * cos(angle), posY + radius * sin(angle));
  }
 
  // Calculates distance from this robot to the other
  float calculateDistance(Robot other) {
    float deltaX = this.posX - other.posX;
    float deltaY = this.posY - other.posY;
    float distance = sqrt(deltaX*deltaX + deltaY*deltaY);
    return distance - this.radius - other.radius; // dies ist anders als im Vorlesungs-Video
  }
}


Woche 4: Bibliotheken

Video-Bibliothek: Abzug des Hintergrunds

Video-Bibliothek: Abzug des Hintergrunds

/**
 * Background Subtraction 
 * by Golan Levin. 
 *
 * Detect the presence of people and objects in the frame using a simple
 * background-subtraction technique. To initialize the background, press a key.
 */
 
 
import processing.video.*;
 
int numPixels;
int[] backgroundPixels;
Capture video;
 
void setup() {
  size(640, 480); 
 
  // This the default video input, see the GettingStartedCapture 
  // example if it creates an error
  //video = new Capture(this, 160, 120);
  String[] cameras = Capture.list();
  video = new Capture(this, cameras[0]);
 
  // Start capturing the images from the camera
  video.start();  
 
  numPixels = video.width * video.height;
  // Create array to store the background image
  backgroundPixels = new int[numPixels];
  // Make the pixels[] array available for direct manipulation
  loadPixels();
}
 
void draw() {
  if (video.available()) {
    video.read(); // Read a new video frame
    video.loadPixels(); // Make the pixels of video available
    // Difference between the current frame and the stored background
    int presenceSum = 0;
    for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
      // Fetch the current color in that location, and also the color
      // of the background in that spot
      color currColor = video.pixels[i];
      color bkgdColor = backgroundPixels[i];
      // Extract the red, green, and blue components of the current pixel's color
      int currR = (currColor >> 16) & 0xFF;
      int currG = (currColor >> 8) & 0xFF;
      int currB = currColor & 0xFF;
      // Extract the red, green, and blue components of the background pixel's color
      int bkgdR = (bkgdColor >> 16) & 0xFF;
      int bkgdG = (bkgdColor >> 8) & 0xFF;
      int bkgdB = bkgdColor & 0xFF;
      // Compute the difference of the red, green, and blue values
      int diffR = abs(currR - bkgdR);
      int diffG = abs(currG - bkgdG);
      int diffB = abs(currB - bkgdB);
      // Add these differences to the running tally
      presenceSum += diffR + diffG + diffB;
      // Render the difference image to the screen
      pixels[i] = color(diffR, diffG, diffB);
      // The following line does the same thing much faster, but is more technical
      //pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB;
    }
    updatePixels(); // Notify that the pixels[] array has changed
    println(presenceSum); // Print out the total amount of movement
  }
}
 
// When a key is pressed, capture the background image into the backgroundPixels
// buffer, by copying each of the current frame's pixels into it.
void keyPressed() {
  video.loadPixels();
  arraycopy(video.pixels, backgroundPixels);
}

OpenCV: Gesichtserkennung

OpenCV: Gesichtserkennung

import gab.opencv.*;
import java.awt.Rectangle;
import processing.video.*;
 
OpenCV opencv;
Rectangle[] faces;
Capture video;
 
void setup() {
  size(640, 480);
  video = new Capture(this, Capture.list()[0]);
  opencv = new OpenCV(this, width, height);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
 
  video.start();
}
 
void draw() {
  video.read();
  opencv.loadImage(video);
 
  image(video, 0, 0); // display a video frame on the drawing canvas
 
  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
  Rectangle[] faces = opencv.detect();
  for (int i = 0; i < faces.length; i++) {
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  }
}

Bildverarbeitung: Kantendetektion

Bildverarbeitung: Kantendetektion

import gab.opencv.*;
import processing.video.*;
 
OpenCV opencv;
Capture video;
 
void setup() {
 
  size(640, 480);
  video = new Capture(this, Capture.list()[0]);
  opencv = new OpenCV(this, width, height);
  video.start();
 
}
 
void draw() {
  video.read(); // read current video frame 
  opencv.loadImage(video); // and load it into the opencv object
  opencv.findCannyEdges(20, 75); // find edges
  PImage canny = opencv.getSnapshot(); // get the black-and-white resulting image
  image(canny, 0, 0); // display the image on the drawing canvas
}

Bildverarbeitung: Hough-Transformation

Bildverarbeitung: Hough-Transformation

import gab.opencv.*;
import processing.video.*;
 
OpenCV opencv;
Capture video;
 
void setup() {
 
  size(640, 480);
  video = new Capture(this, Capture.list()[0]);
  opencv = new OpenCV(this, width, height);
  video.start();
}
 
void draw() {
  video.read(); // read current video frame 
  opencv.loadImage(video); // and load it into the opencv object
  opencv.findCannyEdges(20, 75); // find edges
  opencv.getSnapshot(); // get the black-and-white resulting image
  image(video, 0, 0); // display the video frame on the drawing canvas
 
  // Find lines with Hough line detection
  // Arguments are: threshold, minLength, maxLineGap
  ArrayList<Line> lines = opencv.findLines(100, 30, 20);
 
  for (Line line : lines) {
    // lines include angle in radians, measured in double precision
    // so we can select out vertical and horizontal lines
    // They also include "start" and "end" PVectors with the position
    if (line.angle >= radians(0) && line.angle < radians(1)) {
      stroke(0, 255, 0);
      line(line.start.x, line.start.y, line.end.x, line.end.y);
    }
 
    if (line.angle > radians(89) && line.angle < radians(91)) {
      stroke(255, 0, 0);
      line(line.start.x, line.start.y, line.end.x, line.end.y);
    }
  }
}
skriptsose2020/code-beispiele_aus_den_vorlesungnsvideos_sose2020.txt · Zuletzt geändert: 2020/06/18 01:10 von d.golovko