Ich versuche ein Farbbild in ein Graustufenbild zu konvertieren und es als BYTE-Werte in einem 2D-Array zu speichern und später muss ich etwas an diesem 2D-Array machen. Obwohl die Werte meines Arrays alle Bytes sind, wird mein Ausgabebild blau statt grau. Kann mir bitte jemand helfen? Hier ist mein Code:Wie konvertiert man Int RGB in BYTE 2D-Array Graues Bild
public class HW {
public static void main(String[] args) {
int[][] savedImage = readimage(new File("C:\\Images\\input.jpg"));
BufferedImage grayImage = new BufferedImage(savedImage.length, savedImage[0].length, BufferedImage.TYPE_INT_RGB);
for (int i =0 ; i<savedImage.length ; i ++){
for (int j=0 ; j<savedImage[0].length ; j++){
grayImage.setRGB(i, j, savedImage[i][j]);
System.out.print(savedImage[i][j] + ",");
}
System.out.println();
}
try {
File outputfile = new File ("C:\\Images\\garyoutput.jpg");
ImageIO.write(grayImage, "jpg", outputfile);
}
catch(IOException e) {
e.printStackTrace();
}
}
public static int[][] readimage(File filename) throws IOException {
// Grayscale Image output
BufferedImage img = ImageIO.read(filename);
int width = img.getWidth();
int height = img.getHeight();
int [][] readimageVal = new int [width][height];
for (int i = 0; i<height ; i++) {
for (int j =0 ; j<width ; j++) {
int p = img.getRGB(j,i);
int r = (p>>16)&0xff;
int g = (p>>8)&0xff;
int b = p&0xff;
int avg = ((r+b+g)/3);
readimageVal[j][i] = avg;
}
}
return readimageVal;
}
}
Sie haben den falschen Pixel/Byte-Typ eingestellt. Verwenden Sie 'new BufferedImage (savedImage.length, savedImage [0] .length, BufferedImage.TYPE_USHORT_GRAY);' – JayC667