Advertisement
Guest User

Picture.java

a guest
May 25th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.80 KB | None | 0 0
  1. import javax.imageio.ImageIO;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.awt.Color;
  8. import java.awt.Graphics2D;
  9.  
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13.  
  14. public class Picture {
  15.     private BufferedImage image;
  16.     private JFrame f;
  17.  
  18.     public Picture(int w, int h) {
  19.         image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  20.     }
  21.  
  22.     public Picture(String fileName) { this(new File(fileName)); }
  23.  
  24.     public Picture(File file) {
  25.         try {
  26.             image = ImageIO.read(file);
  27.         } catch(IOException e) {}
  28.     }
  29.  
  30.     public Picture(Picture picture) {
  31.         int w = picture.getWidth();
  32.         int h = picture.getHeight();    
  33.         this.image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  34.         Graphics2D g2 = this.image.createGraphics();
  35.         g2.drawImage(picture.getBufferedImage(), 0, 0, null);
  36.     }
  37.  
  38.     public int getHeight() { return image.getHeight(null); }
  39.     public int getWidth()  { return image.getWidth(null); }
  40.  
  41.     public int getRGB(int i, int j) {
  42.         return image.getRGB(i, j);
  43.     }
  44.  
  45.     public Color getColor(int i, int j) {
  46.         return new Color(getRGB(i, j));
  47.     }
  48.  
  49.     public void setGray(int i, int j, int c) {
  50.         Color color = new Color(c, c, c);
  51.         setColor(i, j, color);
  52.     }
  53.  
  54.     public void setColor(int i, int j, Color c) {
  55.         image.setRGB(i, j, c.getRGB());
  56.     }
  57.  
  58.     public int getGray(int i, int j) {
  59.         Color color = getColor(i, j);
  60.         int r = color.getRed();
  61.         int g = color.getGreen();
  62.         int b = color.getBlue();
  63.         int luminance = (int) (0.299*r + 0.587*g + 0.114*b);
  64.         return luminance;
  65.     }
  66.  
  67.     public BufferedImage getBufferedImage() {
  68.         return image;
  69.     }
  70.  
  71.     public void grayScale() {
  72.         int w = image.getWidth(null);
  73.         int h = image.getHeight(null);
  74.         for (int i = 0; i < w; i++) {
  75.             for (int j = 0; j < h; j++) {
  76.                 setGray(i, j, getGray(i, j));
  77.             }
  78.         }
  79.     }
  80.  
  81.     public void binarise(int threshold) {
  82.         int w = image.getWidth(null);
  83.         int h = image.getHeight(null);
  84.         for (int i = 0; i < w; i++) {
  85.             for (int j = 0; j < h; j++) {
  86.                 image.setRGB(i, j, ((getRGB(i, j) & 0xff) > threshold) ? 0xffffffff : 0xff000000);
  87.             }
  88.         }
  89.     }
  90.  
  91.     public void otsuMethod() {
  92.         final int n = 51;
  93.         int space = (n-1)/2;
  94.         int[] histo = histogram();
  95.         int[] hBig = new int[histo.length+2*space];
  96.  
  97.         for (int i = 0; i < histo.length; i++)
  98.             hBig[i+space] = histo[i];
  99.  
  100.         int[] histo2 = new int[histo.length];
  101.         for (int i = 0; i < histo.length; i++) {
  102.             int sum = 0;
  103.             for (int j = 0; j < n; j++)
  104.                 sum += hBig[i+j];
  105.             histo2[i] = sum/n;
  106.         }
  107.         int[] min = minimums(histo2);
  108.         int threshold = 0;
  109.         for (int i = 0; i < min.length; i++)
  110.             if (min[i] > threshold) threshold = min[i];
  111.  
  112.         binarise(threshold);
  113.     }
  114.  
  115.     public int[] histogram() {
  116.         int w = getWidth();
  117.         int h = getHeight();  
  118.         int[] histo = new int[256];
  119.         for (int i = 0; i < w; i++) {
  120.             for (int j = 0; j < h; j++) {
  121.                 int color = getRGB(i, j) & 0xff;
  122.                 histo[color]++;
  123.             }
  124.         }
  125.         return histo;
  126.     }
  127.  
  128.     private int[] minimums(int[] function) {
  129.         final int up = 1;
  130.         final int down = -1;
  131.         ArrayList values = new ArrayList();
  132.         int x = 0;
  133.         int way = 0;
  134.         for (int i = 0; i < function.length; i++) {
  135.             if (function[i] > x) {
  136.                 if (way == down) values.add(new Integer(i-1));
  137.                 x = function[i];
  138.                 way = up;
  139.             }
  140.             if (function[i] < x) {
  141.                 x = function[i];
  142.                 way = down;
  143.             }
  144.         }
  145.         int[] vector = new int[values.size()];
  146.         for (int i = 0; i < vector.length; i++) {
  147.             vector[i] = ((Integer)values.get(i)).intValue();
  148.         }
  149.         return vector;
  150.     }
  151.  
  152.     public void lbp() {
  153.         Picture p = new Picture(this);
  154.         int w = image.getWidth(null);
  155.         int h = image.getHeight(null);
  156.         for (int i = 0; i < w; i++) {
  157.             for (int j = 0; j < h; j++) {
  158.                 ArrayList<Integer> tmp = new ArrayList<Integer>();
  159.                 try { tmp.add(image.getRGB(i-1, j-1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
  160.                 try { tmp.add(image.getRGB(i-1, j) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
  161.                 try { tmp.add(image.getRGB(i-1, j+1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
  162.                 try { tmp.add(image.getRGB(i, j+1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
  163.                 try { tmp.add(image.getRGB(i+1, j+1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
  164.                 try { tmp.add(image.getRGB(i+1, j) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
  165.                 try { tmp.add(image.getRGB(i+1, j-1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
  166.                 try { tmp.add(image.getRGB(i, j-1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
  167.                 int tmpByte = 0;
  168.                 for (int ii = 0; ii < tmp.size(); ii++)
  169.                     tmpByte |= (tmp.get(ii) << (ii));
  170.                 p.setGray(i, j, tmpByte);
  171.             }
  172.         }
  173.         image = p.getBufferedImage();
  174.     }
  175.  
  176.  
  177.     public JLabel getJLabel() {
  178.         if (image == null) return null;
  179.         ImageIcon icon = new ImageIcon(image);
  180.         return new JLabel(icon);
  181.     }
  182.  
  183.     public void show() {
  184.         if (f == null) {
  185.             f = new JFrame();
  186.             f.setContentPane(getJLabel());
  187.             f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  188.             f.setTitle("Picture show");
  189.             f.setResizable(false);
  190.             f.pack();
  191.             f.setVisible(true);
  192.         }
  193.         f.repaint();
  194.     }
  195.  
  196.     public void save(String filename) {
  197.         save(new File(filename));
  198.     }
  199.  
  200.     public void save(File file) {
  201.         String filename = file.getName();
  202.         String suffix = filename.substring(filename.lastIndexOf('.') + 1);
  203.         try {
  204.             ImageIO.write(image, suffix, file);
  205.         } catch (IOException e) {}
  206.     }
  207.  
  208.     public static void main(String[] args) {
  209.         Picture pic = new Picture(args[0]);
  210.         pic.grayScale();
  211.         pic.lbp();
  212.         pic.show();
  213.         pic.save("lbp.png");
  214.     }
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement