Beispiel #1
0
  public Tuple4f getDynamics() {
    Tuple4f dynamics = new Vector4f();
    long count = 0;
    double total = 0;

    float min = Float.POSITIVE_INFINITY;
    float max = Float.NEGATIVE_INFINITY;

    for (int k = 0; k < size; k++) {
      // Do not include NAN or isInfinite in dynamics
      if (!((Float.isNaN(f[k])) || (Float.isInfinite(f[k])))) {

        // System.out.print(k +"="+f[k]+ ", ");
        min = Math.min(min, f[k]);
        max = Math.max(max, f[k]);
        total += (double) f[k];
        count++;
      }
    }
    dynamics.w = min;
    dynamics.x = max;
    dynamics.y = (float) (total / (double) count);
    float range = max - min;
    int N = 256;
    System.out.println("min: " + min + " Max: " + max + " Range: " + range);
    int[] histogram = new int[N];
    int level = 0;
    float quant = N * 0.999f;
    for (int k = 0; k < size; k++) {
      if (!((Float.isNaN(f[k])) || (Float.isInfinite(f[k])))) {
        level = (int) (quant * ((f[k] - min) / range));
        try {
          histogram[level]++;
        } catch (ArrayIndexOutOfBoundsException e) {
          System.out.println("ArrayIndexOutOfBoundsException in ScalarImage.analyze(double) [A].");
        }
      }
    }
    for (int j = 0; j < N; j++) {
      System.out.println(
          "Level: "
              + j
              + " count: "
              + histogram[j]
              + " value: "
              + (float) ((j / quant * (max - min)) + min));
    }
    int target = (int) (count * 0.9);
    int cum = 0;
    for (int i = 0; i < N; i++) {
      cum += histogram[i];
      System.out.println(
          "target: "
              + target
              + " cum: "
              + cum
              + " Level: "
              + i
              + " value: "
              + ((i / quant * (max - min)) + min));
      if (cum >= target) {
        System.out.println(
            "target: " + target + " Level: " + i + " value: " + ((i * (max - min)) + min));
        dynamics.z = (float) ((i / quant * (max - min)) + min);
        break;
      }
    }
    return dynamics;
  }
Beispiel #2
0
 public void normalize(double range) {
   double scale = range;
   float min = 1000f;
   float max = -1000f;
   for (int k = 0; k < size; k++) {
     //    	System.out.println(f[k]);
     /*
      * if (f[k] == Float.NaN) { System.out.println("NaN at k= "+k);
      * f[k] = 0f; }
      */
     if (Float.isNaN(f[k])) {
       System.out.println("NaN at k= " + k);
       f[k] = 0f;
       continue;
     }
     if (Float.isInfinite(f[k])) {
       if (f[k] < 0) {
         System.out.println("-Infinity at k= " + k);
         f[k] = 0f; // -1000f;
       } else {
         System.out.println("+Infinity at k= " + k);
         f[k] = 0f; // +1000f;
       }
       continue;
     }
     // System.out.print(k +"="+f[k]+ ", ");
     min = Math.min(min, f[k]);
     max = Math.max(max, f[k]);
   }
   // System.out.println();
   double offset = 0. - min;
   if ((max - min) != 0f) {
     scale /= (max - min);
     offset *= scale;
     rescale(scale, offset);
   }
   System.out.println(
       "Normalizing using: min= "
           + min
           + " max= "
           + max
           + ", through scaleAdd("
           + scale
           + ", "
           + offset
           + ").");
   min = 1000f;
   max = -1000f;
   for (int k = 0; k < size; k++) {
     /*			if (f[k] == Float.NaN) {
     				System.out.println("NaN at k= " + k);
     				f[k] = 0f;
     			}
     */
     if (Float.isNaN(f[k])) {
       System.out.println("NaN at k= " + k + " after normalization.");
       f[k] = 0f;
       continue;
     }
     // System.out.print(k +"="+f[k]+ ", ");
     min = Math.min(min, f[k]);
     max = Math.max(max, f[k]);
   }
   System.out.println("After normalization: min= " + min + " max= " + max);
 }
