Why need histograms??
Image histograms tell us a lot about their respective images. For example, a histogram with spaced lines tells us that its image has been previously scaled to contrast it. There will be a different post soon, that will tell you how important is the histograms. For now, we will see only, how to create histograms of images.
Extra software:
Although you have ways to make histograms programmatically, but to reduce the effort for the learning process, you can rely on software that can show spreadsheets. I am using Microsoft Excel 2013.
The Code:
int hist[];
String Hist;
public void makeHistogram(BufferedImage img,int bins){//woring on it
int N=bins;
hist=new int[N];
for(int bright=0;bright<N;bright++)
hist[bright]=0;
int width=img.getWidth();
int height=img.getHeight();
int[][] result=new int[width][height];
for(int row=0;row<height;row++){
for(int col=0;col<width;col++){
result[col][row]=img.getRGB(col, row);
Color c=new Color(result[col][row]);
int red=c.getRed();
int blue=c.getBlue();
int green=c.getGreen();
int bright=(int)((red+blue+green)/3);
int index=(int)((float)N*bright/bins);
hist[index]++;
}
}
for(int bright=0;bright<N;bright++){
Hist=Hist+","+hist[bright];
}
}
In the code, the "bright" is calculated for each pixel, which is the average value of red, blue, and green values of the pixel. "bins" are nothing but defines the domain area of the histogram. It may be 256, 64, etc.
We are storing the array hist[] in String Hist, separated by commas. Contents in Hist then can be easily copied to a file. Such files be can easily analysed by these simple steps.
Here is an example:
No comments:
Post a Comment