//Source file: D:\\Projects\\src\\yi\\iu\\processor\\color\\reg\\merge\\KMeansHSIPMSeg.java

package yi.iu.processor.color.reg.merge;

import java.awt.*;
import yi.iu.processor.color.*;
import yi.util.data.*;
import yi.ai.cluster.*;
import yi.util.io.*;
import yi.iu.processor.resample.*;
import yi.iu.processor.color.reg.*;
import yi.util.config.*;

public class KMeansHSIPMSeg
    extends KMeansHSISeg {

  public int[] seg(int[] pixels, Dimension d) {

    int[] labels = super.seg(pixels, d);

    System.out.println("do post merge");

    // a new post merge step to label the region having the similar color
    // together (they can be disjoint)
    double[][] features = ColorUtil.RGB2HSB(pixels);
    double[][] means = yi.util.label.Label.getMeans(features, labels);
    int[] counts = yi.util.label.Label.getCounts(labels);
    double[] double_counts = Conversion.toDouble(counts);
    int[] label_map = ClosestMerger.merge(means, double_counts, MERGE_THRESHOLD, true);
    for (int i = 0; i < labels.length; i++) {
      labels[i] = label_map[labels[i]];
    }

    return labels;
  }

  /**
   * Generate the ColorLabeledRgnFts (region features with labels)
   */
  public static class GenColorLabRgnFtsPxlVal {

    public static void main(String[] args) {
      // init configuration
      Configuration.init(args);

      // get the image name
      String img_name = ReadArgu.getOption('i', args);
      if (img_name == null) {
        System.err.println("Usage: -i image_name");
        System.exit(1);
      }

      // get the scale
      String scale_str = ReadArgu.getOption('s', args);
      int scale = 1;
      if (scale_str != null) {
        scale = Integer.parseInt(scale_str);
      }

      // read in image
      Image image = yi.util.io.ImageReader.JAIReadImage(img_name);
      Dimension d = new Dimension();
      int[] pixels = yi.util.io.ImageReader.getPixels(image, d);
      Dimension old_d = (Dimension) d.clone();

      // rescale the image if necessary
      double the_scale = 1.0;
      if (scale != 1) {
        KMeansHSISeg dummy = new KMeansHSISeg();
        ResampledDescriptor resampled = Resampler.scaleDown(pixels, d,
            dummy.RESAMPLER, scale);
        if (resampled != null) {
          pixels = resampled.m_resampled;
          d = resampled.m_newDimension;
          the_scale = resampled.m_scale;
          img_name = img_name + Resampler.RESIZED_FILE_SUFFIX + scale;
        }
      }

      try {
        // process
        Configuration.setParameter("SOURCE_NAME", img_name);
        yi.util.ImageUtil.setPixels(pixels);
        yi.util.ImageUtil.setDimension(d);

        long startTime = System.currentTimeMillis();
        KMeansHSIPMSeg processor = new KMeansHSIPMSeg();
        ColorLabRgnFtsPxlVal ds = processor.getReusableColorLabRgnFtsPxlVal(
            pixels, d.width, d.height, old_d.width, old_d.height);
        long endTime = System.currentTimeMillis();
        System.out.println("running time: " + (endTime - startTime));

        int[] demo = ds.demo();
        yi.util.io.OutputResult.outputGIFImage(img_name + ".color.rgn2.gif",
                                               demo, d.width,
                                               d.height, false);

        int[] cdemo = ds.colorDemo();
        yi.util.io.OutputResult.outputGIFImage(img_name + ".color.rgn2c.gif",
                                               cdemo, d.width,
                                               d.height, false);
        System.out.println(
            "<img src=" + img_name + ">" +
            "<img src=" + img_name + ".color.rgn2.gif>" +
            "<img src=" + img_name + ".color.rgn2c.gif>" +
            "<br>"
            );

      }
      catch (Exception e) {
        System.err.println("KMeansHSISeg Terminated.");
        e.printStackTrace();
      }

      System.exit(0);
    }

  }

}