Ka Flashcards
Spiegeln Horizontal
{ public Picture spiegelHorizontal(Picture originalbild) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe]; for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { pixelNeu[x][y] = pixel[(breite-1)-x][y]; } }
Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
Spiegeln Vertikal
{ public Picture spiegelVertikal(Picture originalbild) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe]; for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { pixelNeu[x][y] = pixel[x][(hoehe-1)-y]; } }
Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
Dreh Rechts
{ public Picture dreheRechts(Picture originalbild) {
int breite = originalbild.getHeight();
int hoehe = originalbild.getWidth();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { pixelNeu[x][y] = pixel[y][(breite-1)-x]; } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
dreh links
{ public Picture dreheLinks(Picture originalbild) {
int breite = originalbild.getHeight();
int hoehe = originalbild.getWidth();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { pixelNeu[x][y] = pixel[(hoehe-1)-y][x]; } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
drehe 180
{ public Picture drehe180(Picture originalbild) {
Picture bild90 = dreheLinks(originalbild);
Picture bild180 = dreheLinks(bild90);
return bild180; }
graustufe Durchschnitt
{
public Picture graustufenDurchschnitt(Picture originalbild) { int breite = originalbild.getWidth(); int hoehe = originalbild.getHeight(); Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { int grau = (int) ((pixel[x][y].getRed()+ pixel[x][y].getGreen() + pixel[x][y].getBlue())/3); pixelNeu[x][y] = new Color(grau, grau, grau); } }
Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
graustufe Minimum
{ public Picture graustufenMin(Picture originalbild) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { int grau = pixel[x][y].getRed(); if (grau > pixel[x][y].getGreen()) grau = pixel[x][y].getGreen(); if (grau > pixel[x][y].getBlue()) grau = pixel[x][y].getBlue(); pixelNeu[x][y] = new Color(grau, grau, grau); } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
graustufe Maximum
public Picture graustufenMax(Picture originalbild) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { int grau = pixel[x][y].getRed(); if (grau < pixel[x][y].getGreen()) grau = pixel[x][y].getGreen(); if (grau < pixel[x][y].getBlue()) grau = pixel[x][y].getBlue(); pixelNeu[x][y] = new Color(grau, grau, grau); } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
Natürliche Grausstufe
public Picture graustufenNatuerlich(Picture originalbild) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { int grau = (int) (pixel[x][y].getRed()*299+pixel[x][y].getGreen()*587+pixel[x][y].getBlue()*114)/1000; pixelNeu[x][y] = new Color(grau, grau, grau); } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
Bild invertieren
{ public Picture invertieren(Picture originalbild) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { int rot = 255-pixel[x][y].getRed(); int gruen = 255-pixel[x][y].getGreen(); int blau = 255-pixel[x][y].getBlue(); pixelNeu[x][y] = new Color(rot, gruen, blau); } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
tausch rot-grün
public Picture tauschRotGruen(Picture originalbild) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { int rot = pixel[x][y].getGreen(); int gruen = pixel[x][y].getRed(); int blau = pixel[x][y].getBlue(); pixelNeu[x][y] = new Color(rot, gruen, blau); } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
tausch rot blau
public Picture tauschRotBlau(Picture originalbild) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { int rot = pixel[x][y].getBlue(); int gruen = pixel[x][y].getGreen(); int blau = pixel[x][y].getRed(); pixelNeu[x][y] = new Color(rot, gruen, blau); } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
Tausch Grün Blau
public Picture tauschGruenBlau(Picture originalbild) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe];
for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { int rot = pixel[x][y].getRed(); int gruen = pixel[x][y].getBlue(); int blau = pixel[x][y].getGreen(); pixelNeu[x][y] = new Color(rot, gruen, blau); } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
Farbänderung
public Picture farbaenderung(Picture originalbild, double faktor_r, double faktor_g, double faktor_b) {
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = new Color[breite][hoehe]; for(int x=0; x < breite; x++) { for(int y=0;y < hoehe; y++) { int rot = (int) (pixel[x][y].getRed() * faktor_r); int gruen = (int) (pixel[x][y].getGreen() * faktor_g); int blau = (int) (pixel[x][y].getBlue() * faktor_b);
//Begrenzung auf zulässigen Bereich if(rot < 0) rot = 0; if(rot > 255) rot = 255; if(gruen < 0) gruen = 0; if(gruen > 255) gruen = 255; if(blau < 0) blau = 0; if(blau > 255) blau = 255;
pixelNeu[x][y] = new Color(rot, gruen, blau); } } Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
}
Striheln
class Kunst { public Picture stricheln(Picture originalbild, int laenge, int dicke, int anzahl) { int breite = originalbild.getWidth(); int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Picture neuesBild = new Picture(breite,hoehe);
neuesBild.strokeWeight(dicke); Random r = new Random();
for(int i=0; i < anzahl; i++) { int x = r.nextInt(breite); int y = r.nextInt(hoehe); neuesBild.stroke(pixel[x][y].getRed(),pixel[x][y].getGreen(),pixel[x][y].getBlue()); neuesBild.line(x, y, x+laenge, y+laenge); } return neuesBild; }
Tupfen
public Picture tupfen(Picture originalbild, int groesse,int anzahl){
int breite = originalbild.getWidth();
int hoehe = originalbild.getHeight();
Color[][] pixel = originalbild.getPixelArray(); Picture neuesBild = new Picture(breite,hoehe);
Random r = new Random(); neuesBild.noStroke();
for(int i=0; i < anzahl; i++) { int x = r.nextInt(breite); int y = r.nextInt(hoehe); neuesBild.fill(pixel[x][y].getRed(),pixel[x][y].getGreen(),pixel[x][y].getBlue()); neuesBild.ellipse(x, y, groesse, groesse); } return neuesBild; }
Faltung (extra)
{
Picture faltung(Picture originalbild, double[][] filter) { // Neue Pixel zunächst wie alte Pixel, damit der Rand kopiert wird. Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = originalbild.getPixelArray();
// Größe der Filtermatrix bestimmen int laenge = filter.length; int halb = laenge / 2;
// Faltung berechnen // Schleife über alle Spalten for (int x = filter.length/2; x < originalbild.getWidth() - filter.length/2; x++){ // Schleife über alle Zeilen for (int y = filter.length/2; y < originalbild.getHeight() - filter.length/2; y++) { // Deklaration der neuen Farbkomponenten double rot =0; double gruen = 0; double blau = 0;
// Koordinaten des Pixels links oben in der Ecke der Filtermatrix int xx = x - filter.length/2; int yy = y - filter.length/2;
// Schleifen über jeden Punkt der Filtermatrix for (int i = 0; i < filter.length; i++) { for (int j = 0; j < filter.length; j++){ // Summiere die gewichteten Farbkomponenten der Originalpixel rot += filter[i][j] * pixel[xx+i][yy+j].getRed(); gruen += filter[i][j] * pixel[xx+i][yy+j].getGreen(); blau += filter[i][j] * pixel[xx+i][yy+j].getBlue(); } }
//Begrenzung auf zulässigen Bereich if(rot < 0.0) rot = 0.0; if(rot > 255.0) rot = 255.0; if(gruen < 0.0) gruen = 0.0; if(gruen > 255.0) gruen = 255.0; if(blau < 0.0) blau = 0.0; if(blau > 255.0) blau = 255.0;
// Definiere Farbe des neuen Pixels pixelNeu[x][y] = new Color((int) rot, (int) gruen, (int) blau); } } // Erzeuge neues Bild Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
Faltung randbehandlung 1 (extra)
Picture faltung_randbehandlung1(Picture originalbild, double[][] filter) {
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = originalbild.getPixelArray(); int laenge = filter.length; int halb = laenge / 2; for (int x = 0; x < originalbild.getWidth(); x++){ for (int y = 0; y < originalbild.getHeight(); y++) { double rot =0; double gruen = 0; double blau = 0; int xx = x - filter.length/2; int yy = y - filter.length/2; for (int i = 0; i < filter.length; i++) { for (int j = 0; j < filter.length; j++){ Color c = Color.BLACK; if(xx+i >= 0 && xx+i =0 && yy+j < originalbild.getHeight()) { c = pixel[xx+i][yy+j]; } rot += filter[i][j] * c.getRed(); gruen += filter[i][j] * c.getGreen(); blau += filter[i][j] * c.getBlue(); } } if(rot < 0.0) rot = 0.0; if(rot > 255.0) rot = 255.0; if(gruen < 0.0) gruen = 0.0; if(gruen > 255.0) gruen = 255.0; if(blau < 0.0) blau = 0.0; if(blau > 255.0) blau = 255.0;
pixelNeu[x][y] = new Color((int) rot, (int) gruen, (int) blau); } }
Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
Faltung randbehadnlung 2 (extra)
Picture faltung_randbehandlung2(Picture originalbild, double[][] filter) {
Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = originalbild.getPixelArray(); int laenge = filter.length; int halb = laenge / 2; for (int x = 0; x < originalbild.getWidth(); x++){ for (int y = 0; y < originalbild.getHeight(); y++) { double rot =0; double gruen = 0; double blau = 0; int xx = x - filter.length/2; int yy = y - filter.length/2; for (int i = 0; i < filter.length; i++) { for (int j = 0; j < filter.length; j++){ int xxx = xx+i; int yyy = yy+j; if(xxx<0) xxx=0; if(xxx>= originalbild.getWidth()) xxx = originalbild.getWidth()-1; if(yyy<0) yyy=0; if(yyy>= originalbild.getHeight()) yyy = originalbild.getHeight()-1; rot += filter[i][j] * pixel[xxx][yyy].getRed(); gruen += filter[i][j] * pixel[xxx][yyy].getGreen(); blau += filter[i][j] * pixel[xxx][yyy].getBlue(); } } if(rot < 0.0) rot = 0.0; if(rot > 255.0) rot = 255.0; if(gruen < 0.0) gruen = 0.0; if(gruen > 255.0) gruen = 255.0; if(blau < 0.0) blau = 0.0; if(blau > 255.0) blau = 255.0;
pixelNeu[x][y] = new Color((int) rot, (int) gruen, (int) blau); } }
Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
Picture faltung_randbehandlung3(Picture originalbild, double[][] filter) { Color[][] pixel = originalbild.getPixelArray(); Color[][] pixelNeu = originalbild.getPixelArray(); int laenge = filter.length; int halb = laenge / 2;
for (int x = 0; x < originalbild.getWidth() ; x++){ // Schleife über alle Zeilen for (int y = 0; y < originalbild.getHeight() ; y++) {
double rot =0; double gruen = 0; double blau = 0; int xx = x - filter.length/2; int yy = y - filter.length/2;
for (int i = 0; i < filter.length; i++) { for (int j = 0; j < filter.length; j++){ // Bestimme Pixelkoordinaten int xxx = xx+i; int yyy = yy+j;
if(xxx<0) xxx=-xxx; if(xxx>= originalbild.getWidth()) xxx = 2*originalbild.getWidth()-xxx-1; if(yyy<0) yyy=-yyy; if(yyy>= originalbild.getHeight()) yyy = 2*originalbild.getHeight()-yyy-1; rot += filter[i][j] * pixel[xxx][yyy].getRed(); gruen += filter[i][j] * pixel[xxx][yyy].getGreen(); blau += filter[i][j] * pixel[xxx][yyy].getBlue(); } } if(rot < 0.0) rot = 0.0; if(rot > 255.0) rot = 255.0; if(gruen < 0.0) gruen = 0.0; if(gruen > 255.0) gruen = 255.0; if(blau < 0.0) blau = 0.0; if(blau > 255.0) blau = 255.0;
pixelNeu[x][y] = new Color((int) rot, (int) gruen, (int) blau); } }
Picture neuesBild = new Picture(); neuesBild.setPixelArray(pixelNeu); return neuesBild; }
public Picture weichzeichnen(Picture originalbild) { double[][] mittelwertfilter ={{1.0/9,1.0/9,1.0/9}, {1.0/9,1.0/9,1.0/9}, {1.0/9,1.0/9,1.0/9}}; return faltung(originalbild,mittelwertfilter); }
public Picture schaerfen(Picture originalbild) { double[][] schaerfen ={{0,-1,0}, {-1,5,-1}, {0,-1,0}}; return faltung(originalbild,schaerfen); }
public Picture relief(Picture originalbild) { double[][] relieffilter ={{-2,-1,0}, {-1,1,1}, {0,1,2}}; return faltung(originalbild,relieffilter); }
public Picture kantenfinden(Picture originalbild) { double[][] kantenfilter ={{0,1,0}, {1,-4,1}, {0,1,0}}; return faltung(originalbild,kantenfilter); }
public Picture kantenfindenHorizontal(Picture originalbild) { double[][] sobelx ={{-1,0,1}, {-2,0,2}, {-1,0,1}}; return faltung(originalbild,sobelx); }
public Picture kantenfindenVertikal(Picture originalbild) { double[][] sobely={{-1,-2,-1}, {0,0,0}, {1,2,1}}; return faltung(originalbild,sobely); }
// Ergänzungen: Größere Filter
public Picture weichzeichnen(Picture originalbild, int groesse) { // Deklariere die Matrix in der richtigen Größe. double[][] mittelwertfilter = new double[groesse][groesse]; // Berechne Anzahl der Zellen. int anzahl = groesse * groesse;
// Berechne für jede Zelle der Matrix ... for(int i=0; i< groesse; i++) { for(int j=0; j