Ejemplo n.º 1
0
  /**
   * For given two photos, count their similarity. The result is from interval [0;1], 1 means
   * perfect match, 0 totaly different.
   *
   * @param photo1 First photo.
   * @param photo2 Second photo.
   * @return Similarity of given photos from interval [0;1];
   * @throws com.kappa_labs.ohunter.lib.net.OHException When Photos are wrongly set.
   */
  public static synchronized float computeSimilarity(Photo photo1, Photo photo2)
      throws OHException {
    float ret = 0;

    /* Scale the images to provide the best results */
    try {
      photo1.sImage = photo1._sImage = new SImage(photo1.sImage);
      photo2.sImage = photo2._sImage = new SImage(photo2.sImage);

      ((SImage) photo1.sImage).setImage(resize(((SImage) photo1._sImage).toBufferedImage()));
      ((SImage) photo2.sImage).setImage(resize(((SImage) photo2._sImage).toBufferedImage()));
    } catch (Exception e) {
      LOGGER.log(Level.WARNING, "Could not acquire photos from client: {0}", e);
      throw new OHException("Could not acquire photos!", OHException.EXType.OTHER);
    }

    /* Count average from few attempts */
    final int numRepeats = settingsManager.getSimilarityNumberOfRepeats();
    int iteration = numRepeats;
    while (iteration-- > 0) {
      /* Perform segmentation */
      Segment[] segments1 = Segmenter.findSegments(photo1);
      Segment[] segments2 = Segmenter.findSegments(photo2);
      LOGGER.log(
          Level.FINER,
          "... got {0} segments from first and {1} segments" + " in the second photo",
          new Object[] {segments1.length, segments2.length});

      /* Create new Problem from given counted segments */
      Problem problem = new Problem();
      problem.distr1 = prepareDistribution(segments1, photo1);
      problem.distr2 = prepareDistribution(segments2, photo2);

      /* Solve the EMP linear problem and return the final result */
      EMDSolver empm = new EMDSolver(problem);
      float act = Math.max(0f, Math.min(1f, (float) empm.countValue()));
      LOGGER.finer(String.format(" - similarity: %.1f%%", 100 - act * 100));
      ret += act;
    }
    ret /= numRepeats;

    return 1f - ret;
  }