Skip to content

Instantly share code, notes, and snippets.

@JustAnotherJavaProgrammer
Created September 2, 2021 12:44
Show Gist options
  • Save JustAnotherJavaProgrammer/9aded43871dadca912cff6f5c1c03098 to your computer and use it in GitHub Desktop.
Save JustAnotherJavaProgrammer/9aded43871dadca912cff6f5c1c03098 to your computer and use it in GitHub Desktop.
Aus dem Informatik-Unterricht
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