⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 visualdescriptorvisualizationthread.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Caliph & Emir is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2002-2005 by Mathias Lux (mathias@juggle.at)
 * http://www.juggle.at, http://caliph-emir.sourceforge.net
 */
package at.lux.fotoretrieval;

import at.lux.components.ProgressWindow;
import at.lux.fotoretrieval.panels.Visualization2DPanelWithFdp;
import at.lux.imageanalysis.ColorLayout;
import at.lux.imageanalysis.EdgeHistogram;
import at.lux.imageanalysis.ScalableColor;
import at.lux.imageanalysis.VisualDescriptor;
import at.lux.retrieval.fastmap.ArrayFastmapDistanceMatrix;
import at.lux.retrieval.fastmap.FastMap;
import at.lux.retrieval.fastmap.FastmapDistanceMatrix;
import at.lux.retrieval.fastmap.VisualDescriptorDistanceCalculator;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * SimilarImageSearchThread
 *
 * @author Mathias Lux, mathias@juggle.at
 */
public class VisualDescriptorVisualizationThread extends Thread {
    String dir;
    RetrievalFrame parent;
    JProgressBar progress;
    DecimalFormat df;
    VisualDescriptor.Type type;
    Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    LinkedList<String> fileList;

    public VisualDescriptorVisualizationThread(String directory,
                                               RetrievalFrame frame, JProgressBar progress, VisualDescriptor.Type type) {
        this.dir = directory;
        this.parent = frame;
        this.progress = progress;
        df = (DecimalFormat) NumberFormat.getInstance();
        df.setMaximumFractionDigits(2);
        this.type = type;

    }

    public void run() {
        File directory = new File(dir);
        if (directory.exists()) {
            parent.setEnabled(false);
            ProgressWindow pw;
            pw = new ProgressWindow(parent, progress);
            try {
                pw.pack();
                Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
                pw.setLocation((d.width - pw.getWidth()) / 2, (d.height - pw.getHeight()) / 2);
                pw.setVisible(true);
                long stime, ftime;
                stime = System.currentTimeMillis();
                parent.setStatus("Loading & Mapping");

                FastmapDistanceMatrix matrixFastmap = getMatrix(directory, progress);
                FastMap fm = new FastMap(matrixFastmap, 2);
                long ms = System.currentTimeMillis();
                fm.run();
                ms = System.currentTimeMillis() - ms;
                System.out.println("Time for " + matrixFastmap.getDimension() + " images: " + ms + " ms");


//        Vector results = engine.getSimilarImages(ColorLayoutDescriptor, dir, recursive, progress);
                stime = System.currentTimeMillis() - stime;
                ftime = System.currentTimeMillis();
                matrixFastmap.normalize();
                Visualization2DPanelWithFdp panel = new Visualization2DPanelWithFdp(fm.getPoints(), matrixFastmap, fileList, false);
//                Visualization2DPanel panel = new Visualization2DPanel(fm.getPoints(), fileList);
//        ResultsPanel rp = new ResultsPanel(results, progress);
                parent.setStatus("Formatting results ...");
                ftime = System.currentTimeMillis() - ftime;
                parent.addVisualization(panel);
                parent.setStatus("Searched for " + df.format(stime / 1000.0) + " s, formatting lasted " + df.format(ftime / 1000.0) + " s");
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } finally {
                pw.setVisible(false);
                parent.setEnabled(true);
            }
        } else {
            JOptionPane.showMessageDialog(parent, "Chosen repositors directory does not exist.");
        }
    }

    private FastmapDistanceMatrix getMatrix(File directory, JProgressBar progress) throws IOException {
        String[] files;
        files = FileOperations.getAllDescriptions(directory, true);
        LinkedList<ColorLayout> colorLayoutList = new LinkedList<ColorLayout>();
        LinkedList<ScalableColor> scalableColorList = new LinkedList<ScalableColor>();
        LinkedList<EdgeHistogram> edgeHistogramList = new LinkedList<EdgeHistogram>();
        LinkedList<String> clFileList = new LinkedList<String>();
        LinkedList<String> scFileList = new LinkedList<String>();
        LinkedList<String> ehFileList = new LinkedList<String>();

        progress.setMinimum(0);
        progress.setMaximum(files.length);
        progress.setValue(0);


        for (int i = 0; i < files.length; i++) {
            progress.setValue(i);
            String file = files[i];
            SAXBuilder builder = new SAXBuilder();
            try {
                Element e = builder.build(file).getRootElement();
                java.util.List l = RetrievalToolkit.xpathQuery(e, "//VisualDescriptor", null);
                for (Iterator iterator = l.iterator(); iterator.hasNext();) {
                    Element desc = (Element) iterator.next();
                    if (desc.getAttributeValue("type", xsi).equals("ColorLayoutType")) {
                        ColorLayout cl = new ColorLayout(desc);
                        colorLayoutList.add(cl);
                        clFileList.add(file);
                    } else if (desc.getAttributeValue("type", xsi).equals("ScalableColorType")) {
                        ScalableColor sc = new ScalableColor(desc);
                        scalableColorList.add(sc);
                        scFileList.add(file);
                    } else if (desc.getAttributeValue("type", xsi).equals("EdgeHistogramType")) {
                        try {
                            EdgeHistogram sc = new EdgeHistogram(desc);
                            edgeHistogramList.add(sc);
                            ehFileList.add(file);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            } catch (JDOMException e1) {
                System.err.println("Error in " + file + ": " + e1.toString());
            } catch (IOException e1) {
                System.err.println("Error in " + file + ": " + e1.toString());
            }
        }
        FastmapDistanceMatrix matrixFastmap = null;
        if (type == VisualDescriptor.Type.ColorLayout) {
            matrixFastmap = new ArrayFastmapDistanceMatrix(colorLayoutList, new VisualDescriptorDistanceCalculator());
            fileList = clFileList;
        } else if (type == VisualDescriptor.Type.ScalableColor) {
            matrixFastmap = new ArrayFastmapDistanceMatrix(scalableColorList, new VisualDescriptorDistanceCalculator());
            fileList = scFileList;
        } else if (type == VisualDescriptor.Type.EdgeHistogram) {
            matrixFastmap = new ArrayFastmapDistanceMatrix(edgeHistogramList, new VisualDescriptorDistanceCalculator());
            fileList = ehFileList;
        }
        return matrixFastmap;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -