@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;
  }
  @SuppressWarnings("unchecked")
  @Override
  public Region<E> close(Region<E> reg) {
    // return erode(dilate(reg));

    try {
      CubeHistogram<E> ch = new CubeHistogram<E>((PixelCube<E, BaseIndex>) reg);

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

      er.initFunction(ch);

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

      dil.initFunction(ch);

      reg.setIterationPattern(IP_DEFAULT);
      reg.setIndexing(BASE_INDEXING);

      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();
      final VectorCube<E> ve = reg.getVectorCube();
      // System.out.println(ve);

      // final double limbodil=0;s
      // Pair<Number,Number> p= (Pair<Number, Number>) ch.getMinMax();
      // final double limboer=p.second.doubleValue();

      Access<E> accessOut = out.getAccess();

      BoundaryCondition<int[]> bc = reg.getBoundaryCondition();

      final int n = (int) accessOut.length();
      int offset = Util.cumprod(se.getDimensions()) + 1; // (int)ve.size();
      System.out.println("offset: " + offset);
      for (int i = 0; i < n + offset; i++) {
        if (i < n) {
          final E elem = zp.elementTransformCond(ve, dil, bc, i);
          accessOut.putE(i, elem);
        }
        if (i >= offset) {
          final int z = i - offset;
          // System.out.println("z: "+ z);
          final E elem = zp.elementTransformCond(ve, er, bc, z);
          accessOut.putE(z, elem);
        }
      }

      // } // end if

      return out;
    } catch (UnsupportedTypeException e) {
      e.printStackTrace();
    }
    return null;
  }