Histogram from BufferedImage

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2620 Views - Last Post: 16 September 2011 - 08:51 PM Rate Topic: -----

#1 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Histogram from BufferedImage

Posted 10 September 2011 - 10:00 PM

Hey there, I am coming back into Java having not programed with it since I left uni about a year ago.
I currently am trying to write a program that creates an RGB histogram from a bufferedimage. However I haven't been able to find a lot of useful guidance on the web.

Does anyone know of any tutorials that are helpful? Many of the websites that explain it are very brief or incomplete .

Maybe someone could help me with the logic of it.

Is This A Good Question/Topic? 0
  • +

Replies To: Histogram from BufferedImage

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,490
  • Joined: 27-December 08

Re: Histogram from BufferedImage

Posted 10 September 2011 - 10:30 PM

Essentially, the BufferedImage is just there so you can getGraphics() from it. You use the Graphics object to draw your histogram. Remember that the origin in Java is the top-left corner. Check out the Graphics documentation.

From there, you can create an ImageIcon from the BufferedImage, and pass that to a JLabel to display.
Was This Post Helpful? 1
  • +
  • -

#3 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Histogram from BufferedImage

Posted 11 September 2011 - 03:10 AM

I've got my buffered image and up to creating the histogram. I have 2 things I need a bit of help with.

1. I'm using some code from a tutorial as my base however there were a few things that I didn't understand:

int height = image.getHeight();
        int width = image.getWidth();
        
        Raster raster = image.getRaster();
        for(int i=0; i < width ; i++)
        {
            for(int j=0; j < height ; j++)
            {
                //if(gray)
                //{
                //    bins[0][ raster.getSample(i,j, 0) ] ++;
                //}
                //else
                //{
                    bins[0][ raster.getSample(i,j, 0) ] ++;
                    bins[1][ raster.getSample(i,j, 1) ] ++;
                    bins[2][ raster.getSample(i,j, 2) ] ++;
                //}
            }
        }


I've commented out the if(gray) -- the tutorial was a summary and there was no mention of gray, why would I need that there? Shouldn't I just be able to fill the bins without it?
Also when I run this code I get a nullpointer exception, I've initialised the bins variable as int[][] bins = null; Am I doing this wrong?

2. Once I've filled by bins correctly, how do I plot the histogram, all the tutorials i've seen use JAI. I would prefer to use use a JPanel, is that possible?
How does the plot work, where can I find some info on that?

This post has been edited by l0ckz0r: 11 September 2011 - 03:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Histogram from BufferedImage

Posted 12 September 2011 - 12:39 AM

Just to explain more simply

1. I need to know, for an RGB histogram, do I need to have an if(gray) statement, what is it's purpose?

2. How do I draw the histogram in Java 2d once I have it in the bins[][] I can't find anything useful online
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,490
  • Joined: 27-December 08

Re: Histogram from BufferedImage

Posted 12 September 2011 - 06:58 AM

Why would you need an if statement to test if the Color is grey? Have you checked out the Graphics class yet? The fillRect() method seems appropriate.

Quote

Once I've filled by bins correctly, how do I plot the histogram, all the tutorials i've seen use JAI. I would prefer to use use a JPanel, is that possible?
How does the plot work, where can I find some info on that?

It looks like the JAI has a Histogram class. I've never worked with the JAI API before. If you wanted to do it with Swing, you can draw to a BufferedImage and create an ImageIcon from that to be displayed on a JLabel. Or you can extend JPanel, override the paint() (or paintComponent()) method and use the Graphics param for the method you override to draw the Image to the JPanel.
Was This Post Helpful? 1
  • +
  • -

#6 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Histogram from BufferedImage

Posted 13 September 2011 - 02:39 AM

Sorry what I need help is, I don't know how the drawing process of a histogram actually works

So I have my bins

[0][44000][0]...
[23][233][434]...
[232][123][23]...

I can't draw a line thats 44000 pixels long, so whats the process I need to go through?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,490
  • Joined: 27-December 08

Re: Histogram from BufferedImage

Posted 13 September 2011 - 08:09 AM

The keyword here is scale. If you have a bin with 4400, you need to determine what one pixel represents. Is one pixel = 20 in your bin? The API is very self-explanatory. I think you need to think it through logically now.
Was This Post Helpful? 0
  • +
  • -

#8 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Histogram from BufferedImage

Posted 15 September 2011 - 04:09 AM

Thanks for all your help - I have got it working. One final question now:

The first image here is my histogram - as you can see it's quite spiky and there's a few gaps
Attached Image

However the photoshop histogram for the same image is much more rounded out and there are no gaps - it's the same image:
Attached Image

Is it okay that my graphs are spiky and have gaps or do I need to use some algorithms to change it? Let me know if you need to see some of my code.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Histogram from BufferedImage

Posted 15 September 2011 - 06:16 PM

Try to make the JFrame wider to see if the problem persists
Seems that, due to some rounding, you skip a pixel X sometimes
Was This Post Helpful? 0
  • +
  • -

#10 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Histogram from BufferedImage

Posted 15 September 2011 - 09:24 PM

The Panels themselves are a little wider than the drawing, and they stay the same size no matter how big the Jframe is.

I actually checked the input and for say the colour red, there is no value of 3 - hence the gap. Is this normal for images? Or have I messed something up?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Histogram from BufferedImage

Posted 15 September 2011 - 09:35 PM

Obviously rounding error
If an X is absent, use the preceeding Y or the following Y or even better the mean between the two
Was This Post Helpful? 0
  • +
  • -

#12 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Histogram from BufferedImage

Posted 16 September 2011 - 06:54 AM

When I actually get the value from the raster getsample() function - there is no value of 3 for the red channel, hence the gap. So RedBin[3] never gets incremented. Is that still a rounding error? Do I still need to do the mean trick?
Was This Post Helpful? 0
  • +
  • -

#13 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Histogram from BufferedImage

Posted 16 September 2011 - 12:39 PM

if (RedBin[3] == 0) RedBin[3] = (RedBin[2] + RedBin[4]) / 2;
Is that the idea ?
Was This Post Helpful? 0
  • +
  • -

#14 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Re: Histogram from BufferedImage

Posted 16 September 2011 - 04:00 PM

That's the idea, but if there is no pixel with that value should I really be drawing it in the graph? I don't know how these things work.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Histogram from BufferedImage

Posted 16 September 2011 - 04:09 PM

If there is really no values chances that there will be no value at the left or the right or both so (0 + 0) / 2 = 0 or (5 + 0) / 2 = 2
You won't be far
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2