2012-12-21 46 views
3

我嘗試編譯下面的代碼,但我發現圖像變成全部黑色,只爲我輸入的所有圖像進行測試。在java中的直方圖均衡

P.S.我是一個Java新手,我不允許使用任何包裝除了內置包

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 
import javax.swing.*; 
import java.util.*; 

public class ImageProcessor 
{ 
public static BufferedImage convert(Image img) 
{ 
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); 
    Graphics bg = bi.getGraphics(); 
    bg.drawImage(img, 0, 0, null); 
    bg.dispose(); 
    return bi; 
} 

public static BufferedImage toGrayScale(Image img) 
{ 
    // Convert image from type Image to BufferedImage 
    BufferedImage bufImg = convert(img); 

    // Scan through each row of the image 
    for(int j=0; j < bufImg.getHeight(); j++) 
    { 
     // Scan through each columns of the image 
     for(int i=0; i < bufImg.getWidth(); i++) 
     { 
      // Returns an integer pixel in the default RGB color model 
      int values=bufImg.getRGB(i,j); 
      // Convert the single integer pixel value to RGB color 
      Color oldColor = new Color(values); 

      int red = oldColor.getRed();  // get red value 
      int green = oldColor.getGreen(); // get green value 
      int blue = oldColor.getBlue(); // get blue value 

      // Convert RGB to gray scale using formula 
      // gray = 0.299 * R + 0.587 * G + 0.114 * B 
      double grayVal = 0.299*red + 0.587*green + 0.114*blue; 

      // Assign each channel of RGB with the same value 
      Color newColor = new Color((int)grayVal, (int)grayVal, (int)grayVal); 

      // Get back the integer representation of RGB color 
      // and assign it back to the original position 
      bufImg.setRGB(i, j, newColor.getRGB()); 
     } 
    } 
    // return back the resulting image in BufferedImage type 
    return bufImg; 
} 

public static BufferedImage histEqualization(Image img) 
{ 
    //Convert image to BufferedImage 
    img = ImageProcessor.toGrayScale(img); 
    BufferedImage bufImg = convert(img); 


    //Getting information of each pixel; 
    int[][] intensity = new int[bufImg.getWidth()][ bufImg.getHeight()]; 
    int[] counter = new int[256]; 
    for(int j=0; j < bufImg.getHeight();j++) 
     for(int i=0; i < bufImg.getWidth();i++) 
     { 
      int values=bufImg.getRGB(i,j);    
      Color oldColor = new Color(values); 
      intensity[i][j] = oldColor.getBlue(); 
      counter[intensity[i][j]]++; 
     } 

    //BEGIN OF Histogram Equalization 

    //find out how many rows the table have 
    int row=0; 

    for(int i=0;i<256;i++) 
     if(counter[i]!=0) 
      row++; 

    //Find out the v column of the table 
    //table[row][0] = v column 
    //table[row][1] = c column 
    int temp=0; 
    int[][] table = new int[row][2]; 


    for(int i=0;i<256;i++) 
     if(counter[i]!=0) 
     { 
      table[temp][0] = i; 
      temp++; 
     } 

    //Find out the c column of the table 
    for(int i=0;i<row;i++) 
     table[i][1] = counter[table[i][0]]; 

    //C-> CS 

    int sum = 0; 

    for(int i=0;i<row;i++) 
    { 
     sum += table[i][1]; 
     table[i][1] = sum; 
    } 

    //CS->NCS 
    int min = table[0][1], max = table[row-1][1]; 

    for(int i=0;i<row;i++) 
     table[i][1] = Math.round((table[i][1]-min)/(max-min)); 

    //Mapping 
    for(int j=0;j<bufImg.getHeight();j++) 
     for(int i=0;i<bufImg.getWidth();i++) 
     { 
      for(int k=0;k<row;k++) 
       if(intensity[i][j]==table[k][0]) 
        intensity[i][j] = table[k][1]; 

      Color newColor = new Color(intensity[i][j], intensity[i][j], intensity[i][j]); 

      bufImg.setRGB(i, j, newColor.getRGB()); 
     } 


    return bufImg; 
} 

} 
+0

剛纔'(r + g + b)/ 3'不是灰度嗎? – Doorknob

+0

直方圖均衡之前,檢查圖像是否被正確轉換爲灰度。 – auselen

+0

你應該可以像new color(0.299 * red,0.587 * green,0.114 * blue)' – auselen

回答

0

轉換方法可以簡化爲:

return new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY) ; 

,因爲你只想要一個灰度級(單通道)圖像。然後,該方法toGrayScale是:

BufferedImage bufImg = convert(img); 
byte[] bufferbyte = ((DataBufferByte) bufImg.getRaster().getDataBuffer()).getData() ; 
for(int j=0, pos=0; j < bufImg.getHeight(); j++) 
     for(int i=0; i < bufImg.getWidth(); i++, pos++) 
      { 
      int red = bufImg().getSample(i, j, 0) ; 
      int green = bufImg().getSample(i, j, 1) ; 
      int blue = bufImg().getSample(i, j, 2) ; 
      // gray = 0.299 * R + 0.587 * G + 0.114 * B 
      bufferbyte[pos] = (byte)(int)(0.299*red + 0.587*green + 0.114*blue) ; 
      } 
return bufImg ; 

我使用的光柵()來獲取RBG值,但到了DataBuffer直接訪問會快很多(如爲bufferbyte)。

然後你可以做直方圖均衡,但第一行是錯誤的,應該被刪除。