示例#1
0
  public String[] algorithem(double[][] data, double[] xPos, double[] yPos, double[] lev) {
    Contour contour = new Contour();
    Graph[] graphs = contour.contour(data, xPos, yPos, lev);

    String[] res = new String[lev.length];
    for (int i = 0; i < lev.length; i++) {
      StringBuffer sb = new StringBuffer();
      graphs[i].connectInner();
      graphs[i].connectEdgeVertex();
      sb.append(" <path d=\"");
      sb.append(graphToSVG(graphs[i]));

      sb.append(
          "\" style=\"fill:"
              + colour(lev[i] - level[0], level[level.length - 1] - level[0])
              + "\"");

      sb.append(" fill-rule=\"evenodd\"/> ");
      sb.append("\n");

      //			System.out.println(sb2.toString());
      res[i] = sb.toString();
    }
    //		System.out.println(sb.toString());
    return res;
  }
  // Trace one contour starting at (xS,yS)
  // in direction dS with label label
  // trace one contour starting at (xS,yS) in direction dS
  Contour traceContour(int xS, int yS, int label, int dS, Contour cont) {
    int xT, yT; // T = successor of starting point (xS,yS)
    int xP, yP; // P = previous contour point
    int xC, yC; // C = current contour point
    Point pt = new Point(xS, yS);
    int dNext = findNextPoint(pt, dS);
    cont.addPoint(pt);
    xP = xS;
    yP = yS;
    xC = xT = pt.x;
    yC = yT = pt.y;

    boolean done = (xS == xT && yS == yT); // true if isolated pixel

    while (!done) {
      labelArray[yC][xC] = label;
      pt = new Point(xC, yC);
      int dSearch = (dNext + 6) % 8;
      dNext = findNextPoint(pt, dSearch);
      xP = xC;
      yP = yC;
      xC = pt.x;
      yC = pt.y;
      // are we back at the starting position?
      done = (xP == xS && yP == yS && xC == xT && yC == yT);
      if (!done) {
        cont.addPoint(pt);
      }
    }
    return cont;
  }
 private Double contourError(Contour c1, Contour c2) {
   double error = 0.0;
   for (Pair<Double, Double> p : c1) {
     error += (p.second - c2.get(p.first));
   }
   error /= c1.contentSize();
   return error;
 }
 private Double contourRMSE(Contour c1, Contour c2) {
   double rmse = 0.0;
   for (Pair<Double, Double> p : c1) {
     rmse += (p.second - c2.get(p.first)) * (p.second - c2.get(p.first));
   }
   rmse /= c1.contentSize();
   rmse = Math.sqrt(rmse);
   return rmse;
 }
示例#5
0
 /**
  * Gets a contour surface for a specified image value.
  *
  * @param c the image value to contour.
  * @param withNormals true, for normal vectors; false, otherwise.
  */
 public Contour getContour(float c) {
   IntList tlist = new IntList();
   FloatList xlist = new FloatList();
   FloatList ulist = _normals ? new FloatList() : null;
   march(
       _s1.getCount(),
       _s2.getCount(),
       _s3.getCount(),
       _s1.getDelta(),
       _s2.getDelta(),
       _s3.getDelta(),
       _s1.getFirst(),
       _s2.getFirst(),
       _s3.getFirst(),
       _f,
       c,
       tlist,
       xlist,
       ulist);
   Contour contour = new Contour();
   contour.i = tlist.trim();
   contour.x = xlist.trim();
   contour.u = _normals ? ulist.trim() : null;
   if (_swap13) {
     float[] x = contour.x;
     float[] u = contour.u;
     for (int i = x.length - 3; i >= 0; i -= 3) {
       float x1 = x[i];
       float x3 = x[i + 2];
       x[i] = x3;
       x[i + 2] = x1;
       if (u != null) {
         float u1 = u[i];
         float u3 = u[i + 2];
         u[i] = u3;
         u[i + 2] = u1;
       }
     }
   }
   return contour;
 }
  void findAllContours() {
    outerContours = new ArrayList<Contour>(50);
    innerContours = new ArrayList<Contour>(50);
    int label = 0; // current label

    // scan top to bottom, left to right
    for (int v = 1; v < pixelArray.length - 1; v++) {
      label = 0; // no label
      for (int u = 1; u < pixelArray[v].length - 1; u++) {

        if (pixelArray[v][u] == FOREGROUND) {
          if (label != 0) { // keep using same label
            labelArray[v][u] = label;
          } else {
            label = labelArray[v][u];
            if (label == 0) { // unlabeled - new outer contour
              regionId = regionId + 1;
              label = regionId;
              Contour oc = traceOuterContour(u, v, label);
              outerContours.add(oc);
              labelArray[v][u] = label;
            }
          }
        } else { // BACKGROUND pixel
          if (label != 0) {
            if (labelArray[v][u] == 0) { // unlabeled - new inner contour
              Contour ic = traceInnerContour(u - 1, v, label);
              innerContours.add(ic);
            }
            label = 0;
          }
        }
      }
    }
    // shift back to original coordinates
    Contour.moveContoursBy(outerContours, -1, -1);
    Contour.moveContoursBy(innerContours, -1, -1);
  }