Beispiel #3
0
  public void analyze(boolean doit) {

    float min = Float.POSITIVE_INFINITY;
    float max = Float.NEGATIVE_INFINITY;
    for (int k = 0; k < size; k++) {
      //    	System.out.println(f[k]);
      /*
       * if (f[k] == Float.NaN) { System.out.println("NaN at k= "+k);
       * f[k] = 0f; }
       */
      if (Float.isNaN(f[k])) {
        System.out.println("NaN at k= " + k);
        f[k] = 0f;
        continue;
      }
      if (Float.isInfinite(f[k])) {
        if (f[k] < 0) {
          System.out.println("-Infinity at k= " + k);
          f[k] = 0f; // -1000f;
        } else {
          System.out.println("+Infinity at k= " + k);
          f[k] = 0f; // +1000f;
        }
        continue;
      }
      // System.out.print(k +"="+f[k]+ ", ");
      min = Math.min(min, f[k]);
      max = Math.max(max, f[k]);
    }

    int N = 256;
    float[] histogram = new float[N];

    for (int k = 0; k < size; k++) {
      int level = (int) (0.999f * (float) N * (f[k] - min) / (max - min));
      try {
        histogram[level]++;
      } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("ArrayIndexOutOfBoundsException in ScalarImage.analyze(double) [A].");
      }
    }

    float[] cumulative = new float[N];
    if (histogram[0] > 0) {
      if (doit) {
        cumulative[0] = histogram[0];
      } else {
        cumulative[0] = 1; // histogram[0];
      }
    }
    for (int i = 1; i < N; i++) {
      if (histogram[i] > 0) {
        if (doit) {
          cumulative[i] = cumulative[i - 1] + histogram[i];
        } else {
          cumulative[i] = cumulative[i - 1] + 1; // histogram[i];
        }
      } else {
        cumulative[i] = cumulative[i - 1];
      }
    }

    /*		for(int k=0; k<size; k++) {
    			int level = (int) ( 0.999f*(float)N*(f[k]-min)/(max-min) );
    			try {
    				f[k]=(cumulative[level]-cumulative[0])/(cumulative[N-1]-cumulative[0]);
    			} catch( ArrayIndexOutOfBoundsException e) {
    				System.out.println("ArrayIndexOutOfBoundsException in ScalarImage.analyze(double) [B].");
    			}
    		}
    */
    for (int k = 0; k < size; k++) {
      float x, x1, x2, f1, f2;
      try {
        x = Math.abs((f[k] - min) / (max - min));
        x1 = (float) Math.floor((float) (N - 1) * x * 0.999f) / (float) (N - 1);
        x2 = (float) Math.ceil((float) (N - 1) * x * 0.999f) / (float) (N - 1);
        f1 =
            (cumulative[(int) Math.floor((float) (N - 1) * x * 0.999f)] - cumulative[0])
                / (cumulative[N - 1] - cumulative[0]);
        f2 =
            (cumulative[(int) Math.ceil((float) (N - 1) * x * 0.999f)] - cumulative[0])
                / (cumulative[N - 1] - cumulative[0]);
        f[k] = (f2 - f1) * (x - x1) / (x2 - x1) + f1;
      } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(
            "ArrayIndexOutOfBoundsException in ScalarImage.analyze(double) [C]. " + e.getMessage());
      }
    }

    return;

    /*
    		double offset = 0. - min;
    		if ((max - min) != 0f) {
    			scale /= (max - min);
    			offset *= scale;
    			rescale(scale, offset);
    		}
    		System.out.println("Normalizing using: min= " + min + " max= " + max
    				+ ", through scaleAdd(" + scale + ", " + offset + ").");
    		min = 1000f;
    		max = -1000f;
    		for (int k = 0; k < size; k++) {
    			if (Float.isNaN(f[k])) {
    				System.out.println("NaN at k= " + k + " after normalization.");
    				f[k] = 0f;
    				continue;
    			}
    			min = Math.min(min, f[k]);
    			max = Math.max(max, f[k]);
    		}
    		System.out.println("After normalization: min= " + min + " max= " + max);
    */
  }