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

📄 contourshape.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * 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) 2005 by Werner Klieber (werner@klieber.info)
 * http://caliph-emir.sourceforge.net
 */

package at.wklieber.mpeg7;

import at.wklieber.tools.Console;
import at.wklieber.tools.Java2dTools;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.XMLOutputter;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;


class IndexCoords {
    int i;
    double x;
    double y;

    public IndexCoords() {
        reset();
    }

    public void reset() {
        i = 0;
        x = 0;
        y = 0;
    }
}

class SortInd implements Comparator {
    public int compare(Object o1, Object o2) {
        //return (((const IndexCoords *)v1)->y <= ((const IndexCoords *)v2)->y) ? -1 : 1;

        IndexCoords v1 = (IndexCoords) o1;
        IndexCoords v2 = (IndexCoords) o2;
        return (v1.y <= v2.y ? -1 : 1);
    }

}

class
        Edge {
    int i;
    double x;
    double y;
    double dx;
    double dy;

    public Edge() {
        reset();
    }

    public void reset() {
        i = 0;
        x = 0;
        y = 0;
        dx = 0;
        dy = 0;
    }
}

class SortEdge implements Comparator {
    public int compare(Object o1, Object o2) {
        //return (((const Edge *)v1)->x <= ((const Edge *)v2)->x ? -1 : 1);

        Edge v1 = (Edge) o1;
        Edge v2 = (Edge) o2;
        return (v1.x <= v2.x ? -1 : 1);
    }

}


public class ContourShape {
    private static Console console = Console.getReference();
    private static Java2dTools java2dTools = Java2dTools.getReference();
    static final boolean debug = true;

    private static int BITS_TO_MASK(int a) {
        return ((2 << ((a) - 1)) - 1);
    }

    private static long BITS_TO_MASK(long a) {
        return ((2 << ((a) - 1)) - 1);
    }

    private static double CONTOURSHAPE_YP = 0.05;

    private static double CONTOURSHAPE_AP = 0.09;
    private static int CONTOURSHAPE_MAXCSS = 10;
    private static double CONTOURSHAPE_T = 0.000001;
    private static double CONTOURSHAPE_TXA0 = 3.8;
    private static double CONTOURSHAPE_TXA1 = 0.6;

    private static int CONTOURSHAPE_CSSPEAKBITS = 6;
    private static int CONTOURSHAPE_XBITS = 6;
    private static long CONTOURSHAPE_YBITS = 7;
    private static int CONTOURSHAPE_YnBITS = 3;
    private static int CONTOURSHAPE_CBITS = 6;
    private static int CONTOURSHAPE_EBITS = 6;

    private static double CONTOURSHAPE_ETHR = 0.6;
    private static double CONTOURSHAPE_CTHR = 1.0;
    private static double CONTOURSHAPE_ECOST = 0.4;
    private static double CONTOURSHAPE_CCOST = 0.3;

    private static double CONTOURSHAPE_NMATCHPEAKS = 2;
    private static double CONTOURSHAPE_TMATCHPEAKS = 0.9;

    private static double CONTOURSHAPE_XMAX = 1.0;
    private static double CONTOURSHAPE_YMAX = 1.7;
    private static double CONTOURSHAPE_CMIN = 12.0;
    private static double CONTOURSHAPE_CMAX = 110.0;
    private static double CONTOURSHAPE_EMIN = 1.0;
    private static double CONTOURSHAPE_EMAX = 10.0;

    private static int CONTOURSHAPE_CSSPEAKMASK = BITS_TO_MASK(CONTOURSHAPE_CSSPEAKBITS);
    private static int CONTOURSHAPE_XMASK = BITS_TO_MASK(CONTOURSHAPE_XBITS);
    private static long CONTOURSHAPE_YMASK = BITS_TO_MASK(CONTOURSHAPE_YBITS);
    private static int CONTOURSHAPE_YnMASK = BITS_TO_MASK(CONTOURSHAPE_YnBITS);
    private static int CONTOURSHAPE_CMASK = BITS_TO_MASK(CONTOURSHAPE_CBITS);
    private static long CONTOURSHAPE_EMASK = BITS_TO_MASK(CONTOURSHAPE_EBITS);


    private BufferedImage img;

    private BufferedImage regionShapeImage;

