簡體   English   中英

如何將int RGB轉換為BYTE 2D陣列灰度圖像

[英]How to convert int RGB into BYTE 2D array gray image

我正在嘗試將彩色圖像轉換為灰度縮放圖像並將其作為BYTE值保存到2D陣列中,之后我需要在此2D陣列上執行一些處理。 雖然,我的數組的值都是字節,但我的輸出圖像變為藍色而不是灰色。 有人可以幫我嗎? 這是我的代碼:

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;
}

}

問題出在main方法的主要嵌套循環中:

for (int i =0 ; i<savedImage.length ; i ++){
    for (int j=0 ; j<savedImage[0].length ; j++){
           grayImage.setRGB(i, j, savedImage[i][j]);//← Here
        System.out.print(savedImage[i][j] + ",");
    }
}

int[][] savedImage包含一個表示灰度值的字節,但grayImage仍然是BufferedImage.TYPE_INT_RGB類型。 因此,如果您只調用grayImage.setRGB(i, j, savedImage[i][j]) ,則只會設置int的藍色部分:

RRRRRRRR GGGGGGGG BBBBBBBB
00000000 00000000 xxxxxxxx

所以你需要改為調用它:

int rgb = savedImage[i][j];
rgb = (rgb<<16)|(rgb<<8)|(rgb);
grayImage.setRGB(i, j, rgb);

您有不同的可能性,但首先,這是一種更簡單的方法來訪問像素:

public static int[][] readimage(File filename) throws IOException {
    BufferedImage img = ImageIO.read(filename) ;// Grayscale Image output
    final int width = img.getWidth() ;
    final int height = img.getHeight() ;
    int[][] graylevelarray = new int[height][width] ;

    for (int y=0 ; y < height ; y++)
        for (int x=0 ; x < width ; x++) {
            int r = img.getRaster().getSample(x, y, 0) ;
            int g = img.getRaster().getSample(x, y, 1) ;
            int b = img.getRaster().getSample(x, y, 2) ;
            graylevelarray[y][x] = ??? ;
            }

    return graylevelarray ;
    }

正如我所說,對於轉換,您有許多不同的可能性:

  • 你做的平均值,但它很少使用。
  • CIE 601(0.299 * R + 0.587 * G + 0.114 * B)或CIE 709(0.2125 * R + 0.7154 * G + 0.0721 * B)推薦,最常用。
  • 便宜/快速綠色(R + 2 * G + B)/ 4,即更快,例如用於庫中Zxing。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM