FB Automatisierung und Informatik: Programmierung 1, MI/WI 1
Programmierung 1 Studiengang MI / WI
Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
Hochschule Harz
FB Automatisierung und Informatik
mwilhelm@hs-harz.de
http://mwilhelm.hs-harz.de
Raum 2.202
Tel. 03943 / 659 338
Inhalt der Vorlesung
Überblick:
•
Erste Beispiele, Interaktion
•
elementare Datentypen
•
Variablen und Kontrollstrukturen
•
Arrays und Funktionen
•
Objekte und Methoden
•
Bilder mittels PImage
•
Algorithmen und Pseudocode
•
Laufzeitverhalten
•
Simulation
•
Bibliotheken
• Folien basierend auf Daniel Schiffman “Learning Processing” und Donald W. Smith
• Folien basierend auf Vorlesung „Programmierung1“ von Prof. Singer
Grundlegende Algorithmen und Methoden:
•
Suchen und Sortieren
•
Hashing
•
Rekursion
•
Graphen
•
Dynamische
Programmierung
Von Processing zu Java
FB Automatisierung und Informatik: Programmierung 1, MI/WI 33
Kapitel Bilder
Klasse PImage
•
Attribute
•
Methoden
Allgemeine Methoden
• loadImage
• tint
• image
Klassen PImage
Pimage bietet die Funktionalität, um Bilder anzeigen zu können
Atribute
•
width Breite des Bildes
•
height Höhe des Bildes
•
pixels[] öffentliche Arrays der Pixel mit den Farben Allgemeine Methoden
•
image image(int x, inty)
image(int x, int y,int width, int height)
•
Bilder sind im „data“-Ordner
FB Automatisierung und Informatik: Programmierung 1, MI/WI 5
Beispiel: Image_01
PImage image1;
void setup() {
size(BREITE, HOEHE);
image1 = loadImage("bild1.jpg");
}
void draw() {
background(0, 200, 255);
image(image1, 20, 20); // image, x, y
}
Beispiel: Image_01
FB Automatisierung und Informatik: Programmierung 1, MI/WI 7
Beispiel Image_02
Änderung:
•
Datei: bild2.jpg, kleiner skaliert
•
Anzeige beim Mauskursor ?
get
get() liefert komplette Bild
get(x,y) liefert Pixelfarbe bei x,y get(x,y,width,height) liefert Bildauschnitt
set
set(x,y) setzt eine Farbe für x/y
set(x,y,img) kopiert ein Bild in ein Bild
copy kopiert innerhalb eines Bildes
copy(sx, sy, swidth, sheight, dx, dy, dwidth, dheight);
copy(srcImg, sx, sy, swidth, sheight, dx, dy, dwidth, dheight);
mask
mask(img) img dient als Maske für Alpha-Wert mask(array) array dient als Maske für Alpha-Wert
Methoden von PImage
FB Automatisierung und Informatik: Programmierung 1, MI/WI 9
Blend einfügen von Pixel in ein Bild Mehrere komplexe Modi
filter Interne Konvertierung eines Bildes
THRESHOLD, GRAY, INVERT, POSTERIZE, BLUR, OPAQUE, ERODE, or DILATE
PImage a;
a = loadImage(„bild1.jpg");
a.filter(GRAY);
image(a, 50, 0);
save() Saves the image to a TIFF, TARGA, PNG, or JPEG file resize() Changes the size of an image to a new width and
Methoden von PImage
save Speichern des Bildes nach TIFF, TARGA, PNG, JPG wichtig für createImage
resize Ändert die Größe eines Bildes
loadPixels lädt die Pixel aus dem Bild. Speicherung in ein eindimensionales Array:
pixels[] array
updatePixels Das Array “pixels” wird als Bild zurückgeschrieben
Methoden von PImage
FB Automatisierung und Informatik: Programmierung 1, MI/WI 11
Beispiel Image_03
Änderung:
•
Dateien:
−
bild1.jpg
−
bild2.jpg
−
bild3.jpg
•
Maustaste gedrückt
−
Alle Bilder bewegen sich auf den Cursor zu
Beispiel Image_03
FB Automatisierung und Informatik: Programmierung 1, MI/WI 13
void mousePressed() {
for (int i=0; i<MAX; i++) {
x[i]+= calc(mouseX,x[i],STEP);
y[i]+= calc(mouseY,y[i],STEP);
} } }
Beispiel Image_04
Bildbearbeitung in Processing
FB Automatisierung und Informatik: Programmierung 1, MI/WI 15
Direkter Zugriff auf Bildpixel
•
Nach dem Aufruf der Methode loadPixels()stehen die Pixel des Bildes als 1-dimensionales Array zur Verfügung.
−
Attribut „pixels“
•
Nach Veränderungen der Pixel müssen diese mittels der
Methode updatePixels() wieder in das Bild geschrieben werden.
Datentyp „color“
•
enthält 32-bit Ganzzahl, jeweils 8 bit für
•
rot, grün, blau und alpha.
0 1 2 3 4 5 6 7 8 9
0 0 1 2 3 4 5 6 7 8 9
1 10 11 12 13 14 15 16 17 18 19
2 21 22 23 24 25 26 27 28 29 30
3 31 32 33 34 35 36 37 38 39 40
4 41 42 43 44 45 46 47 48 49 50
5 51 52 53 54 55 56 57 58 59 60
breite = 10
höhe = 5
•
Umwandlung von 2D (i,j) in 1D (index)
•
für pixel[] Array
•
index = zeile * breite + spalte
Direkter Zugriff auf Bildpixel
FB Automatisierung und Informatik: Programmierung 1, MI/WI 17
PImage img;
void setup() {
image1 = loadImage("bild1.jpg");
size(img.width, img.height);
}
void draw() {
img.loadPixels();
for(int j=0; j<img.height; j++) { for(int i=0; i<img.width; i++) {
int index = j*img.width + i;
float r = red(img.pixels[index]);
float g = green(img.pixels[index]);
float b = blue(img.pixels[index]);
.... // Bearbeite Pixelfarben hier img.pixels[index] = color(r, g, b);
} // for } // for
img.updatePixels();
} // draw
image_06
image_06
image1.pixels[index] = color(b,r,g);
FB Automatisierung und Informatik: Programmierung 1, MI/WI 19
Beispiel: image_07
Schwarz-Weiß:
•
col1=(int) ((b+r+g)/3);
•
image1.pixels[index] = color(col1,col1,col1);
Ohne Rot-Anteil:
•
image1.pixels[index] = color(0,g,b);
Brightness:
•
image1.pixels[index] = color( r*1.1, g*1.1, b*1.1);
Beispiel: image_07
FB Automatisierung und Informatik: Programmierung 1, MI/WI 21
Beispiel: image_08: blur-Filter beim Tastendruck
float fRed=0.5;
float fGreen=0.2;
float fBlue=0.3;
void calc() {
image1.loadPixels();
for (int j=0; j<image1.height; j++) { for (int i=0; i<image1.width; i++) {
...
image1.pixels[index] =
color(r*fRed,g*fGreen,b*fBlue);
} // for } // for
image1.updatePixels();
}
image_08
FB Automatisierung und Informatik: Programmierung 1, MI/WI 23
void keyPressed() { fRed=random(0,2);
fGreen=random(0,2);
fBlue=random(0,2);
println("Red: "+fRed
+" Green: "+fGreen +" Blue: "+fBlue);
calc();
}
Beispiel: image_08
Beispiel: image_09: Analyse der Farbpixel
final int MAX=256;
PImage image1;
int[][] rgb1=new int[MAX][3];
void setup() {
String[] captions={"rot","gruen","blau"};
image1 = loadImage("bild1.jpg");
for (int i=0; i<MAX;i++) { for (int j=0; j<3; j++) {
rgb1[i][j]=0;
} }
calc();
FB Automatisierung und Informatik: Programmierung 1, MI/WI 25
Beispiel: image_09: Analyse der Farbpixel
for (int i=0; i<MAX;i++) { print("i: "+i+" ");
for (int j=0; j<3; j++) {
print(captions[j]+": "+rgb1[i][0]+" ");
}
println();
}
}
void calc() {
image1.loadPixels();
for (int j=0; j<image1.height; j++) { for (int i=0; i<image1.width; i++) {
int index = j*image1.width + i;
int r = (int)red(image1.pixels[index]);
int g = (int)green(image1.pixels[index]);
int b = (int)blue(image1.pixels[index]);
// Bearbeite Pixelfarben hier
rgb1[r][0]++; // Addiere rot-Anteil rgb1[g][1]++; // Addiere grün-Anteil rgb1[b][2]++; // Addiere blau-Anteil } // for
} // for
}
FB Automatisierung und Informatik: Programmierung 1, MI/WI 27