Created
September 2, 2021 12:44
-
-
Save JustAnotherJavaProgrammer/9aded43871dadca912cff6f5c1c03098 to your computer and use it in GitHub Desktop.
Aus dem Informatik-Unterricht
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import de.informatics4kids.Picture; | |
public class Histogramm { | |
public static void main(String[] args) { | |
Picture pic = new Picture("/home/student/devel/Bildverarbeitung/img/hund.jpg"); | |
int[][] absoluteCounts = new int[3][256]; | |
for (int y = 0; y < pic.heightY(); y++) { | |
for (int x = 0; x < pic.widthX(); x++) { | |
Color pxlCol = pic.getColor(x, y); | |
for (int i = 0; i < 3; i++) { | |
absoluteCounts[i][getColVal(pxlCol, i)]++; | |
} | |
} | |
} | |
for (int i = 0; i < absoluteCounts.length; i++) { | |
StringBuilder builder = new StringBuilder(); | |
for (int j = 0; j < absoluteCounts[i].length; j++) { | |
if (j != 0) | |
builder.append("\n"); | |
builder.append( | |
// j + "\t" + | |
((double) absoluteCounts[i][j]) / (pic.widthX() * pic.heightY())); | |
} | |
try { | |
Files.writeString(Paths.get("/home/student/", String.valueOf(i) + ".txt"), builder.toString(), StandardOpenOption.CREATE); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public static int getColVal(Color c, int valNo) { | |
switch (valNo) { | |
case 0: | |
return c.getRed(); | |
case 1: | |
return c.getGreen(); | |
case 2: | |
return c.getBlue(); | |
default: | |
return c.getAlpha(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment