Example #1
0
  @SuppressWarnings("unchecked")
  @Override
  public Region<E> erode(Region<E> reg) {
    try {
      CubeHistogram<E> ch = new CubeHistogram<E>((PixelCube<E, BaseIndex>) reg);

      Erosion<E> er = new Erosion<E>((StructureElement<E>) se);

      er.initFunction(ch);

      reg.setIterationPattern(IP_DEFAULT);
      reg.setIndexing(BASE_INDEXING);
      reg.setElementFnct(er);
      setLimboErosion(reg, ch);

      Region<E> out = new Region<E>((PixelCube<E, ?>) reg.clone(), (StructureElement<E>) se);
      // out.setIterationPattern(IP_DEFAULT);
      // out.setIndexing(BASE_INDEXING);

      final ZonalProcessor zp = reg.getProcessor();
      VectorCube<E> ve = reg.getVectorCube();
      // System.out.println(ve);

      BoundaryCondition<int[]> bc = reg.getBoundaryCondition();
      // System.out.println(bc);
      if (useIter) {

        ArrayIterator<E> iter = (ArrayIterator<E>) reg.iterator();
        ArrayIterator<E> iterout = (ArrayIterator<E>) out.iterator();

        while (iter.hasNext() && iterout.hasNext()) {
          int idx = iter.index();
          // System.out.print(idx+" ");

          // final E elem=zp.elementTransform(ve, er, idx);
          final E elem = zp.elementTransformCond(ve, er, bc, idx);
          iter.inc();
          iterout.put(elem);
        }

      } else {
        Access<E> accessOut = out.getAccess();
        final int n = accessOut.size()[0];
        for (int idx = 0; idx < n; idx++) {

          // System.out.print(" i="+i+",");
          // final E elem=zp.elementTransform(ve, er, idx);
          final E elem = zp.elementTransformCond(ve, er, bc, idx);

          accessOut.putE(idx, elem);
        }
      }

      return out;
    } catch (UnsupportedTypeException e) {
      e.printStackTrace();
    }
    return null;
  }
