Ich bin neu in Java und möchte rote Pixel in einem gegebenen Bild zählen. Ich habe bisher unter Code, aber nicht sicher, welche Bedingung hinzugefügt werden, um zu überprüfen, ob Pixel rot ist. Ich habe bisher unter Code. Danke im Voraus.Zählen rote Pixel in einem gegebenen Bild
public static int countRedPixels(Picture v){
BufferedImage image = (v.getBufferedImage());
int width = image.getWidth();
int height = image.getHeight();
int redCount = 0;
int pixelCount = 0;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height ; y++) {
int rgb = image.getRGB(x, y);
//get rgbs
//int alpha = (rgb >>> 24) & 0xFF;
int red = (rgb >>> 16) & 0xFF;
int green = (rgb >>> 8) & 0xFF;
int blue = (rgb >>> 0) & 0xFF;
if (red == 255 && green == 0 && blue == 0 || image.getRGB(x, y) == 0xFFFF0000) {
redCount++;
}
pixelCount++;
}
}
System.out.println("Red Pixel Count:" + redCount);
System.out.println("Pixel Count:" + pixelCount);
return redCount;
}
Ich denke, es hängt von Ihrer Definition von * rot * ab? Wäre es rgb (255,0,0)? – domsson
Ja, es wäre (255,0,0). –
Nun, dann ... 'if (rot == 255 && grün == 0 && blau == 0) {++ redCount; } '? Oder sogar 'if (rgb == 0xFFFF0000) ...' – domsson