Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class Picture {
- private BufferedImage image;
- private JFrame f;
- public Picture(int w, int h) {
- image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
- }
- public Picture(String fileName) { this(new File(fileName)); }
- public Picture(File file) {
- try {
- image = ImageIO.read(file);
- } catch(IOException e) {}
- }
- public Picture(Picture picture) {
- int w = picture.getWidth();
- int h = picture.getHeight();
- this.image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g2 = this.image.createGraphics();
- g2.drawImage(picture.getBufferedImage(), 0, 0, null);
- }
- public int getHeight() { return image.getHeight(null); }
- public int getWidth() { return image.getWidth(null); }
- public int getRGB(int i, int j) {
- return image.getRGB(i, j);
- }
- public Color getColor(int i, int j) {
- return new Color(getRGB(i, j));
- }
- public void setGray(int i, int j, int c) {
- Color color = new Color(c, c, c);
- setColor(i, j, color);
- }
- public void setColor(int i, int j, Color c) {
- image.setRGB(i, j, c.getRGB());
- }
- public int getGray(int i, int j) {
- Color color = getColor(i, j);
- int r = color.getRed();
- int g = color.getGreen();
- int b = color.getBlue();
- int luminance = (int) (0.299*r + 0.587*g + 0.114*b);
- return luminance;
- }
- public BufferedImage getBufferedImage() {
- return image;
- }
- public void grayScale() {
- int w = image.getWidth(null);
- int h = image.getHeight(null);
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < h; j++) {
- setGray(i, j, getGray(i, j));
- }
- }
- }
- public void binarise(int threshold) {
- int w = image.getWidth(null);
- int h = image.getHeight(null);
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < h; j++) {
- image.setRGB(i, j, ((getRGB(i, j) & 0xff) > threshold) ? 0xffffffff : 0xff000000);
- }
- }
- }
- public void otsuMethod() {
- final int n = 51;
- int space = (n-1)/2;
- int[] histo = histogram();
- int[] hBig = new int[histo.length+2*space];
- for (int i = 0; i < histo.length; i++)
- hBig[i+space] = histo[i];
- int[] histo2 = new int[histo.length];
- for (int i = 0; i < histo.length; i++) {
- int sum = 0;
- for (int j = 0; j < n; j++)
- sum += hBig[i+j];
- histo2[i] = sum/n;
- }
- int[] min = minimums(histo2);
- int threshold = 0;
- for (int i = 0; i < min.length; i++)
- if (min[i] > threshold) threshold = min[i];
- binarise(threshold);
- }
- public int[] histogram() {
- int w = getWidth();
- int h = getHeight();
- int[] histo = new int[256];
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < h; j++) {
- int color = getRGB(i, j) & 0xff;
- histo[color]++;
- }
- }
- return histo;
- }
- private int[] minimums(int[] function) {
- final int up = 1;
- final int down = -1;
- ArrayList values = new ArrayList();
- int x = 0;
- int way = 0;
- for (int i = 0; i < function.length; i++) {
- if (function[i] > x) {
- if (way == down) values.add(new Integer(i-1));
- x = function[i];
- way = up;
- }
- if (function[i] < x) {
- x = function[i];
- way = down;
- }
- }
- int[] vector = new int[values.size()];
- for (int i = 0; i < vector.length; i++) {
- vector[i] = ((Integer)values.get(i)).intValue();
- }
- return vector;
- }
- public void lbp() {
- Picture p = new Picture(this);
- int w = image.getWidth(null);
- int h = image.getHeight(null);
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < h; j++) {
- ArrayList<Integer> tmp = new ArrayList<Integer>();
- try { tmp.add(image.getRGB(i-1, j-1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
- try { tmp.add(image.getRGB(i-1, j) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
- try { tmp.add(image.getRGB(i-1, j+1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
- try { tmp.add(image.getRGB(i, j+1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
- try { tmp.add(image.getRGB(i+1, j+1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
- try { tmp.add(image.getRGB(i+1, j) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
- try { tmp.add(image.getRGB(i+1, j-1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
- try { tmp.add(image.getRGB(i, j-1) < image.getRGB(i, j) ? 1 : 0); } catch (Exception e) { tmp.add(0); }
- int tmpByte = 0;
- for (int ii = 0; ii < tmp.size(); ii++)
- tmpByte |= (tmp.get(ii) << (ii));
- p.setGray(i, j, tmpByte);
- }
- }
- image = p.getBufferedImage();
- }
- public JLabel getJLabel() {
- if (image == null) return null;
- ImageIcon icon = new ImageIcon(image);
- return new JLabel(icon);
- }
- public void show() {
- if (f == null) {
- f = new JFrame();
- f.setContentPane(getJLabel());
- f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- f.setTitle("Picture show");
- f.setResizable(false);
- f.pack();
- f.setVisible(true);
- }
- f.repaint();
- }
- public void save(String filename) {
- save(new File(filename));
- }
- public void save(File file) {
- String filename = file.getName();
- String suffix = filename.substring(filename.lastIndexOf('.') + 1);
- try {
- ImageIO.write(image, suffix, file);
- } catch (IOException e) {}
- }
- public static void main(String[] args) {
- Picture pic = new Picture(args[0]);
- pic.grayScale();
- pic.lbp();
- pic.show();
- pic.save("lbp.png");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement