Package de.mpi.rgblab.data

Source Code of de.mpi.rgblab.data.Data1

package de.mpi.rgblab.data;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import de.mpi.rgblab.color.ColorModel;
import de.mpi.rgblab.color.ColorTransformer;
import de.mpi.rgblab.color.ColorType;
import de.mpi.rgblab.math.numeric.Numeric;
import de.mpi.rgblab.utils.ImageResizer;

// the state of the class before starting implementing the optimization
public class Data1 {
  // current data is a vector of coordinates (x,y), the classlabel and
  // measures (z_score, visibility_score)
  // x, y, classlabel, z_score, visibility_score
  protected Vector<Vector<Integer>> data = new Vector<Vector<Integer>>();;
  // protected Vector<Integer> class_labels; // = new Vector<Integer>();
  protected int classNumbers;

  /*
   *
   * it stores the number of every label class up to the current point. the
   * variable is used for the integeral image algorithm it starts with
   * coordinates (0,0) then goes to (0,1), (0,2) (0,n), (1,0) (1,1) ... (1,n)
   * ... (n,n)
   *
   * the first parameter corresponds the the parameter in the variable named
   * data
   */
  protected int[][][] integralClassLabels = null;
  protected float[][][] integralSaliency = null;

  protected Vector<Double> saliency = new Vector<Double>();
  protected Vector<Double> visibility = new Vector<Double>();

  protected double min_saliency;
  protected double max_saliency;
  protected double min_visibility;
  protected double max_visibility;

  protected static ColorModel labWhite = new ColorModel(100, 0, 0,
      ColorType.LAB);

  protected String outputFile = "measures.dat";

  protected int x_res;
  protected int y_res;

  public int getX_res() {
    return x_res;
  }

  public void setX_res(int x_res) {
    this.x_res = x_res;
  }

  public int getY_res() {
    return y_res;
  }

  public void setY_res(int y_res) {
    this.y_res = y_res;
  }

  public Vector<Vector<Integer>> getData() {
    return data;
  }

  public int getClassNumbers() {
    return classNumbers;
  }

  public Data1(int x, int y) {
    x_res = x;
    y_res = y;
  }

  public Data1() {
    Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
    x_res = windowSize.width;
    y_res = windowSize.height;
  }

  public void printCoordinates() {
    System.out.println(x_res);
    System.out.println(y_res);
  }

  public boolean importData(String inputData) {
    // data = new Vector<Vector<Integer>>();

    data.removeAllElements();
    saliency.removeAllElements();
    visibility.removeAllElements();
    // class_labels = new Vector<Integer>();
    // saliency = new Vector<Double>();
    // visibility = new Vector<Double>();

    readAndSortInputData(inputData);
    if (integralClassLabels == null)
      integralClassLabels = new int[y_res + 1][x_res + 1][classNumbers+1];
    if (integralSaliency == null)
      integralSaliency = new float[y_res + 1][x_res + 1][classNumbers];
    precomputeIntegralClassLabelNumbers();

    initMinSaliency();
    initMaxSaliency();
    initMinVisibility();
    initMaxVisibility();

    // freeMemory();
    return true;

  }

  public boolean importDataTest(String inputData) {
    // data = new Vector<Vector<Integer>>();

    data.removeAllElements();
    saliency.removeAllElements();
    visibility.removeAllElements();
    // class_labels = new Vector<Integer>();
    // saliency = new Vector<Double>();
    // visibility = new Vector<Double>();

    // readAndSortInputDataTest(inputData);
    readAndSortInputData(inputData);
    if (integralClassLabels == null)
      integralClassLabels = new int[y_res + 1][x_res + 1][classNumbers];
    if (integralSaliency == null)
      integralSaliency = new float[y_res + 1][x_res + 1][classNumbers];
    precomputeIntegralClassLabelNumbers0Test();

    initMinSaliency();
    initMaxSaliency();
    initMinVisibility();
    initMaxVisibility();

    // freeMemory();
    return true;

  }

  // invokes readAndSortInputData1 which does not invert to screen coordinates
  // and directly reads integers - if faster
  public boolean importData1(String inputData) {
    data = new Vector<Vector<Integer>>();
    // class_labels = new Vector<Integer>();
    saliency = new Vector<Double>();
    visibility = new Vector<Double>();

    readAndSortInputData1(inputData);
    integralClassLabels = new int[y_res + 1][x_res + 1][classNumbers+1];
    if (integralSaliency == null)
      integralSaliency = new float[y_res + 1][x_res + 1][classNumbers];
    precomputeIntegralClassLabelNumbers();

    initMinSaliency();
    initMaxSaliency();
    initMinVisibility();
    initMaxVisibility();

    // freeMemory();
    return true;

  }

  private void initMinSaliency() {
    min_saliency = 1000000;
  }

  private void initMaxSaliency() {
    max_saliency = 0;
  }

  private void initMinVisibility() {
    min_visibility = 1000000;

  }

  private void initMaxVisibility() {
    max_visibility = 0;
  }

  // if we do not need to to tranform it to screen coordinates and to
  // eliminate the duplicates
  private void readAndSortInputData1(String inputFile) {

    Comparator comp = new YXComparator();

    // read the input data
    Vector<Vector<Double>> coordinates = new Vector<Vector<Double>>();
    Vector<Integer> class_label = new Vector<Integer>();
    Vector<Vector<Integer>> ret = new Vector<Vector<Integer>>();

    DataReader fileReader = new DataReader(inputFile);
    fileReader.get_data(coordinates);
    fileReader.get_class_label(class_label);
    classNumbers = fileReader.get_number_classes();

    // Numeric.scale_data_2_screen_res(coordinates, ret, x_res, y_res);
    // data = eliminatePoints(ret, class_label);
    Vector<Integer> t = new Vector<Integer>();
    Vector<Double> tmp;
    for (int i = 0; i < coordinates.size(); i++) {
      tmp = coordinates.get(i);
      t.add(tmp.get(0).intValue());
      t.add(tmp.get(1).intValue());
      t.add(class_label.get(i));
      data.add(t);
    }
    Collections.sort(data, comp);
  }

  private void readAndSortInputData(String inputFile) {

    Comparator comp = new XYComparator();

    // read the input data
    Vector<Vector<Double>> coordinates = new Vector<Vector<Double>>();
    Vector<Integer> class_label = new Vector<Integer>();
    Vector<Vector<Integer>> ret = new Vector<Vector<Integer>>();

    DataReader fileReader = new DataReader(inputFile);
    // read 7sec
    fileReader.get_data(coordinates);
    fileReader.get_class_label(class_label);
    classNumbers = fileReader.get_number_classes();

    // 6sec
    ImageResizer.scale_data_2_screen_res(coordinates, ret, x_res, y_res);
    // 1sec
    data = eliminatePoints(ret, class_label);

    // for (int i = 0; i < data.size(); i++) {
    // Vector<Integer> tmp = data.get(i);
    // tmp.add(class_label.get(i));
    // data.set(i, tmp);
    // }

    // 0,3sec
    Collections.sort(data, comp);

    // convert vector to array
    // Vector[] inputArray = new Vector[data.size()];
    // data.copyInto(inputArray);
    //
    // // sort
    // Arrays.sort(inputArray, comp);
    // for (int i = 0; i < data.size(); i++) {
    // data.set(i, inputArray[i]);
    // }

    /*
     * adding the class_labels to the vector of the coordinates
     *
     * Vector <Double> row = new Vector<Double>(); double classLabel;
     * Vector<Integer> class_labels = new Vector<Integer>();
     * fileReader.get_class_label(class_labels);
     *
     * for(int j=0; j< current_data.size(); j++) { row = (Vector<Double>)
     * current_data.get(j); classLabel = class_labels.get(j);
     * row.add(classLabel); current_data.set(j, row); }
     */
  }

  public void readAndSortInputDataTest(String inputFile) {

    Comparator comp = new YXComparator();

    // read the input data
    Vector<Vector<Double>> coordinates = new Vector<Vector<Double>>();
    Vector<Integer> class_label = new Vector<Integer>();
    Vector<Vector<Integer>> ret = new Vector<Vector<Integer>>();

    DataReader fileReader = new DataReader(inputFile);
    // read 7sec
    fileReader.get_data(coordinates);
    fileReader.get_class_label(class_label);
    classNumbers = fileReader.get_number_classes();

    // 6sec
    ImageResizer.scale_data_2_screen_res(coordinates, ret, x_res, y_res);
    // 1sec
    data = eliminatePoints(ret, class_label);

    // for (int i = 0; i < data.size(); i++) {
    // Vector<Integer> tmp = data.get(i);
    // tmp.add(class_label.get(i));
    // data.set(i, tmp);
    // }

    // 0,3sec
    Collections.sort(data, comp);

    // convert vector to array
    // Vector[] inputArray = new Vector[data.size()];
    // data.copyInto(inputArray);
    //
    // // sort
    // Arrays.sort(inputArray, comp);
    // for (int i = 0; i < data.size(); i++) {
    // data.set(i, inputArray[i]);
    // }

    /*
     * adding the class_labels to the vector of the coordinates
     *
     * Vector <Double> row = new Vector<Double>(); double classLabel;
     * Vector<Integer> class_labels = new Vector<Integer>();
     * fileReader.get_class_label(class_labels);
     *
     * for(int j=0; j< current_data.size(); j++) { row = (Vector<Double>)
     * current_data.get(j); classLabel = class_labels.get(j);
     * row.add(classLabel); current_data.set(j, row); }
     */
    int i = 1;

  }

  public double getMaxSaliency() {
    return max_saliency;
  }

  public double getMinSaliency() {
    return min_saliency;
  }

  public double getMaxVisibility() {
    return max_visibility;
  }

  public double getMinVisibility() {
    return min_visibility;
  }

  public Vector<Double> getVisibility() {
    return visibility;
  }

  public Vector<Double> getSaliency() {
    return saliency;
  }

  public int getNumberOfClasses() {
    return classNumbers;
  }

