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

📄 edgehistogram.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.imageanalysis;

import org.jdom.Element;
import org.jdom.Namespace;
import org.vizir.EdgeHistogramDescriptor;
import org.vizir.MediaContent;

import java.awt.image.BufferedImage;
import java.util.StringTokenizer;

/**
 * Created by: Mathias Lux, mathias@juggle.at
 * Date: 13.09.2004
 * Time: 12:50:44
 */
public class EdgeHistogram implements VisualDescriptor {
    private EdgeHistogramDescriptor ehd = null;
    private int[] histogram = null;


    public EdgeHistogram(BufferedImage image) {
        MediaContent media = new MediaContent(image);
        ehd = new EdgeHistogramDescriptor(media);
        try {
            ehd.extractFeature();
        } catch (Exception e) {
            e.printStackTrace();
        }
        histogram = ehd.setEdgeHistogram();
    }

    public EdgeHistogram(Element descriptor) throws Exception {
        Namespace mpeg7, xsi;
        mpeg7 = Namespace.getNamespace("", "urn:mpeg:mpeg7:schema:2001");
        xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        Element bins = descriptor.getChild("BinCounts", mpeg7);
        int[] hist = new int[80];
        if (bins != null) {
            StringTokenizer st = new StringTokenizer(bins.getTextTrim());
            int count = 0;
            do {
                hist[count] = Integer.parseInt(st.nextToken());
                count++;
            } while (st.hasMoreTokens());
            if (count < 80)
                throw new Exception("Edgehistogram has less than 80 bins - that too less!");
            else
                histogram = hist;
        } else {
            throw new Exception("This is no EdgeHistogram Descriptor!");
        }
    }

    public int[] getHistogram() {
        return histogram;
    }

    public float getSimilarity(int[] edgeHistogram) {
        return EdgeHistogramDescriptor.calculateDistance(histogram, edgeHistogram);
    }

    public static float getSimilarity(int[] edgeHistogramA, int[] edgeHistogramB) {
        return EdgeHistogramDescriptor.calculateDistance(edgeHistogramA, edgeHistogramB);
    }

    private Element createXML() {
        Namespace mpeg7, xsi;
        mpeg7 = Namespace.getNamespace("", "urn:mpeg:mpeg7:schema:2001");
        xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

        org.jdom.Element descriptor = new Element("VisualDescriptor", mpeg7);
        org.jdom.Element BinCounts = new Element("BinCounts", mpeg7);
        descriptor.addContent(BinCounts);
        descriptor.setAttribute("type", "EdgeHistogramType", xsi);
        StringBuilder bc = new StringBuilder();
        bc.append(histogram[0]);
        for (int i = 1; i < histogram.length; i++) {
            bc.append(' ');
            bc.append(histogram[i]);
        }
        BinCounts.setText(bc.toString());
        return descriptor;
    }

    public Element getDescriptor() {
        return createXML();
    }

    /**
     * Compares one descriptor to another.
     *
     * @param descriptor
     * @return the distance from [0,infinite) or -1 if descriptor type does not match
     */
    public float getDistance(VisualDescriptor descriptor) {
        if (!(descriptor instanceof EdgeHistogram)) return -1f;
        EdgeHistogram e = (EdgeHistogram) descriptor;
        return e.getSimilarity(this.histogram);
    }
}

⌨️ 快捷键说明

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