Question

I making App in netbeans platform using java Swing and JAI. In this i want to do image processing. I capture .tiff black and white image using X-Ray gun. after that i want to plot histogram of that Black and White image. so, for plot to histogram , first we have to get gray or black and white image pixel value. then we can plot histogram using this pixel value.so, how can i get this pixel value of black and white image?

Was it helpful?

Solution

This should work if you use java.awt.image.BufferedImage.

Since you want to create a histogram, I suppose you will loop through all the pixels. There is the method for returning a single pixel value.

int getRGB(int x, int y)

However, since looping will take place I suppose you'd want to use this one:

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

When you get the array, use:

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

To extract the channel data. Not sure if the variables can be declared as byte (we are using only one byte of the integer in the array, although byte is signed and different arithmetic takes place - two's complement form), but you can declare them as short.

Then preform some maths on these values, for example:

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

This will return the average for the pixel, giving you a point you can use in a simple luminosity histogram.

EDIT:

Regarding histogram creation, I have used this class. It takes the image you want the histogram of as an argument to its setImage(BufferedImage image) method. Use updateHistogram() for array populating. The drawing data is in paintComponent(Graphics g). I must admit, it is sloppy, especially when calculating the offsets, but it can be easily simplified.

Here is the whole class:

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
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top