Long time no see …

Kategorija: Faks,Programiranje,Razno — Napisal ZeroCool, 22.5.2010 @ 21:53

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 : ).

  1.     // ############################################################################################### //
  2.     //                             DRAWING HISTOGRAMS, MAKING HISTOGRAMS                               //
  3.     // ############################################################################################### //
  4.  
  5.     /*
  6.      * Draw an image histogram
  7.      *
  8.      * Author: Boštjan Cigan (ZeroCool), some rights reserved.
  9.      * http://zerocool.area52.us
  10.      * Can be distributed as long as comments stay intact.
  11.      *
  12.      * Function call:
  13.      * BufferedImage histogram = drawHistogram(image, color)
  14.      *
  15.      * Where color is an integer which represents:
  16.      * 0 – Red
  17.      * 1 – Green
  18.      * 2 – Blue
  19.      * 3 – Average
  20.      *
  21.      */
  22.     public static BufferedImage drawHistogram(BufferedImage input, int color) {
  23.  
  24.         int[] histogramValues = new int[256];
  25.         int pixel = 0;
  26.         int fill = colorToRGBNoAlpha(255, 255, 255);
  27.  
  28.         switch(color) {
  29.  
  30.             case 0: {
  31.                 histogramValues = imageHistogram(input).get(color);
  32.                 pixel = colorToRGBNoAlpha(255, 0, 0);
  33.                 break;
  34.             }
  35.             case 1: {
  36.                 histogramValues = imageHistogram(input).get(color);
  37.                 pixel = colorToRGBNoAlpha(0, 255, 0);
  38.                 break;
  39.             }
  40.             case 2: {
  41.                 histogramValues = imageHistogram(input).get(color);
  42.                 pixel = colorToRGBNoAlpha(0, 0, 255);
  43.                 break;
  44.             }
  45.             case 3: {
  46.                 histogramValues = imageHistogramAverage(input);
  47.                 pixel = colorToRGBNoAlpha(0, 0, 0);
  48.                 break;
  49.             }
  50.         }
  51.  
  52.         int maxVal = findMaxValue(histogramValues);
  53.         BufferedImage histogram = new BufferedImage(255, 130, BufferedImage.TYPE_3BYTE_BGR);
  54.  
  55.         float drawValue = (float) (130.0/maxVal);
  56.  
  57.         int[] drawedValues = new int[256];
  58.         for(int i=0; i<drawedValues.length; i++) {
  59.             drawedValues[i] = (int) (histogramValues[i]*drawValue);
  60.         }
  61.  
  62.         for(int i=0; i<histogram.getWidth(); i++) {
  63.             for(int l=0; l<histogram.getHeight(); l++) {
  64.                 histogram.setRGB(i, l, fill);
  65.             }
  66.         }
  67.  
  68.         for(int j=0; j<histogram.getWidth(); j++) {
  69.  
  70.             if(drawedValues[j] > 0) {
  71.                 histogram.setRGB(j, Math.abs(drawedValues[j]-130), pixel);
  72.  
  73.                 for(int k=Math.abs(drawedValues[j]); k>0; k–) {
  74.                     histogram.setRGB(j, Math.abs(k-130), pixel);
  75.                 }
  76.  
  77.             }
  78.         }
  79.  
  80.         return histogram;
  81.  
  82.     }
  83.  
  84.     // Return a histogram containing average values from R, G, B channels
  85.     public static int[] imageHistogramAverage(BufferedImage input) {
  86.  
  87.         int[] histogram = new int[256];
  88.  
  89.         for(int i=0; i<input.getWidth(); i++) {
  90.             for(int j=0; j<input.getHeight(); j++) {
  91.  
  92.                 int red = new Color(input.getRGB (i, j)).getRed();
  93.                 int green = new Color(input.getRGB (i, j)).getGreen();
  94.                 int blue = new Color(input.getRGB (i, j)).getBlue();
  95.  
  96.                 int avg = (int) (red + green + blue)/3;
  97.                 histogram[avg]++;
  98.  
  99.             }
  100.         }
  101.  
  102.         return histogram;
  103.  
  104.     }
  105.  
  106.     // Return an ArrayList containing histogram values for separate R, G, B channels
  107.     public static ArrayList<int[]> imageHistogram(BufferedImage input) {
  108.  
  109.         int[] rhistogram = new int[256];
  110.         int[] ghistogram = new int[256];
  111.         int[] bhistogram = new int[256];
  112.  
  113.         for(int i=0; i<rhistogram.length; i++) rhistogram[i] = 0;
  114.         for(int i=0; i<ghistogram.length; i++) ghistogram[i] = 0;
  115.         for(int i=0; i<bhistogram.length; i++) bhistogram[i] = 0;
  116.  
  117.         for(int i=0; i<input.getWidth(); i++) {
  118.             for(int j=0; j<input.getHeight(); j++) {
  119.  
  120.                 int red = new Color(input.getRGB (i, j)).getRed();
  121.                 int green = new Color(input.getRGB (i, j)).getGreen();
  122.                 int blue = new Color(input.getRGB (i, j)).getBlue();
  123.  
  124.                 rhistogram[red]++; ghistogram[green]++; bhistogram[blue]++;
  125.  
  126.             }
  127.         }
  128.  
  129.         ArrayList<int[]> hist = new ArrayList<int[]>();
  130.         hist.add(rhistogram);
  131.         hist.add(ghistogram);
  132.         hist.add(bhistogram);
  133.  
  134.         return hist;
  135.  
  136.     }
  137.  
  138.     // Find max value in an array
  139.     public static int findMaxValue(int[] input) {
  140.        
  141.         int maximum = input[0];
  142.         for(int i=1; i<input.length; i++) {
  143.             if(input[i] > maximum) {
  144.                 maximum = input[i];
  145.             }
  146.         }
  147.        
  148.         return maximum;
  149.    
  150.     }
  151.  
  152.     // Convert R, G, B to standard 8 bit
  153.     private static int colorToRGBNoAlpha(int red, int green, int blue) {
  154.  
  155.         int newPixel = 0;
  156.         newPixel += red; newPixel = newPixel << 8;
  157.         newPixel += green; newPixel = newPixel << 8;
  158.         newPixel += blue;
  159.  
  160.         return newPixel;
  161.  
  162.     }
Teče na Wordpress | Powered by Kaley, Gostuje SCE
© 2006-10 Boštjan Cigan (ZeroCool). Vse pravice pridržane.