Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
skriptsose2020:code-beispiele_aus_den_vorlesungnsvideos_sose2020 [2020/04/29 20:34] d.golovko |
skriptsose2020:code-beispiele_aus_den_vorlesungnsvideos_sose2020 [2020/06/18 01:10] (aktuell) d.golovko |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====Code-Beispiele aus den Vorlesungsvideos SoSe2020===== | ====Code-Beispiele aus den Vorlesungsvideos SoSe2020===== | ||
- | ==Woche 2: Rekapitulation Woche 1 und Start in die Woche 2== | + | ===Woche 2: Rekapitulation Woche 1 und Start in die Woche 2=== |
<hidden Strings> | <hidden Strings> | ||
<code java> | <code java> | ||
Zeile 35: | Zeile 35: | ||
\\ | \\ | ||
- | ==Woche 2: Arrays== | + | ===Woche 2: Arrays=== |
<hidden Code> | <hidden Code> | ||
<code java> | <code java> | ||
Zeile 79: | Zeile 79: | ||
\\ | \\ | ||
- | ==Woche 2: Schleifen== | + | ===Woche 2: Schleifen=== |
<hidden while und for> | <hidden while und for> | ||
<code java> | <code java> | ||
Zeile 217: | Zeile 217: | ||
</hidden> | </hidden> | ||
\\ | \\ | ||
- | ==Woche 2: Methoden=== | + | ===Woche 2: Methoden==== |
<hidden Beispeiele von Methoden> | <hidden Beispeiele von Methoden> | ||
Zeile 268: | Zeile 268: | ||
void greet() { | void greet() { | ||
println("Hallo Welt"); | println("Hallo Welt"); | ||
+ | } | ||
+ | </code> | ||
+ | </hidden> | ||
+ | \\ | ||
+ | |||
+ | ===Woche 3: Steuerung eines zweirädigen Roboters=== | ||
+ | |||
+ | <hidden Über globale Variablen> | ||
+ | <code java> | ||
+ | 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)); | ||
+ | } | ||
+ | |||
+ | |||
+ | </code> | ||
+ | </hidden> | ||
+ | \\ | ||
+ | |||
+ | ===Woche 3: Klassen=== | ||
+ | |||
+ | <hidden Haupt-Sketch> | ||
+ | <code java> | ||
+ | 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); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | </hidden> | ||
+ | <hidden Klasse Robot> | ||
+ | <code java> | ||
+ | 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 | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | </hidden> | ||
+ | \\ | ||
+ | |||
+ | ===Woche 4: Bibliotheken=== | ||
+ | {{anchor:woche4}} | ||
+ | |||
+ | <hidden Video-Bibliothek: Abzug des Hintergrunds> | ||
+ | <code java> | ||
+ | /** | ||
+ | * 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); | ||
+ | } | ||
+ | </code> | ||
+ | </hidden> | ||
+ | |||
+ | <hidden OpenCV: Gesichtserkennung> | ||
+ | <code java> | ||
+ | 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); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | </hidden> | ||
+ | |||
+ | <hidden Bildverarbeitung: Kantendetektion> | ||
+ | <code java> | ||
+ | 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 | ||
+ | } | ||
+ | </code> | ||
+ | </hidden> | ||
+ | |||
+ | <hidden Bildverarbeitung: Hough-Transformation> | ||
+ | <code java> | ||
+ | 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); | ||
+ | } | ||
+ | } | ||
} | } | ||
</code> | </code> | ||
</hidden> | </hidden> | ||