Long time no see …
Razlog … faks. Zadnje čase se ukvarjam z manipulacijo slik v Javi. Se pravi risanje histogramov, urejanjem kontrasta, enačenjem histograma ipd. Če bo mogoče komu koristila tale majčkena stvarca; funkcija sprejme BufferedImage in en integer (razlaga v komentarju kode), vrne pa sliko histograma velikosti 255×150.
Malce več razlage bom dopisal naslednjič, ko bo več časa : ).
-
// ############################################################################################### //
-
// DRAWING HISTOGRAMS, MAKING HISTOGRAMS //
-
// ############################################################################################### //
-
-
/*
-
* Draw an image histogram
-
*
-
* Author: Boštjan Cigan (ZeroCool), some rights reserved.
-
* http://zerocool.area52.us
-
* Can be distributed as long as comments stay intact.
-
*
-
* Function call:
-
* BufferedImage histogram = drawHistogram(image, color)
-
*
-
* Where color is an integer which represents:
-
* 0 – Red
-
* 1 – Green
-
* 2 – Blue
-
* 3 – Average
-
*
-
*/
-
public static BufferedImage drawHistogram(BufferedImage input, int color) {
-
-
int[] histogramValues = new int[256];
-
int pixel = 0;
-
int fill = colorToRGBNoAlpha(255, 255, 255);
-
-
switch(color) {
-
-
case 0: {
-
histogramValues = imageHistogram(input).get(color);
-
pixel = colorToRGBNoAlpha(255, 0, 0);
-
break;
-
}
-
case 1: {
-
histogramValues = imageHistogram(input).get(color);
-
pixel = colorToRGBNoAlpha(0, 255, 0);
-
break;
-
}
-
case 2: {
-
histogramValues = imageHistogram(input).get(color);
-
pixel = colorToRGBNoAlpha(0, 0, 255);
-
break;
-
}
-
case 3: {
-
histogramValues = imageHistogramAverage(input);
-
pixel = colorToRGBNoAlpha(0, 0, 0);
-
break;
-
}
-
}
-
-
int maxVal = findMaxValue(histogramValues);
-
BufferedImage histogram = new BufferedImage(255, 130, BufferedImage.TYPE_3BYTE_BGR);
-
-
float drawValue = (float) (130.0/maxVal);
-
-
int[] drawedValues = new int[256];
-
for(int i=0; i<drawedValues.length; i++) {
-
drawedValues[i] = (int) (histogramValues[i]*drawValue);
-
}
-
-
for(int i=0; i<histogram.getWidth(); i++) {
-
for(int l=0; l<histogram.getHeight(); l++) {
-
histogram.setRGB(i, l, fill);
-
}
-
}
-
-
for(int j=0; j<histogram.getWidth(); j++) {
-
-
if(drawedValues[j] > 0) {
-
histogram.setRGB(j, Math.abs(drawedValues[j]-130), pixel);
-
-
for(int k=Math.abs(drawedValues[j]); k>0; k–) {
-
histogram.setRGB(j, Math.abs(k-130), pixel);
-
}
-
-
}
-
}
-
-
return histogram;
-
-
}
-
-
// Return a histogram containing average values from R, G, B channels
-
public static int[] imageHistogramAverage(BufferedImage input) {
-
-
int[] histogram = new int[256];
-
-
for(int i=0; i<input.getWidth(); i++) {
-
for(int j=0; j<input.getHeight(); j++) {
-
-
int red = new Color(input.getRGB (i, j)).getRed();
-
int green = new Color(input.getRGB (i, j)).getGreen();
-
int blue = new Color(input.getRGB (i, j)).getBlue();
-
-
int avg = (int) (red + green + blue)/3;
-
histogram[avg]++;
-
-
}
-
}
-
-
return histogram;
-
-
}
-
-
// Return an ArrayList containing histogram values for separate R, G, B channels
-
public static ArrayList<int[]> imageHistogram(BufferedImage input) {
-
-
int[] rhistogram = new int[256];
-
int[] ghistogram = new int[256];
-
int[] bhistogram = new int[256];
-
-
for(int i=0; i<rhistogram.length; i++) rhistogram[i] = 0;
-
for(int i=0; i<ghistogram.length; i++) ghistogram[i] = 0;
-
for(int i=0; i<bhistogram.length; i++) bhistogram[i] = 0;
-
-
for(int i=0; i<input.getWidth(); i++) {
-
for(int j=0; j<input.getHeight(); j++) {
-
-
int red = new Color(input.getRGB (i, j)).getRed();
-
int green = new Color(input.getRGB (i, j)).getGreen();
-
int blue = new Color(input.getRGB (i, j)).getBlue();
-
-
rhistogram[red]++; ghistogram[green]++; bhistogram[blue]++;
-
-
}
-
}
-
-
ArrayList<int[]> hist = new ArrayList<int[]>();
-
hist.add(rhistogram);
-
hist.add(ghistogram);
-
hist.add(bhistogram);
-
-
return hist;
-
-
}
-
-
// Find max value in an array
-
public static int findMaxValue(int[] input) {
-
-
int maximum = input[0];
-
for(int i=1; i<input.length; i++) {
-
if(input[i] > maximum) {
-
maximum = input[i];
-
}
-
}
-
-
return maximum;
-
-
}
-
-
// Convert R, G, B to standard 8 bit
-
private static int colorToRGBNoAlpha(int red, int green, int blue) {
-
-
int newPixel = 0;
-
newPixel += red; newPixel = newPixel << 8;
-
newPixel += green; newPixel = newPixel << 8;
-
newPixel += blue;
-
-
return newPixel;
-
-
}
Danes sem ugotovil, kako je v PHP-ju življenje enostavnejše. Če sem tam želel zakodirati niz z MD5, sem samo klical funkcijo md5($niz). V Javi je pa to seveda malce drugače. Da marsikomu (ki bo mogoče to rabil) olajšam življenje, prilagam kodo ; )









Web 2.0
Koledar
Citat trenutka
Prijatelji
Blog