@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;
  }
  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;
  }
  /**
   * @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
  }
Beispiel #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();
  } //
Beispiel #6
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();
    }
  }
Beispiel #7
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;
  }
Beispiel #8
0
  public static void main(String[] args) {
    // n_cpus=1;
    new MtreadingTest();
    System.out.println("CPUs " + n_cpus + "");
    int[] dims = {2000, 2000};

    final int size = Util.cumprod(dims);
    byte[] byte_pixels = Util.rampByte(size, 100);

    final Thread[] threads = new Thread[n_cpus];

    int csize = size / n_cpus;

    System.out.println("size " + size + " csize " + csize + "");
    final int overlap = 10;

    try {
      final Access<Byte> accesin = Access.rawAccess(byte_pixels, null);

      for (int ithread = 0; ithread < threads.length; ithread++) {
        final int offset = ithread * csize;
        int climit = offset + csize + overlap;
        if (ithread == threads.length - 1) climit = size;
        final int limit = climit;

        // System.out.println("offset "+offset+" limit "+limit);

        final int u = ithread;

        threads[ithread] =
            new Thread() {

              int _id = u;
              boolean _debug = debug;

              {
                setPriority(Thread.NORM_PRIORITY);
              }

              // task specific code goes here
              public void run() {
                for (int i = offset; i < limit; i++) {
                  int p = accesin.elementInt(i) + 1;
                  accesin.putInt(i, p);
                }

                if (_debug) System.out.println("finishing " + limit + " " + _id);
              } // end run
            }; // end thread
        // Concurrently run in as many threads as CPUs
      } // end for
      long time = -System.currentTimeMillis();
      startAndJoin(threads);
      time += System.currentTimeMillis();
      System.out.println("run time " + time + " ");

      PixelCube<Byte, BaseIndex> pci = new PixelCube<Byte, BaseIndex>(dims, byte_pixels);
      new ImageJ();
      PixLib plib = new PixLib();

      ImagePlus imp1 = null;
      try {
        imp1 = plib.imageFrom("test", pci);
      } catch (UnsupportedTypeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      imp1.show();

    } catch (UnsupportedTypeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }