2012-04-12 163 views
4

我在使用Java Swing和JAI的NetBeans平臺上製作應用程序。在這個我想做圖像處理。我使用X射線槍捕獲黑白圖像。之後我想繪製黑白圖像的直方圖。因此,對於直方圖來說,首先我們必須得到灰度或黑白圖像的像素值。那麼我們可以使用這個像素值繪製直方圖。那麼,我怎樣才能得到這個黑白圖像的像素值呢?如何獲得黑白圖像的像素值?

回答

4

這應該工作,如果您使用java.awt.image.BufferedImage。

既然你想創建一個直方圖,我想你會遍歷所有的像素。有返回單個像素值的方法。

int getRGB(int x, int y) 

然而,由於循環將發生,我想你想使用這一個:

int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) 

當你的陣列,使用:

int alpha = (pixels[i] >> 24) & 0x000000FF; 
int red = (pixels[i] >> 16) & 0x000000FF; 
int green = (pixels[i] >>8) & 0x000000FF; 
int blue = pixels[i] & 0x000000FF; 

要提取頻道數據。不知道是否變量可以聲明爲字節(我們只使用數組中的整數的一個字節,儘管字節是有符號的,並且發生了不同的算術 - 二的補碼形式),但是您可以聲明它們是短的。

然後瓶坯一些數學上的這些值,例如:

int average = (red + green + blue)/3; 

這將返回的平均水平像素,給你一個點,你可以在一個簡單的亮度直方圖使用。

編輯:

關於直方圖製作,我已經使用這個類。它將您想要的直方圖作爲其參數的圖像採用setImage(BufferedImage image)方法。使用updateHistogram()進行數組填充。繪圖數據在paintComponent(Graphics g)。我必須承認,這是渺茫的,特別是在計算偏移量時,但它可以很容易地簡化。

這是全班同學:

class HistogramCtrl extends JComponent 
{ 
    BufferedImage m_image; 
    int[] m_histogramArray = new int[256]; //What drives our histogram 
    int m_maximumPixels; 
    public HistogramCtrl(){ 
     m_maximumPixels = 0; 
     for(short i = 0; i<256; i++){ 
      m_histogramArray[i] = 0; 
     } 
    } 

    void setImage(BufferedImage image){ 
     m_image = image; 
     updateHistogram(); 
     repaint(); 
    } 

    void updateHistogram(){ 
     if(m_image == null) return; 

     int[] pixels = m_image.getRGB(0, 0, m_image.getWidth(), m_image.getHeight(), null, 0, m_image.getWidth()); 
     short currentValue = 0; 
     int red,green,blue; 
     for(int i = 0; i<pixels.length; i++){   
       red = (pixels[i] >> 16) & 0x000000FF; 
       green = (pixels[i] >>8) & 0x000000FF; 
       blue = pixels[i] & 0x000000FF; 
       currentValue = (short)((red + green + blue)/3); //Current value gives the average //Disregard the alpha 
       assert(currentValue >= 0 && currentValue <= 255); //Something is awfully wrong if this goes off... 
       m_histogramArray[currentValue] += 1; //Increment the specific value of the array 
     } 

     m_maximumPixels = 0; //We need to have their number in order to scale the histogram properly 
     for(int i = 0; i < m_histogramArray.length;i++){ //Loop through the elements 
      if(m_histogramArray[i] > m_maximumPixels){ //And find the bigges value 
       m_maximumPixels = m_histogramArray[i]; 
      } 
     } 
    } 

    protected void paintComponent(Graphics g){ 
     assert(m_maximumPixels != 0); 

     Rectangle rect = g.getClipBounds(); 

     Color oldColor = g.getColor(); 
     g.setColor(new Color(210,210,210)); 
     g.fillRect((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight()); 
     g.setColor(oldColor); 

     String zero = "0"; 
     String thff = "255"; 

     final short ctrlWidth = (short)rect.getWidth(); 
     final short ctrlHeight = (short)rect.getHeight(); 

     final short activeWidth = 256; 
     final short activeHeight = 200; 

     final short widthSpacing = (short)((ctrlWidth - activeWidth)/2); 
     final short heightSpacing = (short)((ctrlHeight - activeHeight)/2); 

     Point startingPoint = new Point(); 
     final int substraction = -1; 
     startingPoint.x = widthSpacing-substraction; 
     startingPoint.y = heightSpacing+activeHeight-substraction; 

     g.drawString(zero,widthSpacing-substraction - 2,heightSpacing+activeHeight-substraction + 15); 
     g.drawString(thff,widthSpacing+activeWidth-substraction-12,heightSpacing+activeHeight-substraction + 15); 

     g.drawLine(startingPoint.x, startingPoint.y, widthSpacing+activeWidth-substraction, heightSpacing+activeHeight-substraction); 
     g.drawLine(startingPoint.x,startingPoint.y,startingPoint.x,heightSpacing-substraction); 

     double factorHeight = (double)activeHeight/m_maximumPixels; //The height divided by the number of pixels is the factor of multiplication for the other dots 

     Point usingPoint = new Point(startingPoint.x,startingPoint.y); 

     usingPoint.x+=2; //I want to move this two points in order to be able to draw the pixels with value 0 a bit away from the limit 
     Point tempPoint = new Point(); 
     for(short i = 0; i<256; i++){ 
      tempPoint.x = usingPoint.x; 
      tempPoint.y = (int)((heightSpacing+activeHeight-substraction) - (m_histogramArray[i] * factorHeight)); 
      if((i!=0 && (i % 20 == 0)) || i == 255){ 
       oldColor = g.getColor(); 
       g.setColor(oldColor.brighter()); 
       //Draw horizontal ruler sections 
       tempPoint.x = widthSpacing + i; 
       tempPoint.y = heightSpacing+activeHeight-substraction+4; 
       g.drawLine(tempPoint.x,tempPoint.y,widthSpacing + i,heightSpacing+activeHeight-substraction-4); 

       if(i <= 200){ 
        //Draw vertical ruler sections 
        tempPoint.x = widthSpacing - substraction - 3; 
        tempPoint.y = heightSpacing+activeHeight-substraction-i; 
        g.drawLine(tempPoint.x,tempPoint.y,widthSpacing - substraction + 4, heightSpacing+activeHeight-substraction-i); 
       } 
       tempPoint.x = usingPoint.x; 
       tempPoint.y = usingPoint.y; 

       g.setColor(oldColor); 
      } 
      g.drawLine(usingPoint.x, usingPoint.y, tempPoint.x, tempPoint.y); 
      usingPoint.x++; //Set this to the next point 
     } 
    } 
} 
+0

我已經嘗試過,但它並沒有確切的答案。 – Jay 2012-04-12 12:43:14

+0

你需要幫助創建/繪製直方圖嗎? – 2012-04-12 12:51:30

+0

是的。我想繪製直方圖。 – Jay 2012-04-12 13:29:13