public static BufferedImage HistogramEqualization(BufferedImage image) {
      int width  = image.getWidth();
      int height = image.getHeight();
      int[] H = new int[256]; 
      
      BufferedImage temp = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
      
      for(int i = 0; i < 256; i++) 
         H[i] = 0;
      
      for(int x = 0; x < width; x++) {
         for(int y = 0; y < height; y++) {
            int pixel = image.getRGB(x, y) & 0x000000FF;
            
            H[pixel] += 1;
         }
      }
      
      for(int i = 1; i < 256; i++)
         H[i] = H[i-1] + H[i];
      
      for(int i = 0; i < 256; i++)
         H[i] = H[i]*255/(width*height);
      
      for(int x = 0; x < width; x++) {
         for(int y = 0; y < height; y++) {
            int pixel = image.getRGB(x, y) & 0x000000FF;
            int result = 0;
            
            int pVal = H[pixel];
            
            result = result | 0xFF000000;
            result = result | (pVal << 16);
            result = result | (pVal << 8);
            result = result | (pVal);
            
            temp.setRGB(x, y, result);
            
         }
      }
            
      return temp;
   }