Example usage for org.opencv.core TermCriteria MAX_ITER

List of usage examples for org.opencv.core TermCriteria MAX_ITER

Introduction

In this page you can find the example usage for org.opencv.core TermCriteria MAX_ITER.

Prototype

int MAX_ITER

To view the source code for org.opencv.core TermCriteria MAX_ITER.

Click Source Link

Document

The maximum number of iterations or elements to compute

Usage

From source file:imageprocess.ObjectFinder.java

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat image = Highgui.imread("D:\\backup\\opencv\\baboon1.jpg");
    // Define ROI
    Rect rect = new Rect(110, 260, 35, 40);
    Mat imageROI = new Mat(image, rect);
    Core.rectangle(image, new Point(110, 260), new Point(145, 300), new Scalar(0, 0, 255));

    Imshow origIm = new Imshow("Origin");
    origIm.showImage(image);/*from   w  ww. j  a v a  2s .  co  m*/

    ObjectFinder finder = new ObjectFinder(false, 0.2f);

    // Get the Hue histogram
    int minSat = 65;
    Mat hist = finder.getHueHistogram(imageROI, minSat);
    Mat norm = new Mat();
    Core.normalize(hist, norm, 1, 0, NORM_L2);

    finder.setROIHistogram(norm);

    // Convert to HSV space
    Mat hsv = new Mat();
    Imgproc.cvtColor(image, hsv, CV_BGR2HSV);
    // Split the image
    List<Mat> v = new ArrayList<>();
    Core.split(hsv, v);

    // Eliminate pixels with low saturation
    Imgproc.threshold(v.get(1), v.get(1), minSat, 255, THRESH_BINARY);
    Imshow satIm = new Imshow("Saturation");
    satIm.showImage(v.get(1));
    // Get back-projection of hue histogram
    Mat result = finder.find(hsv, new MatOfInt(0), new MatOfFloat(0.0f, 180.0f));

    Imshow resultHueIm = new Imshow("Result Hue");
    resultHueIm.showImage(result);

    Core.bitwise_and(result, v.get(1), result);
    Imshow resultHueAndIm = new Imshow("Result Hue and raw");
    resultHueAndIm.showImage(result);

    // Second image
    Mat image2 = Highgui.imread("D:\\backup\\opencv\\baboon3.jpg");

    // Display image
    Imshow img2Im = new Imshow("Imgage2");
    img2Im.showImage(image2);

    // Convert to HSV space
    Imgproc.cvtColor(image2, hsv, CV_BGR2HSV);

    // Split the image
    Core.split(hsv, v);

    // Eliminate pixels with low saturation
    Imgproc.threshold(v.get(1), v.get(1), minSat, 255, THRESH_BINARY);
    Imshow satIm2 = new Imshow("Saturation2");
    satIm2.showImage(v.get(1));

    // Get back-projection of hue histogram
    finder.setThreshold(-1.0f);
    result = finder.find(hsv, new MatOfInt(0), new MatOfFloat(0.0f, 180.0f));

    Imshow resultHueIm2 = new Imshow("Result Hue2");
    resultHueIm2.showImage(result);

    Core.bitwise_and(result, v.get(1), result);
    Imshow resultHueAndIm2 = new Imshow("Result Hue and raw2");
    resultHueAndIm2.showImage(result);

    Rect rect2 = new Rect(110, 260, 35, 40);
    Core.rectangle(image2, new Point(110, 260), new Point(145, 300), new Scalar(0, 0, 255));

    TermCriteria criteria = new TermCriteria(TermCriteria.MAX_ITER | TermCriteria.EPS, 100, 0.01);
    int steps = Video.meanShift(result, rect2, criteria);

    Core.rectangle(image2, new Point(rect2.x, rect2.y),
            new Point(rect2.x + rect2.width, rect2.y + rect2.height), new Scalar(0, 255, 0));

    Imshow meanshiftIm = new Imshow("Meanshift result");
    meanshiftIm.showImage(image2);

}

From source file:opencv.CaptchaDetection.java

private static Mat k_means_spilter(Mat src) {
    Mat dst = Mat.zeros(src.size(), CvType.CV_8UC1);

    int width = src.cols();
    int height = src.rows();
    int dims = src.channels();

    //   //ww  w.  jav  a 2s  .  c  o m
    int clusterCount = 3;

    Mat points = new Mat(width * height, dims, CvType.CV_32F, new Scalar(0));
    Mat centers = new Mat(clusterCount, dims, CvType.CV_32F);
    Mat labels = new Mat(width * height, 1, CvType.CV_32S);

    //    points
    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            int index = row * width + col;
            double[] s_data = src.get(row, col);

            for (int channel = 0; channel < 3; channel++) {
                float[] f_buff = new float[1];
                f_buff[0] = (float) s_data[channel];

                points.put(index, channel, f_buff);
            }
        }
    }

    //  knn ?
    TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER, 10, 0.1);
    Core.kmeans(points, clusterCount, labels, criteria, 3, Core.KMEANS_PP_CENTERS, centers);

    //  ??? label index
    Map<Integer, Integer> tmp = new TreeMap<>();
    for (int i = 0; i < clusterCount; i++) {
        int sum = 0;
        for (int j = 0; j < dims; j++) {
            sum += centers.get(i, j)[0];
        }
        while (tmp.containsKey(sum))
            sum++;
        tmp.put(sum, i);
    }

    int count = 0;
    int[] label_order = new int[clusterCount];
    for (Map.Entry<Integer, Integer> iter : tmp.entrySet()) {
        label_order[count++] = iter.getValue();
    }

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            int index = row * width + col;
            int label = (int) labels.get(index, 0)[0];

            if (label == label_order[1]) {
                byte[] d_buff = new byte[1];
                d_buff[0] = (byte) 255;
                dst.put(row, col, d_buff);
            }
        }
    }

    return dst;
}

From source file:se.hb.jcp.bindings.opencv.ClassifierBase.java

License:Open Source License

protected TermCriteria readTerminationCriteria() {
    if (_jsonParameters.has("termination_criteria")) {
        JSONObject termination = _jsonParameters.getJSONObject("termination_criteria");
        int criteria = 0;
        int max_iter = 0;
        double epsilon = 0.0;
        if (termination.has("max_count")) {
            criteria += TermCriteria.MAX_ITER;
            max_iter = termination.getInt("max_iter");
        }//from w  w w  .ja va2s.c om
        if (termination.has("epsilon")) {
            criteria += TermCriteria.EPS;
            epsilon = termination.getDouble("epsilon");
        }
        return new TermCriteria(criteria, max_iter, epsilon);
    } else {
        return null;
    }
}