  private static void rgbToLabColors(Vector<ColorModel> rgbColors,
      Vector<ColorModel> labColors) {
    // copy the the vector to an array
    ColorModel[] my_rgbColor = new ColorModel[rgbColors.size()];
    rgbColors.copyInto(my_rgbColor);

    // transform to lab space
    ColorModel labColor[] = ColorTransformer.rgb2lab(my_rgbColor);

    // delete the old elements
    if (false == labColors.isEmpty())
      labColors.removeAllElements();
    // array to vector
    for (int i = 0; i < labColor.length; i++) {
      labColors.addElement(labColor[i]);
    }
  }

  public boolean computeSaliencyPrintTime(int radius,
      Vector<ColorModel> rgbColors) {
    System.out.println("start computing saliency");
    long currentTimeInSeconds = System.currentTimeMillis();

    boolean ret = computeSaliency(radius, rgbColors);

    currentTimeInSeconds = System.currentTimeMillis()
        - currentTimeInSeconds;
    System.out.println("saliency computed in: " + currentTimeInSeconds
        + " milliseconds");
    return ret;
  }

  public boolean computeSaliencySungKill(int radius, Vector<ColorModel> rgbColors) {

    // return computeSaliencyTest(radius, rgbColors);

    initMinSaliency();
    initMaxSaliency();
    saliency.clear();
    int[] sliceBoundaries = getSliceBoundaries(radius);

    Vector<ColorModel> labColors = new Vector<ColorModel>();
    rgbToLabColors(rgbColors, labColors);
    ColorModel currentPointColor;

    // int my_count1 = 0;
    // tmp variables
    // Iterator it_map_matrix1 = coordinates.iterator();
    // Iterator it_map_matrix2;
    // Iterator it_tmp = it_map_matrix

    Vector<Integer> tmp;
    int x, y, l;
    int[] sum = new int[classNumbers +1];
    int upperBound_y, right_x;
    int lowerBound_y, left_x; // [] = new int[sliceBoundaries.length];

    double z;
//    Vector<ColorModel> nbh = new Vector<ColorModel>();
//    Vector<Integer> nbh_number = new Vector<Integer>();
   
    System.out.print(data.size());
    for (int i = 0; i < data.size(); i++) {
      tmp = data.get(i);
      x = tmp.get(0);
      y = tmp.get(1);
      l = tmp.get(2);

      for (int j = 0; j < sliceBoundaries.length; j++) {
        right_x = x + j;
        left_x = x - j;

        // if (left_x < 0) {
        // left_x = 0;
        // }
        // else if (right_x > x_res) {
        // right_x = x_res;
        // }

        upperBound_y = y + sliceBoundaries[j];
        lowerBound_y = y - sliceBoundaries[j] - 1;

        // check index boundaries
        if (upperBound_y > y_res)
          upperBound_y = y_res;
        else if (lowerBound_y < 0)
          lowerBound_y = 0;

        for (int k = 0; k <= classNumbers; k++) {
          try {
            sum[k] += integralClassLabels[upperBound_y][right_x][k]
                - integralClassLabels[lowerBound_y][right_x][k];
          } catch (Exception e) {

          }
          try {
            sum[k] += integralClassLabels[upperBound_y][left_x][k]
                - integralClassLabels[lowerBound_y][left_x][k];
          } catch (Exception e) {

          }
        }
      }

      // change to the algorithm mike suggested.
      // here we are not important what colors in the surrounding are represented, but also how many points from this color are presented
      // it means we are interested in the weight of the colors in the surrounding.
//      for (int k = 0; k < classNumbers; k++) {
//        if (sum[k] > 0) {
//          nbh.add((ColorModel) labColors.elementAt(k));
//          sum[k] = 0;
//        }
//      }
      //nbh.add(labWhite);

      currentPointColor = (ColorModel) labColors.elementAt(l);

      // compute z score
      // if (computing_type == 0)
      // z = Numeric.compute_z_score(tmp, nbh);
      // // compute euclidian distance
      // else
      // if (computing_type == 1) {
      // System.out.println("current color: " + tmp);
      // System.out.println("neigbours' color: " + nbh);
      z = eucl_distance_center_surrounding(currentPointColor, labColors, sum);

      // } else {
      // System.out.println("false computing type");
      // return false;
      // }

      if (z > max_saliency)
        max_saliency = z;
      if (z < min_saliency)
        min_saliency = z;

      saliency.add(z);
      for (int k = 0; k <= classNumbers; k++) {
        sum[k] = 0;
      }
      //nbh.clear();
    }

    return true;
  }
 
  public boolean computeSaliency(int radius, Vector<ColorModel> rgbColors) {

    return computeSaliencySungKill(radius, rgbColors);
    // return computeSaliencyTest(radius, rgbColors);

//    initMinSaliency();
//    initMaxSaliency();
//    saliency.clear();
//    int[] sliceBoundaries = getSliceBoundaries(radius);
//
//    Vector<ColorModel> labColors = new Vector<ColorModel>();
//    rgbToLabColors(rgbColors, labColors);
//    ColorModel currentPointColor;
//
//    // int my_count1 = 0;
//    // tmp variables
//    // Iterator it_map_matrix1 = coordinates.iterator();
//    // Iterator it_map_matrix2;
//    // Iterator it_tmp = it_map_matrix
//
//    Vector<Integer> tmp;
//    int x, y, l;
//    int[] sum = new int[classNumbers];
//    int upperBound_y, right_x;
//    int lowerBound_y, left_x; // [] = new int[sliceBoundaries.length];
//
//    double z;
//    Vector<ColorModel> nbh = new Vector<ColorModel>();
//    System.out.print(data.size());
//    for (int i = 0; i < data.size(); i++) {
//      tmp = data.get(i);
//      x = tmp.get(0);
//      y = tmp.get(1);
//      l = tmp.get(2);
//
//      for (int j = 0; j < sliceBoundaries.length; j++) {
//        right_x = x + j;
//        left_x = x - j;
//
//        // if (left_x < 0) {
//        // left_x = 0;
//        // }
//        // else if (right_x > x_res) {
//        // right_x = x_res;
//        // }
//
//        upperBound_y = y + sliceBoundaries[j];
//        lowerBound_y = y - sliceBoundaries[j] - 1;
//
//        // check index boundaries
//        if (upperBound_y > y_res)
//          upperBound_y = y_res;
//        else if (lowerBound_y < 0)
//          lowerBound_y = 0;
//
//        for (int k = 0; k < classNumbers; k++) {
//          try {
//            sum[k] += integralClassLabels[upperBound_y][right_x][k]
//                - integralClassLabels[lowerBound_y][right_x][k];
//          } catch (Exception e) {
//
//          }
//          try {
//            sum[k] += integralClassLabels[upperBound_y][left_x][k]
//                - integralClassLabels[lowerBound_y][left_x][k];
//          } catch (Exception e) {
//
//          }
//        }
//      }
//
//      for (int k = 0; k < classNumbers; k++) {
//        if (sum[k] > 0) {
//          nbh.add((ColorModel) labColors.elementAt(k));
//          sum[k] = 0;
//        }
//      }
//      nbh.add(labWhite);
//
//      currentPointColor = (ColorModel) labColors.elementAt(l);
//
//      // compute z score
//      // if (computing_type == 0)
//      // z = Numeric.compute_z_score(tmp, nbh);
//      // // compute euclidian distance
//      // else
//      // if (computing_type == 1) {
//      // System.out.println("current color: " + tmp);
//      // System.out.println("neigbours' color: " + nbh);
//      z = Numeric.eucl_Distance(currentPointColor, nbh);
//
//      // } else {
//      // System.out.println("false computing type");
//      // return false;
//      // }
//
//      if (z > max_saliency)
//        max_saliency = z;
//      if (z < min_saliency)
//        min_saliency = z;
//
//      saliency.add(z);
//      nbh.clear();
//    }
//
//    return true;
  }

  public boolean computeSaliencyTest(int radius, Vector<ColorModel> rgbColors) {

    BufferedWriter out;
    FileWriter fw;

    initMinSaliency();
    initMaxSaliency();
    saliency.clear();
    int[] sliceBoundaries = getSliceBoundaries(radius);

    Vector<ColorModel> labColors = new Vector<ColorModel>();
    rgbToLabColors(rgbColors, labColors);
    ColorModel currentPointColor;

    // int my_count1 = 0;
    // tmp variables
    // Iterator it_map_matrix1 = coordinates.iterator();
    // Iterator it_map_matrix2;
    // Iterator it_tmp = it_map_matrix

    Vector<Integer> tmp;
    int x, y, l;
    int[] sum = new int[classNumbers];
    int upperBound_y, right_x;
    int lowerBound_y, left_x; // [] = new int[sliceBoundaries.length];

    double z;
    Vector<ColorModel> nbh = new Vector<ColorModel>();
    System.out.print(data.size());

    try {
      File file = new File("saliency_test.dat");
      fw = new FileWriter(file);
      out = new BufferedWriter(fw);
      for (int i = 0; i < data.size(); i++) {
        tmp = data.get(i);
        x = tmp.get(0);
        y = tmp.get(1);
        l = tmp.get(2);

        for (int j = 0; j < sliceBoundaries.length; j++) {
          right_x = x + j;
          left_x = x - j;

          // if (left_x < 0) {
          // left_x = 0;
          // }
          // else if (right_x > x_res) {
          // right_x = x_res;
          // }

          upperBound_y = y + sliceBoundaries[j];
          lowerBound_y = y - sliceBoundaries[j] - 1;

          // check index boundaries
          if (upperBound_y > y_res)
            upperBound_y = y_res;
          else if (lowerBound_y < 0)
            lowerBound_y = 0;

          for (int k = 0; k < classNumbers; k++) {
            try {
              sum[k] += integralClassLabels[upperBound_y][right_x][k]
                  - integralClassLabels[lowerBound_y][right_x][k];
            } catch (Exception e) {

            }
            try {
              sum[k] += integralClassLabels[upperBound_y][left_x][k]
                  - integralClassLabels[lowerBound_y][left_x][k];
            } catch (Exception e) {

            }
          }
        }

        for (int k = 0; k < classNumbers; k++) {
          if (sum[k] > 0) {
            nbh.add((ColorModel) labColors.elementAt(k));
            sum[k] = 0;
          }
        }
        nbh.add(labWhite);

        currentPointColor = (ColorModel) labColors.elementAt(l);

        // compute z score
        // if (computing_type == 0)
        // z = Numeric.compute_z_score(tmp, nbh);
        // // compute euclidian distance
        // else
        // if (computing_type == 1) {
        // System.out.println("current color: " + tmp);
        // System.out.println("neigbours' color: " + nbh);
        z = Numeric.eucl_Distance(currentPointColor, nbh);

        // } else {
        // System.out.println("false computing type");
        // return false;
        // }

        if (z > max_saliency)
          max_saliency = z;
        if (z < min_saliency)
          min_saliency = z;

        out.write(Integer.toString(x));
        out.write("|");
        out.write(Integer.toString(y));
        out.write("|");
        out.write(Integer.toString(l));
        out.write("|");
        out.write(Double.toString(z));
        out.write("|");
        out.write(Integer.toString(nbh.size()));
        out.write("|");
        for (int tmpi = 0; tmpi < nbh.size(); tmpi++) {
          out.write(Double.toString(nbh.get(tmpi).getDimension_1()));
          out.write(",");
          out.write(Double.toString(nbh.get(tmpi).getDimension_2()));
          out.write(",");
          out.write(Double.toString(nbh.get(tmpi).getDimension_3()));
          out.write("|");
        }
        out.write(System.getProperty("line.separator"));
        saliency.add(z);
        nbh.clear();
      }
      out.flush();
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return true;
  }

  public boolean computeVisiblityPrintTime(int radius) {
    System.out.println("start computing visibility");
    long currentTimeInSeconds = System.currentTimeMillis();

    boolean ret = computeVisibility(radius);

    currentTimeInSeconds = System.currentTimeMillis()
        - currentTimeInSeconds;
    System.out.println("Visibility computed in: " + currentTimeInSeconds
        + " milliseconds");
    return ret;
  }

  public boolean computeVisibility(int radius) {

    precomputeIntegralSaliency();

    initMinVisibility();
    initMaxVisibility();
    visibility.clear();
    int[] sliceBoundaries = getSliceBoundaries(radius);

    // Vector<ColorModel> labColors = new Vector<ColorModel>();
    // rgbToLabColors(rgbColors, labColors);
    // ColorModel currentPointColor;

    // int my_count1 = 0;
    // tmp variables
    // Iterator it_map_matrix1 = coordinates.iterator();
    // Iterator it_map_matrix2;
    // Iterator it_tmp = it_map_matrix

    Vector<Integer> tmp;
    int x, y, l;
    // float[] sum = new float[classNumbers];
    int upperBound_y, right_x;
    int lowerBound_y, left_x; // [] = new int[sliceBoundaries.length];

    double z = 0;
    // Vector<ColorModel> nbh = new Vector<ColorModel>();
    System.out.print(data.size());
    for (int i = 0; i < data.size(); i++) {
      tmp = data.get(i);
      x = tmp.get(0);
      y = tmp.get(1);
      l = tmp.get(2);

      for (int j = 0; j < sliceBoundaries.length; j++) {
        right_x = x + j;
        left_x = x - j;

        upperBound_y = y + sliceBoundaries[j];
        lowerBound_y = y - sliceBoundaries[j] - 1;

        // check index boundaries
        if (upperBound_y > y_res)
          upperBound_y = y_res;
        else if (lowerBound_y < 0)
          lowerBound_y = 0;

        // compute
        try {
          z += integralSaliency[upperBound_y][right_x][l]
              - integralSaliency[lowerBound_y][right_x][l];
        } catch (Exception e) {
          //right_x > res_x or right_x < 0
        }
        try {
          z += integralSaliency[upperBound_y][left_x][l]
              - integralSaliency[lowerBound_y][left_x][l];
        } catch (Exception e) {

        }

      }

      // depends on whether it must be computed in the sum of the nbh
      try {
        z -= (integralSaliency[y][x][l] - integralSaliency[y-1][x][l]);
      } catch (Exception e) {
        z -= (integralSaliency[y][x][l] - integralSaliency[y_res][x-1][l]);
      }
     
      if (z > max_visibility)
        max_visibility = z;
      if (z < min_visibility)
        min_visibility = z;

      visibility.add(z);
      z = 0;
    }

    return true;
  }

  public void precomputeIntegralSaliencyPrintTime() {
    System.out.println("start precomputeIntegralSaliencyPrintTime");
    long currentTimeInSeconds = System.currentTimeMillis();

    precomputeIntegralSaliency();

    currentTimeInSeconds = System.currentTimeMillis()
        - currentTimeInSeconds;
    System.out.println("Running Time: " + currentTimeInSeconds
        + " milliseconds");
  }

  public void precomputeIntegralSaliency() {

    int currentClassLabelValue = 0;
    float[] sum = new float[classNumbers];

    int i = 0;
    Vector<Integer> tmp = data.get(i);

    for (int x = 0; x <= x_res; x++) {
      for (int y = 0; y <= y_res; y++) {
        if (tmp.get(1) == y && tmp.get(0) == x) {

          currentClassLabelValue = (int) tmp.get(2);
          sum[currentClassLabelValue] += saliency.get(i).floatValue();

          i++;
          try {
            tmp = data.get(i);
          } catch (Exception e) {

          }
        }
        for (int k = 0; k < classNumbers; k++) {
          integralSaliency[y][x][k] = sum[k];
        }
      }
    }

  }

  // not updated - for update have a look at
  // precomputeIntegralClassLabelNumbers0
  public void precomputeIntegralSaliency0() {
    // not changed

    // if (integralSal == null)
    // integralSal = new float[y_res +1 ][x_res +1][classNumbers];

    Vector<Integer> tmp;
    int currentClassLabelValue;
    float[] last = new float[classNumbers];
    float sal;

    // like do while
    tmp = data.get(0);
    currentClassLabelValue = (int) tmp.get(2);
    last[currentClassLabelValue] = saliency.get(0).floatValue();
    integralSaliency[tmp.get(1)][tmp.get(0)][currentClassLabelValue] = last[currentClassLabelValue];

    int i = 1;
    try {
      for (i = 1; i < data.size(); i++) {
        tmp = data.get(i);
        currentClassLabelValue = (int) tmp.get(2);
        last[currentClassLabelValue] += saliency.get(0).floatValue();
        integralSaliency[tmp.get(1)][tmp.get(0)][currentClassLabelValue] = last[currentClassLabelValue];

      }
    } catch (Exception e) {
      // System.out.println(e);
      e.printStackTrace();
      System.out.println(tmp.get(1));
      System.out.println(tmp.get(0));
      System.out.println(currentClassLabelValue);
      System.out.println(last[currentClassLabelValue]);
      System.out.println(i);
    }

    for (i = 0; i <= y_res; i++) {
      for (int j = 0; j <= x_res; j++) {
        for (int k = 0; k < classNumbers; k++) {
          if (integralSaliency[i][j][k] == 0) {
            if (i > 0)
              integralSaliency[i][j][k] = integralSaliency[i - 1][j][k];
            else if (j > 0)
              integralSaliency[i][j][k] = integralSaliency[i][j - 1][k];
          }
        }
      }
    }

  }

  private void precomputeIntegralClassLabelNumbersPrintTime() {
    System.out.println("start precomputeIntegralClassLabelNumbers1");
    long currentTimeInSeconds = System.currentTimeMillis();

    precomputeIntegralClassLabelNumbers();

    currentTimeInSeconds = System.currentTimeMillis()
        - currentTimeInSeconds;
    System.out.println("time: " + currentTimeInSeconds + " milliseconds");

  }

  private void precomputeIntegralClassLabelNumbers0PrintTime() {
    System.out.println("start precomputeIntegralClassLabelNumbers1");
    long currentTimeInSeconds = System.currentTimeMillis();

    precomputeIntegralClassLabelNumbers0();

    currentTimeInSeconds = System.currentTimeMillis()
        - currentTimeInSeconds;
    System.out.println("time: " + currentTimeInSeconds + " milliseconds");

  }

  private void precomputeIntegralClassLabelNumbers() {

    int currentClassLabelValue;
    int[] sum = new int[classNumbers+1];

    int i = 0;
    Vector<Integer> tmp = data.get(i);

    int x = 0, y = 0, k;

    for (x = 0; x <= x_res; x++) {
      for (y = 0; y <= y_res; y++) {

        if (y == tmp.get(1) && x == tmp.get(0)) {

          currentClassLabelValue = (int) tmp.get(2);
          sum[currentClassLabelValue]++;
          i++;
          try {
            tmp = data.get(i);
          } catch (Exception e) {
          }
        }
        else {
          sum[classNumbers]++;
        }
        for (k = 0; k <= classNumbers; k++) {
          integralClassLabels[y][x][k] = sum[k];
        }
      }
    }

  }

  private void precomputeIntegralClassLabelNumbersTest() {

    System.out.println("start precomputeIntegralClassLabelNumbers1");
    long currentTimeInSeconds = System.currentTimeMillis();

    Vector<Integer> tmp;
    int currentClassLabelValue = 0;
    int[] sum = new int[classNumbers];

    // like do while
    // tmp = data.get(0);
    // currentClassLabelValue = (int) tmp.get(2);
    // sum[currentClassLabelValue]++;
    // integralClassLabels[tmp.get(1)][tmp.get(0)][currentClassLabelValue] =
    // 1;

    int i = 0;
    tmp = data.get(i);

    int x = 0, y = 0, k;
    // try {
    for (x = 0; x <= x_res; x++) {
      for (y = 0; y <= y_res; y++) {
        if (y == tmp.get(1) && x == tmp.get(0)) {

          currentClassLabelValue = (int) tmp.get(2);
          sum[currentClassLabelValue]++;
          i++;
          try {// will throw an exception if the end of data comes
            tmp = data.get(i);
          } catch (Exception e) {
          }

        }
        for (k = 0; k < classNumbers; k++) {
          integralClassLabels[y][x][k] = sum[k];
        }
      }
    }
    // } catch (Exception e) {
    // System.out.println(tmp.get(1));
    // System.out.println(tmp.get(0));
    // System.out.println(currentClassLabelValue);
    // System.out.println(sum[currentClassLabelValue]);
    // System.out.println(i);
    // }

    currentTimeInSeconds = System.currentTimeMillis()
        - currentTimeInSeconds;
    System.out.println("time: " + currentTimeInSeconds + " milliseconds");

  }

  public void precomputeIntegralClassLabelNumbers_Export(int xprint) {

    BufferedWriter out;
    FileWriter fw;

    System.out.println("start precomputeIntegralClassLabelNumbers1Export");
    long currentTimeInSeconds = System.currentTimeMillis();
    //
    // Vector<Integer> tmp;
    // int currentClassLabelValue;
    // int[] sum = new int[classNumbers];
    //
    // // like do while
    // tmp = data.get(0);
    // currentClassLabelValue = (int) tmp.get(2);
    // sum[currentClassLabelValue]++;
    // integralClassLabels[tmp.get(1)][tmp.get(0)][currentClassLabelValue] =
    // 1;
    //
    // int i = 1;
    // tmp = data.get(i);
    // //
    // try {
    // for (int y = 1; y <= y_res; y++) {
    // for (int x = 0; x <= x_res; x++) {
    // if (y == tmp.get(1) && x == tmp.get(0)) {
    //
    // currentClassLabelValue = (int) tmp.get(2);
    // sum[currentClassLabelValue]++;
    // i++;
    // try {
    // tmp = data.get(i);
    // } catch (Exception e) {
    // }
    // }
    // for (int k = 0; k < classNumbers; k++) {
    // integralClassLabels[y][x][k] = sum[k];
    // }
    // }
    // }
    // } catch (Exception e) {
    // System.out.println(tmp.get(1));
    // System.out.println(tmp.get(0));
    // System.out.println(currentClassLabelValue);
    // System.out.println(sum[currentClassLabelValue]);
    // System.out.println(i);
    // }

    int i;
    try {
      File file = new File(
          "precomputeIntegralClassLabelNumbers1Export.dat");
      fw = new FileWriter(file);
      out = new BufferedWriter(fw);

      for (int j = xprint; j < xprint + 1; j++) {
        for (i = 0; i < y_res; i++) {
          out.write(Integer.toString(xprint));
          out.write('|');
          out.write(Integer.toString(i));
          out.write('|');
          for (int k = 0; k <= classNumbers; k++) {

            out.write(Integer
                .toString(integralClassLabels[i][j][k]));
            out.write('|');
          }

          out.write(System.getProperty("line.separator"));
        }
      }

      out.flush();
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    currentTimeInSeconds = System.currentTimeMillis()
        - currentTimeInSeconds;
    System.out.println("time: " + currentTimeInSeconds + " milliseconds");

  }

  private void precomputeIntegralClassLabelNumbers0() {

    Vector<Integer> tmp;
    int currentClassLabelValue;
    int[] last = new int[classNumbers];

    // like do while
    tmp = data.get(0);
    currentClassLabelValue = (int) tmp.get(2);
    last[currentClassLabelValue]++;
    integralClassLabels[tmp.get(1)][tmp.get(0)][currentClassLabelValue] = 1;

    int i = 1;
    for (i = 1; i < data.size(); i++) {
      tmp = data.get(i);
      currentClassLabelValue = (int) tmp.get(2);
      last[currentClassLabelValue]++;
      integralClassLabels[tmp.get(1)][tmp.get(0)][currentClassLabelValue] = last[currentClassLabelValue];
    }

    for (int j = 0; j <= x_res; j++) {
      for (i = 0; i <= y_res; i++) {
        for (int k = 0; k < classNumbers; k++) {
          if (integralClassLabels[i][j][k] == 0) {
            if (i > 0) {
              integralClassLabels[i][j][k] = integralClassLabels[i - 1][j][k];
            } else if (j > 0) {
              integralClassLabels[i][j][k] = integralClassLabels[y_res][j - 1][k];
            }

          }
        }
      }
    }

  }

  private void precomputeIntegralClassLabelNumbers0Test() {

    System.out.println("start precomputeIntegralClassLabelNumbers");
    long currentTimeInSeconds = System.currentTimeMillis();

    Vector<Integer> tmp;
    int currentClassLabelValue;
    int[] last = new int[classNumbers];

    // like do while
    tmp = data.get(0);
    currentClassLabelValue = (int) tmp.get(2);
    last[currentClassLabelValue]++;
    integralClassLabels[tmp.get(1)][tmp.get(0)][currentClassLabelValue] = 1;

    int i = 1;
    try {

      for (i = 1; i < data.size(); i++) {
        tmp = data.get(i);
        currentClassLabelValue = (int) tmp.get(2);
        last[currentClassLabelValue]++;

        // if (tmp.get(0) == 800) {
        // System.out.print(currentClassLabelValue);
        // System.out.print("  ");
        // System.out.println(last[currentClassLabelValue]);
        // }

        integralClassLabels[tmp.get(1)][tmp.get(0)][currentClassLabelValue] = last[currentClassLabelValue];

      }
    } catch (Exception e) {
      System.out.println(tmp.get(1));
      System.out.println(tmp.get(0));
      System.out.println(currentClassLabelValue);
      System.out.println(last[currentClassLabelValue]);
      System.out.println(i);
    }

    for (int j = 0; j <= x_res; j++) {
      for (i = 0; i <= y_res; i++) {
        for (int k = 0; k < classNumbers; k++) {
          if (integralClassLabels[i][j][k] == 0) {
            if (i > 0) {
              integralClassLabels[i][j][k] = integralClassLabels[i - 1][j][k];
            } else if (j > 0) {
              integralClassLabels[i][j][k] = integralClassLabels[y_res][j - 1][k];
            }
            // System.out.println(integralClassLabels[i][j][k]);

          }
        }
      }
    }

    currentTimeInSeconds = System.currentTimeMillis()
        - currentTimeInSeconds;
    System.out.println("time: " + currentTimeInSeconds + " milliseconds");

  }

  /**
   * calculate the the positive quarter (first quandrant) for the circle. It
   * returns the boundaries for the slice used for applying the integral image
   * algorithm.
   *
   *
   * the center of the circle is the 0, 0
   *
   * example if the radius is 30
   *
   * @param radius
   * @return
   */
  public static int[] getSliceBoundaries(int radius) {
    int diameter = radius * 8;
    int[] circle = new int[radius + 1];
    int center = radius;
    // circle[0] = 0;
    // circle[0][1] = 1;

    double theta;
    int x, y;
    int last_x = radius;
    circle[0] = radius;
    int last_y = 0;

    for (int i = 0; i <= diameter; i++) {

      theta = Math.PI / 2 * i / (diameter);
      x = (int) (float) Math.round((radius * Math.cos(theta) + 0));
      y = (int) (float) Math.round((radius * Math.sin(theta) + 0));

      if (last_x != x) {

        // System.out.print(last_x);
        // System.out.print(" ");
        // System.out.println(last_y);

        circle[last_x] = last_y;
      }
      last_x = x;
      last_y = y;
    }
    // circle[diameter+1] = 0;
    // circle[diameter+1][1] = 0;
    return circle;
  }

  private static Vector<Vector<Integer>> eliminatePoints(
      Vector<Vector<Integer>> inputData, Vector<Integer> colors) {

    HashMap hm = new HashMap(15000, 0.75f);
    for (int i = 0; i < inputData.size(); i++) {
      hm.put((Vector<Integer>) inputData.get(i), colors.get(i));
    }

    Vector<Vector<Integer>> ret = new Vector<Vector<Integer>>();
    Set set = hm.entrySet();
    Iterator i = set.iterator();

    Map.Entry me;
    Vector<Integer> tmp;
    while (i.hasNext()) {
      me = (Map.Entry) i.next();
      tmp = (Vector<Integer>) me.getKey();
      tmp.add((Integer) me.getValue());
      ret.add(tmp);
    }
    return ret;

  }

  public static void freeMemory() {
    Runtime r = Runtime.getRuntime();
    r.gc();
  }

  public void export(String filename) {
    outputFile = filename;
    export();
  }
 
  public void export() {
    BufferedWriter out;
    FileWriter fw;

    int x1 = 0, y1 = 0, class_ = 0;
    double sal_score = 0, vis_score = 0;
    Vector<Double> key_row, key_row2, value_row;
    Vector<Integer> tmp;

    try {
      File file = new File(outputFile);
      fw = new FileWriter(file);
      out = new BufferedWriter(fw);

      for (int i = 0; i < data.size(); i++) {
        tmp = data.get(i);
        x1 = tmp.get(0);
        y1 = tmp.get(1);
        class_ = tmp.get(2);
        try {
          sal_score = saliency.get(i);
          vis_score = visibility.get(i);
        } catch (Exception e) {
        }

        out.write(Double.toString(x1));
        out.write('|');
        out.write(Double.toString(y1));
        out.write('|');
        out.write(Double.toString(class_));
        out.write('|');
        out.write(Double.toString(sal_score));
        out.write('|');
        out.write(Double.toString(vis_score));
        out.write(System.getProperty("line.separator"));

      }
      out.flush();
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  // tests

  public static double eucl_distance_center_surrounding(ColorModel center,
      Vector<ColorModel> colors, int[] number_of_elements) {
    Iterator it = colors.iterator();

   
    double l = 0;
    double b = 0;
    double a = 0;

    int k = 0;
    int area = 0;
   
    ColorModel my_color;

    while (it.hasNext()) {

      my_color = (ColorModel) it.next();
      if (number_of_elements[k] > 0) {
       
        area += number_of_elements[k];
       
        l += my_color.getDimension_1() * number_of_elements[k];
        a += my_color.getDimension_2() * number_of_elements[k];
        b += my_color.getDimension_3() * number_of_elements[k];
      }
      k++;
     
    }

    // white color
    l += 100 * number_of_elements[k];
    area += number_of_elements[k];
    // b += 0 * ...
    // a += 0 * ...
   
    l /= area;
    b /= area;
    a /= area;

    l -= center.getDimension_1();
    a -= center.getDimension_2();
    b -= center.getDimension_3();

    return Math.sqrt(l * l + a * a + b * b);

  }
}
 
TOP

Related Classes of de.mpi.rgblab.data.Data1

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.