Drawing an rbg histogram for a picture
Hi,
So I'm currently playing around with some code for a project and I want to be able to get a histogram from a picture that shows me the distributions of color in that picture. Now I have a general idea of how this would work but there are some things that are still fuzzy in my head so I would love if someone could look over my idea here and tell me if it has any merit.
So I want to begin by filling in my histogram with data. If my histogram is represented by a 2d array then it would look something like this:
Code:
int [] [] histogram = new int [3][255]
Thus creating a 2d array to hold the 3 bands (RBG) and the frequency of each.
So I can now create a 2 for loops to loop through my image and for each pixel increase the frequency count for its specific color like this:
Code:
for ( int i = 0; i < image.width;i++){
for (int j = 0 ; j < image.height; j++){
histogram [0] [image.getSample (i,j,0)] ++;
histogram [1] [image.getSample (i,j,1)] ++;
histogram [2] [image.getSample (i,j,2)] ++;
I now have the data for my histogram correct? So here is where I kinda don't know what to do. How do I draw this data? I am not very familiar with Java2D, from my current understanding, I would have to create a new JPanel and create a method there called paint component. But what should I do from there? Draw 255 lines of varying height really close to each other? Also where can I get this graphics g object that seemingly every method that has anything to do with drawing wants?
Thanks in advance for any help.
Re: Drawing an rbg histogram for a picture
Sounds like a bar chart, for which you could use JFreeChart. But if you need to do this yourself, go through Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) and Trail: 2D Graphics (The Java™ Tutorials) where all your questions will be answered.
db