コード例 #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;
  }
コード例 #2
0
  @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;
  }
コード例 #3
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;
  }
コード例 #4
0
ファイル: SeparableKernel.java プロジェクト: dprodanov/pixlib
  /*
   *  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();
    }
  }
コード例 #5
0
ファイル: SeparableKernel.java プロジェクト: dprodanov/pixlib
  // 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();
  } //
コード例 #6
0
ファイル: SeparableKernel.java プロジェクト: dprodanov/pixlib
  /*
   *  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;
  }
コード例 #7
0
ファイル: MtreadingTest.java プロジェクト: dprodanov/pixlib
  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();
    }
  }
コード例 #8
0
ファイル: LabelingTest.java プロジェクト: dprodanov/pixlib
  /** @param args */
  public static void main(String[] args) {
    final int[] dims = {50, 30};
    final int n = 1500;
    final short[] short_pixels = Util.rampShort(n, 0);

    // final int[] int_pixels=Util.rampInt(n,0);
    final float[] float_pixels = Util.rampFloat(n, 0);

    final short[] labels = {0, 1, 2};
    final int[] borders = {0, 10, 200, 1000};

    final float[] flabels = {0, 1, 2};
    final float[] fborders = {0.0f, 10, 200, 1000};

    PixelCube<Short, BaseIndex> pci = new PixelCube<Short, BaseIndex>(dims, short_pixels);
    pci.setIterationPattern(IP_FWD + IP_SINGLE);
    pci.setIndexing(BASE_INDEXING);

    PixelCube<Short, BaseIndex> outc = (PixelCube<Short, BaseIndex>) pci.clone();

    RasterForwardIterator<Short> in = (RasterForwardIterator<Short>) pci.iterator();
    RasterForwardIterator<Short> out = (RasterForwardIterator<Short>) outc.iterator();

    Labeling<Short> labeling = new Labeling<Short>(labels, borders);
    labeling.setIO(in, out);

    labeling.run();
    new ImageJ();
    PixLib plib = new PixLib();

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

    ImagePlus imp2 = null;
    try {
      imp2 = plib.imageFrom("thresholded", outc);
    } catch (UnsupportedTypeException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    imp2.show();

    PixelCube<Float, BaseIndex> pcif = new PixelCube<Float, BaseIndex>(dims, float_pixels);
    pcif.setIterationPattern(IP_FWD + IP_SINGLE);
    pcif.setIndexing(BASE_INDEXING);

    PixelCube<Float, BaseIndex> outcf = (PixelCube<Float, BaseIndex>) pcif.clone();

    RasterForwardIterator<Float> inf = (RasterForwardIterator<Float>) pcif.iterator();
    RasterForwardIterator<Float> outf = (RasterForwardIterator<Float>) outcf.iterator();

    Labeling<Float> labelingf = new Labeling<Float>(flabels, fborders);
    labelingf.setIO(inf, outf);

    Float elem = Float.valueOf(3.0f);
    System.out.println(labelingf.mapFloat(elem).getClass());

    labelingf.run();

    // PixLib plibf=new PixLib();
    ImagePlus imp3 = null;
    try {
      imp3 = plib.imageFrom("test", pcif);
    } catch (UnsupportedTypeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    imp3.show();

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