public static Mat getCCH(Mat image) {
    ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(
        image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

    Mat chainHistogram = Mat.zeros(1, 8, CvType.CV_32F);
    int n = 0;
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    for (MatOfPoint contour : contours) {

      // get the freeman chain code from the contours
      int rows = contour.rows();
      // System.out.println("\nrows"+rows+"\n"+contour.dump());
      int direction = 7;
      Mat prevPoint = contours.get(0).row(0);
      n += rows - 1;
      for (int i = 1; i < rows; i++) {
        // get the current point
        double x1 = contour.get(i - 1, 0)[1];
        double y1 = contour.get(i - 1, 0)[0];

        // get the second point
        double x2 = contour.get(i, 0)[1];
        double y2 = contour.get(i, 0)[0];

        if (x2 == x1 && y2 == y1 + 1) direction = 0;
        else if (x2 == x1 - 1 && y2 == y1 + 1) direction = 1;
        else if (x2 == x1 - 1 && y2 == y1) direction = 2;
        else if (x2 == x1 - 1 && y2 == y1 - 1) direction = 3;
        else if (x2 == x1 && y2 == y1 - 1) direction = 4;
        else if (x2 == x1 + 1 && y2 == y1 - 1) direction = 5;
        else if (x2 == x1 + 1 && y2 == y1) direction = 6;
        else if (x2 == x1 + 1 && y2 == y1 + 1) direction = 7;
        else System.out.print("err");
        double counter = chainHistogram.get(0, direction)[0];
        chainHistogram.put(0, direction, ++counter);
        System.out.print(direction);
      }
    }
    System.out.println("\n" + chainHistogram.dump());
    Scalar alpha = new Scalar(n); // the factor
    Core.divide(chainHistogram, alpha, chainHistogram);
    System.out.println("\nrows=" + n + " " + chainHistogram.dump());
    return chainHistogram;
  }
  /**
   * Identifies the color in the frame
   *
   * @param in the Mat image in the region of interest
   * @return the color
   */
  public char identifyColor(Mat in) {
    // Mat blue = new Mat(in.rows(), in.cols(), CvType.CV_8UC1);
    // Mat green = new Mat(in.rows(), in.cols(), CvType.CV_8UC1);
    // Mat red = new Mat(in.rows(), in.cols(), CvType.CV_8UC1);

    // split the channels of the image
    Mat blue = new Mat(); // default is CV_8UC3
    Mat green = new Mat();
    Mat red = new Mat();
    List<Mat> channels = new ArrayList<Mat>(3);
    Core.split(in, channels);
    blue = channels.get(0); // makes all 3 CV_8UC1
    green = channels.get(1);
    red = channels.get(2);
    // System.out.println(blue.toString());

    // add the intensities
    Mat intensity = new Mat(in.rows(), in.cols(), CvType.CV_32F);
    // Mat mask = new Mat();
    Core.add(blue, green, intensity); // , mask, CvType.CV_32F);
    Core.add(intensity, red, intensity); // , mask, CvType.CV_32F);

    // not sure if correct from here to ...

    Mat inten = new Mat();
    Core.divide(intensity, Scalar.all(3.0), inten);
    // System.out.println(intensity.toString());
    // Core.divide(3.0, intensity, inten);
    // if intensity = intensity / 3.0; means element-wise division
    // use intensity.muls(Mat m)
    // so make new Mat m of same size that has each element of 1/3

    /*
    * or
    * About per-element division you can use Core.divide()

    Core.divide(A,Scalar.all(d), B);

    It's equivalent to B=A/d
    */

    // find normalized values
    Mat bnorm = new Mat();
    Mat gnorm = new Mat();
    Mat rnorm = new Mat();
    // blue.convertTo(blue, CvType.CV_32F);
    // green.convertTo(green, CvType.CV_32F);
    // red.convertTo(red, CvType.CV_32F);

    Core.divide(blue, inten, bnorm);
    Core.divide(green, inten, gnorm);
    Core.divide(red, inten, rnorm);

    // find average norm values
    Scalar val = new Scalar(0);
    val = Core.mean(bnorm);
    String value[] = val.toString().split(",");
    String s = value[0].substring(1);
    double bavg = Double.parseDouble(s);
    val = Core.mean(gnorm);
    String value1[] = val.toString().split(",");
    String s1 = value1[0].substring(1);
    double gavg = Double.parseDouble(s1);
    val = Core.mean(rnorm);
    String value2[] = val.toString().split(",");
    String s2 = value2[0].substring(1);
    double ravg = Double.parseDouble(s2);

    // ... here

    // original values
    /*
    // define the reference color values
    //double RED[] = {0.4, 0.5, 1.8};
    //double GREEN[] = {1.0, 1.2, 1.0};
    double BLUE[] = {1.75, 1.0, 0.5};
    //double YELLOW[] = {0.82, 1.7, 1.7};
    double ORANGE[] = {0.2, 1.0, 2.0};
    double WHITE[] = {2.0, 1.7, 1.7};
    //double BLACK[] = {0.0, 0.3, 0.3};
    */

    // define the reference color values
    // double RED[] = {0.4, 0.5, 1.8};
    // double GREEN[] = {1.0, 1.2, 1.0};
    double BLUE[] = {1.75, 1.0, 0.5};
    // double YELLOW[] = {0.82, 1.7, 1.7};
    double ORANGE[] = {0.2, 1.0, 2.0};
    double WHITE[] = {2.0, 1.7, 1.7};
    // double BLACK[] = {0.0, 0.3, 0.3};

    // compute the square error relative to the reference color values
    // double minError = 3.0;
    double minError = 2.0;
    double errorSqr;
    char bestFit = 'x';

    // test++;
    // System.out.print("\n\n" + test + "\n\n");

    // check BLUE fitness
    errorSqr = normSqr(BLUE[0], BLUE[1], BLUE[2], bavg, gavg, ravg);
    System.out.println("Blue: " + errorSqr);
    if (errorSqr < minError) {
      minError = errorSqr;
      bestFit = COLOR_BLUE;
    }
    // check ORANGE fitness
    errorSqr = normSqr(ORANGE[0], ORANGE[1], ORANGE[2], bavg, gavg, ravg);
    System.out.println("Orange: " + errorSqr);
    if (errorSqr < minError) {
      minError = errorSqr;
      bestFit = COLOR_ORANGE;
    }
    // check WHITE fitness
    errorSqr = normSqr(WHITE[0], WHITE[1], WHITE[2], bavg, gavg, ravg);
    System.out.println("White: " + errorSqr);
    if (errorSqr < minError) {
      minError = errorSqr;
      bestFit = COLOR_WHITE;
    }
    // check BLACK fitness
    /*errorSqr = normSqr(BLACK[0], BLACK[1], BLACK[2], bavg, gavg, ravg);
    System.out.println("Black: " + errorSqr);
    if(errorSqr < minError)
    {
    	minError = errorSqr;
    	bestFit = COLOR_BLACK;
    }*/

    // return the best fit color label
    return bestFit;
  }