Example #1
0
  /**
   * @param img
   * @return the inferred mapping
   */
  private static int[] getInferredMapping(final ImgPlus<?> img) {

    int[] inferredMapping = new int[DEFAULT_IJ1_MAPPING.size()];
    Arrays.fill(inferredMapping, -1);

    for (int d = 0; d < img.numDimensions(); d++) {
      inferredMapping[d] = DEFAULT_IJ1_MAPPING.get(img.axis(d).type());
    }

    int offset = 0;
    for (AxisType type : DEFAULT_IJ1_MAPPING.keySet()) {
      boolean contains = false;
      for (int d = 0; d < img.numDimensions(); d++) {
        if (img.axis(d).type().equals(type)) {
          contains = true;
          break;
        }
      }

      if (!contains) {
        inferredMapping[img.numDimensions() + offset] = DEFAULT_IJ1_MAPPING.get(type);
        offset++;
      }
    }

    return inferredMapping;
  }
Example #2
0
 /**
  * Check if ImgPlus contains axis which can not be mapped to IJ ImagePlus. Valid axes in ImagePlus
  * are X, Y, Channel, Z, Time.
  *
  * @param img
  * @return true if mapping is valid
  */
 public static boolean validateMapping(final ImgPlus<?> img) {
   for (int d = 0; d < img.numDimensions(); d++) {
     if (DEFAULT_IJ1_MAPPING.get(((DefaultTypedAxis) img.axis(d)).type()) == null) {
       return false;
     }
   }
   return true;
 }
  public static void main(final String[] args) {

    // TEST 2D case

    // Parameters
    final int size_x = 200;
    final int size_y = 200;

    final long a = 10;
    final long b = 5;
    final double phi_r = Math.toRadians(30);

    final long max_radius = Math.max(a, b);
    final double[] calibration = new double[] {1, 1};

    // Create blank image
    final Img<UnsignedByteType> img =
        new ArrayImgFactory<UnsignedByteType>()
            .create(new int[] {200, 200}, new UnsignedByteType());
    final ImgPlus<UnsignedByteType> imgplus = new ImgPlus<UnsignedByteType>(img);
    for (int d = 0; d < imgplus.numDimensions(); d++) {
      imgplus.setAxis(new DefaultLinearAxis(imgplus.axis(d).type(), calibration[d]), d);
    }
    final byte on = (byte) 255;

    // Create an ellipse
    long start = System.currentTimeMillis();
    System.out.println(String.format("Creating an ellipse with a = %d, b = %d", a, b));
    System.out.println(String.format("phi = %.1f", Math.toDegrees(phi_r)));
    final long[] center = new long[] {size_x / 2, size_y / 2};
    final long[] radiuses = new long[] {max_radius, max_radius};

    final EllipseNeighborhood<UnsignedByteType> disc =
        new EllipseNeighborhood<UnsignedByteType>(img, center, radiuses);
    final EllipseCursor<UnsignedByteType> sc = disc.cursor();

    double r2, phi, term;
    double cosphi, sinphi;
    while (sc.hasNext()) {
      sc.fwd();
      r2 = sc.getDistanceSquared();
      phi = sc.getPhi();
      cosphi = Math.cos(phi - phi_r);
      sinphi = Math.sin(phi - phi_r);
      term = r2 * cosphi * cosphi / a / a + r2 * sinphi * sinphi / b / b;
      if (term <= 1) sc.get().set(on);
    }
    final long end = System.currentTimeMillis();
    System.out.println("Ellipse creation done in " + (end - start) + " ms.");
    System.out.println();

    ij.ImageJ.main(args);
    ImageJFunctions.show(imgplus);

    start = System.currentTimeMillis();
    final Spot spot = new Spot(center[0], center[1], 0d, max_radius, -1d);

    final SpotMorphologyAnalyzer<UnsignedByteType> bm =
        new SpotMorphologyAnalyzer<UnsignedByteType>(imgplus, null);
    bm.process(spot);

    System.out.println("Blob morphology analyzed in " + (end - start) + " ms.");
    double phiv, thetav, lv;
    for (int j = 0; j < 2; j++) {
      lv = spot.getFeature(featurelist_sa[j]);
      phiv = spot.getFeature(featurelist_phi[j]);
      thetav = spot.getFeature(featurelist_theta[j]);
      System.out.println(
          String.format(
              "For axis of semi-length %.1f, orientation is phi = %.1f°, theta = %.1f°",
              lv, Math.toDegrees(phiv), Math.toDegrees(thetav)));
    }
    System.out.println(spot.echo());

    // TEST 3D case
    /*
     *
     * // Parameters int size_x = 200; int size_y = 200; int size_z = 200;
     *
     * double a = 5.5f; double b = 4.9f; double c = 5; double theta_r =
     * (double) Math.toRadians(0); // I am unable to have it working for
     * theta_r != 0 double phi_r = (double) Math.toRadians(45);
     *
     * double max_radius = Math.max(a, Math.max(b, c)); double[] calibration
     * = new double[] {1, 1, 1};
     *
     * // Create blank image Image<UnsignedByteType> img = new
     * ImageFactory<UnsignedByteType>( new UnsignedByteType(), new
     * ArrayContainerFactory() ).createImage(new int[] {200, 200, 200});
     * final byte on = (byte) 255;
     *
     * // Create an ellipse long start = System.currentTimeMillis();
     * System.out.println(String.format(
     * "Creating an ellipse with a = %.1f, b = %.1f, c = %.1f", a, b, c));
     * System.out.println(String.format("phi = %.1f and theta = %.1f",
     * Math.toDegrees(phi_r), Math.toDegrees(theta_r))); double[] center =
     * new double[] { size_x/2, size_y/2, size_z/2 };
     * SphereCursor<UnsignedByteType> sc = new
     * SphereCursor<UnsignedByteType>(img, center, max_radius, calibration);
     * double r2, theta, phi, term; double cosphi, sinphi, costheta,
     * sintheta; while (sc.hasNext()) { sc.fwd(); r2 =
     * sc.getDistanceSquared(); phi = sc.getPhi(); theta = sc.getTheta();
     * cosphi = Math.cos(phi-phi_r); sinphi = Math.sin(phi-phi_r); costheta
     * = Math.cos(theta-theta_r); sintheta = Math.sin(theta-theta_r); term =
     * r2*cosphi*cosphi*sintheta*sintheta/a/a +
     * r2*sinphi*sinphi*sintheta*sintheta/b/b + r2*costheta*costheta/c/c; if
     * (term <= 1) sc.getType().set(on); } sc.close(); long end =
     * System.currentTimeMillis();
     * System.out.println("Ellipse creation done in " + (end-start) +
     * " ms."); System.out.println();
     *
     * ij.ImageJ.main(args); img.getDisplay().setMinMax();
     * ImageJFunctions.copyToImagePlus(img).show();
     *
     * start = System.currentTimeMillis(); BlobMorphology<UnsignedByteType>
     * bm = new BlobMorphology<UnsignedByteType>(img, calibration); SpotImp
     * spot = new SpotImp(center); spot.putFeature(Feature.RADIUS,
     * max_radius); bm.process(spot); end = System.currentTimeMillis();
     * System.out.println("Blob morphology analyzed in " + (end-start) +
     * " ms."); double phiv, thetav, lv; for (int j = 0; j < 3; j++) { lv =
     * spot.getFeature(featurelist_sa[j]); phiv =
     * spot.getFeature(featurelist_phi[j]); thetav =
     * spot.getFeature(featurelist_theta[j]);
     * System.out.println(String.format(
     * "For axis of semi-length %.1f, orientation is phi = %.1f°, theta = %.1f°"
     * , lv, Math.toDegrees(phiv), Math.toDegrees(thetav))); }
     * System.out.println(spot.echo());
     */
  }