Example #2
0
  public Region<E> rankMin(Region<E> reg) {
    CubeHistogram<E> ch;
    try {
      ch = new CubeHistogram<E>((PixelCube<E, BaseIndex>) reg);

      int ndim = reg.getNDimensions();

      final int[] center = new int[ndim];

      RankMin<E> dil = new RankMin<E>((StructureElement<E>) se);

      dil.initFunction(ch);

      reg.setIterationPattern(IP_DEFAULT);
      reg.setIndexing(BASE_INDEXING);
      reg.setElementFnct(dil);
      // setLimboDilation(reg, ch);
      setLimboErosion(reg, ch);

      Region<E> out = new Region<E>((PixelCube<E, ?>) reg.clone(), (StructureElement<E>) se);
      out.setIterationPattern(IP_DEFAULT);
      out.setIndexing(BASE_INDEXING);

      if (useIter) {
        ArrayIterator<E> iter = (ArrayIterator<E>) reg.iterator();
        ArrayIterator<E> iterout = (ArrayIterator<E>) out.iterator();

        while (iter.hasNext() && iterout.hasNext()) {
          final int idx = iter.index();
          // System.out.print(idx+" ");
          final E elem = reg.elementTransform(idx, center);
          iter.inc();
          if (elem.intValue() == 0) iterout.putInt(0);
          else iterout.inc();
        }

      } else {
        Access<E> accessOut = out.getAccess();
        final int n = accessOut.size()[0];
        for (int i = 0; i < n; i++) {
          final E elem = reg.elementTransform(i, center); // we return a masking element
          if (elem.intValue() == 0) {
            // System.out.print(elem.intValue()+"+");
            accessOut.putInt(i, 0);
          }
        }
      }

      return out;
    } catch (UnsupportedTypeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }
Example #3
0
  /**
   * @param out
   * @param ind
   * @param vcoords
   * @param ivals
   * @param accessin
   * @param mc
   */
  public void minmaxFloat(
      final Access<?> accessin,
      final GridIndex ind,
      final int[][] vcoords,
      final float[] ivals,
      final BoundaryCondition<int[]> mc,
      final Access<?> out) {

    final int[] dims = ind.getDim();
    final int sz = vcoords.length;
    ind.setIndexAndUpdate(0);
    int[] ct = dims.clone();
    final int limit = accessin.size()[0];

    for (int i = 0; i < limit; i++) {
      float sum = 0;
      final int[] coords = ind.getCoordinates();

      // iteration over kernel
      for (int k = 0; k < sz; k++) {
        // adding coordinates
        for (int u = 0; u < ct.length; u++) {
          ct[u] = (coords[u] + vcoords[k][u]);
        }
        // checking for validity
        boolean valid = true;
        for (int a = 0; a < dims.length; a++) {
          valid = valid && (ct[a] >= 0) && (ct[a] < dims[a]);
        }

        int u;
        try {
          if (valid) {
            u = ind.indexOf(ct);
          } else {
            final int[] coords_aux = mc.getCoordsAt(coords);
            u = ind.indexOf(coords_aux);
          } // end else
          final float elem = accessin.elementFloat(u);
          sum += elem * ivals[k];

        } catch (Exception e) {
          // if (debug) {
          System.out.println(e + ": " + ind + " ");
          Util.printIntArray(vcoords[k]);
          // }
        } // end catch
      } // end for

      out.putFloat(i, sum);
      ind.inc();
    } // end for
  }
Example #4
0
  /*
   *  tensor product
   */
  public <N, V> void tPoduct(N arr1, V arr2) {
    try {
      Access<?> access1 = Access.rawAccess(arr1, null);
      Access<?> access2 = Access.rawAccess(arr2, null);
      for (int i = 0; i < access1.size()[0]; i++) {
        final float f = access1.elementFloat(i) * access2.elementFloat(i);
        access1.putFloat(i, f);
      }

    } catch (UnsupportedTypeException e) {
      e.printStackTrace();
    }
  }
Example #5
0
  // TODO
  @SuppressWarnings("unchecked")
  @Override
  public N join() {

    PixelCube<E, BaseIndex> pc = new PixelCube<E, BaseIndex>(dim, dummy);

    pc.setIndexing(BASE_INDEXING);
    pc.one();

    pc.setIterationPattern(IP_SINGLE + IP_DIR + IP_FWD);
    pc.setDir(new int[] {0, 1});
    PixelDirForwardIterator<E> iter = (PixelDirForwardIterator<E>) pc.iterator();
    PixelDirForwardIterator<E> iter2 = (PixelDirForwardIterator<E>) pc.iterator();

    for (int i = 0; i < nKernels; i++) {

      System.out.println("iter: " + i);
      for (int u = 0; u < ndim; u++) {
        int[] dir = {0, u + 1}; // direction
        if (u == ndim - 1) dir = new int[] {1, 0};
        iter.setDirection(dir);
        iter2.setDirection(dir);
        int p = 0;
        Access<?> kaccess;
        try {
          kaccess = Access.rawAccess(kernels[i], null);
          final int len = kaccess.size()[0];
          System.out.println(dim[u] + ",");
          while (iter.hasNext()) {
            float elem = iter.next().floatValue();
            elem *= kaccess.elementFloat(p);
            System.out.print(elem + ",");
            iter2.putFloat(elem);
            iter2.inc();
            p++;
            p = p % len;
          }
          iter.reset();

          iter2.reset();
          System.out.println();
        } catch (UnsupportedTypeException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          return null;
        } // end catch
      } // end for
    } // end for
    // System.out.println(pc);
    return (N) pc.getAccess().getArray();
  } //
Example #6
0
  /*
   *  dot product
   */
  public <N, V> float dotPoduct(N arr1, V arr2) {
    float sum = 0;
    try {
      Access<?> access1 = Access.rawAccess(arr1, null);
      Access<?> access2 = Access.rawAccess(arr2, null);
      for (int i = 0; i < access1.size()[0]; i++) {
        final float f = access1.elementFloat(i) * access2.elementFloat(i);
        sum += f;
      }

    } catch (UnsupportedTypeException e) {
      e.printStackTrace();
    }
    return sum;
  }