    char m_cPeaksCount; // all unsigned
    long[] m_piGlobalCurvatureVector = new long[2];
    long[] m_piPrototypeCurvatureVector = new long[2];
    long m_iHighestPeakY;
    long[] m_pPeak;


    /**
     * Create a ColorLayout Object from the given BufferedImage with the desired number of Coefficients
     *
     * @param image the input image
     */
    public ContourShape(BufferedImage image) {
        this.img = image;
        extract(img);
        init();
    }

    /**
     * Create a ColorLayout Object from the given BufferedImage with the desired number of Coefficients
     *
     * @param pointList List of Point()
     */
    public ContourShape(List pointList) {
        //this.img = image;
        init();
        extract(pointList);

    }


    /**
     * Create a ColorLayout Object from its descriptor
     *
     * @param descriptor the descriptor as JDOM Element
     */
    public ContourShape(Element descriptor) {
        this.img = null;

    }


    private void init() {
        regionShapeImage = null;
        //extract();
    }


    public Element getDescriptor() {
        Element returnVaue = null;

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

        Element vdesc = new Element("VisualDescriptor", mpeg7).setAttribute("type", "ContourShapeType", xsi);

        int num = m_cPeaksCount; //getNoOfPeaks();

        Element globalCurvatureElement = new Element("GlobalCurvature");
        vdesc.addContent(globalCurvatureElement);
        long[] lgcv; //= new long[2];
        lgcv = m_piGlobalCurvatureVector; //getGlobalCurvature();
        //vector<int> gcv(2);
        //gcv[0] = (int)lgcv[0];
        //gcv[1] = (int)lgcv[1];
        String gcv = lgcv[0] + " " + lgcv[1];
        globalCurvatureElement.setText(gcv);

        if (num > 0) {
            Element prototypeCurvatureElement = new Element("PrototypeCurvature");
            vdesc.addContent(prototypeCurvatureElement);
            long[] lpcv = new long[2];
            lpcv = m_piPrototypeCurvatureVector; //getPrototypeCurvature(lpcv[0], lpcv[1]);
            //vector<int> pcv(2);
            //pcv[0] = (int)lpcv[0];
            //pcv[1] = (int)lpcv[1];
            String pcv = lpcv[0] + " " + lpcv[1];
            prototypeCurvatureElement.setText(pcv);
        }

        Element highestPeakElement = new Element("HighestPeakY");
        vdesc.addContent(highestPeakElement);

        //getHighestPeakY()
        long peak = m_iHighestPeakY;
        highestPeakElement.setText("" + peak);

        for (int i = 1; i < num; i++) {
            //short xp, yp;

            Element peakElement = new Element("Peak");
            long[] point = getPeak(i);

            String xp = "" + point[0];
            String yp = "" + point[1];
            peakElement.setAttribute("peakX", xp);
            peakElement.setAttribute("peakY", yp);
        }


        returnVaue = vdesc;
        return vdesc;
    }

    private int getNoOfPeaks() {
        return m_cPeaksCount;
    }

    private void setNoOfPeaks(int cPeaks) {
        int cOldPeaks = m_cPeaksCount;

        // Only 5 bits used so mask out rest

        if (cPeaks > CONTOURSHAPE_CSSPEAKMASK)
            m_cPeaksCount = (char) CONTOURSHAPE_CSSPEAKMASK;
        else
            m_cPeaksCount = (char) cPeaks;

        // Manage peak memory
        if (m_cPeaksCount == cOldPeaks) {
            //if (m_pPeak != null) {
            //  m_pPeak = new short[0];
            //}

            m_pPeak = null;
            if (m_cPeaksCount > 0) {
                m_pPeak = new long[m_cPeaksCount * 2];
            }
        } else {
            //memset(m_pPeak, 0, m_cPeaksCount * sizeof(unsigned short) * 2);
            m_pPeak = new long[m_cPeaksCount * 2];
        }
    }


    private long[] getGlobalCurvature() {
        return m_piGlobalCurvatureVector;
    }


    private void setGlobalCurvature(long lC, long lE) {
        m_piGlobalCurvatureVector[0] = lC;
        m_piGlobalCurvatureVector[1] = lE;
    }


    private long getHighestPeakY() {
        return m_iHighestPeakY;
    }

    private void setHighestPeakY(long iHigh) {
        m_iHighestPeakY = iHigh;
    }

