2 Replies Latest reply: Oct 25, 2012 9:55 AM by 970531 RSS

    content base image retrieval- code not working

    970531
      //package main1;
      import java.awt.*;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.io.File;
      import javax.swing.ImageIcon;
      import javax.swing.JButton;
      import javax.swing.JFileChooser;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JTextArea;
      import javax.swing.JTextField;
      import java.awt.Image;

      import java.applet.*;
      import java.awt.image.PixelGrabber;
      import java.awt.image.ColorModel;
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Container;
      import java.awt.FlowLayout;
      import java.awt.GridBagConstraints;
      import java.awt.GridBagLayout;

      import java.awt.Rectangle;
      import java.awt.Shape;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import java.io.FileInputStream;
      import java.util.ArrayList;
      import java.util.Date;

      import javax.swing.Icon;
      import javax.swing.JButton;

      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.JOptionPane;

      import javax.swing.JScrollPane;



      import java.awt.image.BufferedImage;
      import java.awt.image.ImageObserver;
      import java.io.IOException;
      import java.net.MalformedURLException;
      import java.net.URI;
      import java.net.URL;
      import java.util.logging.Level;
      import java.util.logging.Logger;
      import javax.imageio.ImageIO;

      class CBIR extends JFrame implements ActionListener {

      JPanel panel ;
      JButton BROWSE = new JButton("BROWSE");
      JButton SEARCH= new JButton("SEARCH");
      JTextField tf = new JTextField(10);
      JTextArea textarea;
      JLabel l1, l2, l3;
      File file;
      String path;
      JFileChooser fc=new JFileChooser();
      JFrame frame;
      ImageIcon icon;
      Container cp;
      ArrayList<int[]> a1;

      public CBIR()
      {
      frame = new JFrame("CONTENT BASED IMAGE RETREIVAL SYSTEM");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(300,300);
      panel=new JPanel();
      panel.add(tf);
      panel.add(BROWSE);
      panel.add(SEARCH);
      frame.getContentPane().add(BorderLayout.SOUTH,panel);
      frame.setVisible(true);
      BROWSE.addActionListener(this);
      SEARCH.addActionListener(this);
      }

      public static void main(String[] args)
      {
      CBIR cb = new CBIR();

      }

      public void actionPerformed(ActionEvent e)
      {
      String command = e.getActionCommand();
      if (command.equals("BROWSE"))
      {

      //if (e.getSource() == BROWSE)
      {
      int returnVal = fc.showOpenDialog(CBIR.this);
      if (returnVal == JFileChooser.APPROVE_OPTION)
      {
      File file = fc.getSelectedFile();
      path=file.getPath();

      tf.setText(file.getPath());
      }

      }
      }
      if (command.equals("SEARCH"))
      {

      path=fc.getSelectedFile().getAbsolutePath().toString();
      l1=new JLabel();
      icon=new ImageIcon(path);
      l1.setIcon(icon);
      panel.add(l1);
      Image image1=iconToImage(icon);
      BufferedImage b1=toBufferedImage(image1);
      a1=imageHistogram(b1);
      try {
      comparehistogram();
      } catch (MalformedURLException ex) {
      Logger.getLogger(CBIR.class.getName()).log(Level.SEVERE, null, ex);
      }
      catch(IllegalArgumentException e1){
      e1.getCause();}
      }
      }

      public void comparehistogram() throws MalformedURLException{
      float similarity = 0;
      int[] targetHistogramData ;
      int[] targetCandidateHistogramData ;
      File file = new File("C:/Program Files (x86)/Java/jdk1.6.0/bin");
      URI url= file.toURI();

      for(int i=1;i<95;i++){


      ImageIcon icon2= new ImageIcon(url+"/"+i+".jpeg");
      Image image2=iconToImage(icon2);
      BufferedImage b2=toBufferedImage(image2);
      ArrayList<int[]> a2=imageHistogram(b2);
      for(int j=0;j<3;j++){
      targetHistogramData = a2.get(j);
      targetCandidateHistogramData = a1.get(j);

      //if( targetHistogramData.length != targetCandidateHistogramData.length){
      //throw new IncompatibleHistogramsException();
      //}

      //for(int k = 0; k < targetHistogramData.length; k++){
      similarity += Math.sqrt(targetHistogramData[j]*targetCandidateHistogramData[j]);
      //}
      }
      if(similarity>0.5){
      JLabel l2=new JLabel();

      l2.setIcon(icon2);
      panel.add(l2);

      }
      }
      //return similarity;


      /*catch (MalformedURLException ex) {
      Logger.getLogger(CBIR.class.getName()).log(Level.SEVERE, null, ex);
      }*/
      }


      private Image iconToImage(Icon icon)
      {
      if(icon instanceof ImageIcon)
      {
      return ((ImageIcon) icon).getImage();
      }
      else
      {
      BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);

      icon.paintIcon(new JPanel(), image.getGraphics(), 0, 0);
      return image;
      }
      }








      // This method returns a buffered image with the contents of an image
      public static BufferedImage toBufferedImage(Image image) {
      if (image instanceof BufferedImage) {
      return (BufferedImage)image;
      }

      // This code ensures that all the pixels in the image are loaded
      image = new ImageIcon(image).getImage();
      // Determine if the image has transparent pixels; for this method's
      // implementation, see Determining If an Image Has Transparent Pixels
      //boolean hasAlpha = hasAlpha(image);

      // Create a buffered image with a format that's compatible with the screen
      BufferedImage bimage = null;
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      try {
      // Determine the type of transparency of the new buffered image
      int transparency = Transparency.OPAQUE;
      // if (hasAlpha) {
      // transparency = Transparency.BITMASK;
      //}

      // Create the buffered image
      ImageObserver img = null;
      GraphicsDevice gs = ge.getDefaultScreenDevice();
      GraphicsConfiguration gc = gs.getDefaultConfiguration();
      bimage = gc.createCompatibleImage( image.getWidth(img), image.getHeight(null), transparency);
      } catch (HeadlessException e) {
      // The system does not have a screen
      }

      if (bimage == null) {
      // Create a buffered image using the default color model
      int type = BufferedImage.TYPE_INT_RGB;
      //if (hasAlpha) {
      // type = BufferedImage.TYPE_INT_ARGB;
      //}
      bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
      }

      // Copy image to buffered image
      Graphics g = bimage.createGraphics();

      // Paint the image onto the buffered image
      g.drawImage(image, 0, 0, null);
      g.dispose();

      return bimage;
      }






      // This method returns true if the specified image has transparent pixels
      /*public static boolean hasAlpha(Image image) {
      // If buffered image, the color model is readily available
      if (image instanceof BufferedImage) {
      BufferedImage bimage = (BufferedImage)image;
      return bimage.getColorModel().hasAlpha();
      }

      // Use a pixel grabber to retrieve the image's color model;
      // grabbing a single pixel is usually sufficient
      PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
      try {
      pg.grabPixels();
      } catch (InterruptedException e) {
      }

      // Get the image's color model
      ColorModel cm = pg.getColorModel();
      return cm.hasAlpha();
      }*/

      // create an histogram

      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();

      // Increase the values of colors
      rhistogram[red]++; ghistogram[green]++; bhistogram[blue]++;

      }
      }

      ArrayList<int[]> hist = new ArrayList<int[]>();
      hist.add(rhistogram);
      hist.add(ghistogram);
      hist.add(bhistogram);

      return hist;

      }

      /*public float comparehistogram(){
      //bhattacharya coeficcent method to compare histograms
      float similarity = 0;
      databasehistogram d1;
      ArrayList<int[]> a1= d1.createhistogram();

      for(int i=0;i<94;i++){
      int[] targetHistogramData = a1.get(i);}
      float[] targetCandidateHistogramData = ...//histogram2.getData();

      if( targetHistogramData.length != targetCandidateHistogramData.length){
      throw new IncompatibleHistogramsException();
      }

      for(int i = 0; i < targetHistogramData.length; i++){
      similarity += Math.sqrt(targetHistogramData*targetCandidateHistogramData[i]);
      }

      return similarity;

      }*/
      }