    void setPrototypeCurvature(long lC, long lE) {
        if (m_cPeaksCount > 0) {
            m_piPrototypeCurvatureVector[0] = lC;
            m_piPrototypeCurvatureVector[1] = lE;
        }
    }


    private long[] getPeak(int cIndex) {
        int cOffset = (cIndex * 2);
        long[] returnValue = new long[2];

        if (cIndex < m_cPeaksCount) {
            returnValue[0] = m_pPeak[cOffset];
            returnValue[1] = m_pPeak[cOffset + 1];
        } else {
            returnValue[0] = 0;
            returnValue[1] = 0;
        }

        return returnValue;
    }


    private void setPeak(int cIndex, long iX, long iY) {
        int cOffset = cIndex * 2;
        if (cIndex < m_cPeaksCount) {
            m_pPeak[cOffset] = iX;
            m_pPeak[cOffset + 1] = iY;
        }
    }

    /**
     * extract the shape from an image with white background and black shape
     *
     * @param image
     */
    public void extract(BufferedImage image) {
        throw new java.lang.UnsupportedOperationException("Method extract(BufferedImage) not yet implemented.");
        //long something = ExtractContour(10, new Point());
        // todo: call ExtractContor and next extract(List)
        //int nContour = ExtractContour(CONTOUR_SIZE, coords);
    }

    /**
     * extract the descrptor data
     *
     * @param pointList List of Point()
     */
    public void extract(List pointList) {
        //throw new java.lang.UnsupportedOperationException("Method extract(BufferedImage) not yet implemented.");

        /*
        Point2D.Double[] pointArray = new Point2D.Double[pointList.size()];
              pointArray =  (Point2D.Double[]) pointList.toArray(pointArray);
        extractCurvature(pointList.size(), pointArray);
        */

        extractPeaks(pointList);
    }

    public void importDescriptor(Element descriptor) {
        throw new java.lang.UnsupportedOperationException("Method importDescriptor() not yet implemented.");

        /*
      if (!aDescription)
        return (unsigned long)-1;

      GenericDS l_DDLDescription;
      GenericDSInterfaceABC *l_DDLDescriptionInterface = NULL;

      string xsitype;
      if (aDescription->GetDSName() == "Descriptor")
      {
        aDescription->GetTextAttribute("xsi:type", xsitype);
        if (xsitype == "ContourShapeType")
        {
          l_DDLDescriptionInterface = aDescription;
        }
      }

      if (!l_DDLDescriptionInterface)
      {
        l_DDLDescription = aDescription->GetDescription("Descriptor");

        while (!l_DDLDescription.isNull())
        {
          l_DDLDescription.GetTextAttribute("xsi:type", xsitype);
          if (xsitype == "ContourShapeType")
            break;
          l_DDLDescription = l_DDLDescription.GetNextSibling("Descriptor");
        }

        if (l_DDLDescription.isNull())
          return (unsigned long)-1;

        l_DDLDescriptionInterface = l_DDLDescription.GetInterface();
      }

      GenericDS GlobalCurvature_element = l_DDLDescriptionInterface->GetDescription("GlobalCurvature");
      vector<int> gcv;
      GlobalCurvature_element.GetIntVector(gcv);
      SetGlobalCurvature((unsigned long)gcv[0], (unsigned long)gcv[1]);

      GenericDS PrototypeCurvature_element = l_DDLDescriptionInterface->GetDescription("PrototypeCurvature");
      if (!PrototypeCurvature_element.isNull())
      {
        vector<int> pcv;
        PrototypeCurvature_element.GetIntVector(pcv);
        SetPrototypeCurvature((unsigned long)pcv[0], (unsigned long)pcv[1]);
      }
      else
        SetPrototypeCurvature(0, 0);

      GenericDS HighestPeak_element = l_DDLDescriptionInterface->GetDescription("HighestPeakY");
      int peak0;
      HighestPeak_element.GetIntValue(peak0);
      SetHighestPeakY(peak0);

      // Now that the 'numberOfPeaks' attribute has been removed, we need this
      // special case to check for the case where there are zero peaks.

      if (peak0 > 0)
      {
        // The SetNoOfPeaks() call must be done BEFORE reading the peak data, in
        // order to allocate the memory, so we step through the Peak elements
        // here first...

⌨️ 快捷键说明

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