Example usage for org.opencv.core Mat convertTo

List of usage examples for org.opencv.core Mat convertTo

Introduction

In this page you can find the example usage for org.opencv.core Mat convertTo.

Prototype

public void convertTo(Mat m, int rtype, double alpha) 

Source Link

Usage

From source file:classes.TextRecognitionPreparer.java

public static Scalar cluster(Scalar userColor, Mat cutout, int k) {

    Mat samples = cutout.reshape(1, cutout.cols() * cutout.rows());
    Mat samples32f = new Mat();
    samples.convertTo(samples32f, CvType.CV_32F, 1.0 / 255.0);

    Mat labels = new Mat();
    TermCriteria criteria = new TermCriteria(TermCriteria.COUNT, 100, 1);
    Mat centers = new Mat();
    Core.kmeans(samples32f, k, labels, criteria, 1, Core.KMEANS_PP_CENTERS, centers);

    Scalar fillingColor = getFillingColor(userColor, cutout, labels, centers);

    return fillingColor;
}

From source file:classes.TextRecognitionPreparer.java

private static Scalar getFillingColor(Scalar userColor, Mat cutout, Mat labels, Mat centers) {

    double minDistance = 1000000;
    Scalar fillingColor = null;//  w ww  . java  2s.com

    centers.convertTo(centers, CvType.CV_8UC1, 255.0);
    centers.reshape(3);

    List<Mat> clusters = new ArrayList<Mat>();
    for (int i = 0; i < centers.rows(); i++) {
        clusters.add(Mat.zeros(cutout.size(), cutout.type()));
    }

    Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
    for (int i = 0; i < centers.rows(); i++) {
        counts.put(i, 0);
    }

    int rows = 0;
    for (int y = 0; y < cutout.rows(); y++) {
        for (int x = 0; x < cutout.cols(); x++) {
            int label = (int) labels.get(rows, 0)[0];
            int r = (int) centers.get(label, 2)[0];
            int g = (int) centers.get(label, 1)[0];
            int b = (int) centers.get(label, 0)[0];
            counts.put(label, counts.get(label) + 1);
            clusters.get(label).put(y, x, b, g, r);
            rows++;
        }
    }

    Set<Integer> keySet = counts.keySet();
    Iterator<Integer> iterator = keySet.iterator();
    while (iterator.hasNext()) {

        int label = (int) iterator.next();
        int r = (int) centers.get(label, 2)[0];
        int g = (int) centers.get(label, 1)[0];
        int b = (int) centers.get(label, 0)[0];

        Scalar currentColor = new Scalar(r, g, b);

        double distance = getColorDistance(currentColor, userColor);

        if (distance < minDistance) {
            minDistance = distance;
            fillingColor = currentColor;
        }

    }

    return fillingColor;
}

From source file:com.astrocytes.core.operationsengine.OperationsImpl.java

License:Open Source License

private Mat applyKmeans(Mat source) {
    Mat dest = new Mat();

    source.convertTo(source, CvType.CV_32F, 1.0 / 255.0);

    Mat centers = new Mat();
    Mat labels = new Mat();
    TermCriteria criteria = new TermCriteria(TermCriteria.COUNT, 20, 0.1);
    Core.kmeans(source, 4, labels, criteria, 10, Core.KMEANS_PP_CENTERS, centers);

    List<Mat> mats = showClusters(source, labels, centers);
    //mats.get(0).convertTo(dest, CvType.CV_8UC3);
    Core.merge(mats, dest);/*from  w w  w . j  a  va2s.co  m*/
    //centers.convertTo(dest, CvType.CV_8UC3);
    return dest;
}

From source file:com.astrocytes.core.operationsengine.OperationsImpl.java

License:Open Source License

private List<Mat> showClusters(Mat cutout, Mat labels, Mat centers) {
    centers.convertTo(centers, CvType.CV_8UC1, 255.0);
    centers.reshape(3);//from w w  w . j av a2  s .  c om

    List<Mat> clusters = new ArrayList<Mat>();
    for (int i = 0; i < centers.rows(); i++) {
        clusters.add(Mat.zeros(cutout.size(), cutout.type()));
    }

    Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
    for (int i = 0; i < centers.rows(); i++) {
        counts.put(i, 0);
    }

    for (int y = 0; y < cutout.rows(); y++) {
        int rows = 0;
        for (int x = 0; x < cutout.cols(); x++) {
            int label = (int) labels.get(rows, 0)[0];
            int r = (int) centers.get(label, 2)[0];
            int g = (int) centers.get(label, 1)[0];
            int b = (int) centers.get(label, 0)[0];
            counts.put(label, counts.get(label) + 1);
            clusters.get(label).put(y, x, b, g, r);
            rows++;
        }
    }
    System.out.println(counts);
    return clusters;
}

From source file:cubesolversimulator.VisualInputForm.java

private void findAvg(List<Rect> roi) {
    cols = new int[9];
    for (int i = 0; i < roi.size(); i++) {
        Mat ri = new Mat(blured, roi.get(i));
        ri.convertTo(ri, -1, 1.0);
        Highgui.imwrite("extract" + i + ".jpg", ri);
        Scalar clr = new Scalar(0, 0, 0);
        clr = Core.mean(ri);/* w  ww . j ava2  s  .  c  om*/
        System.out.println("old col: " + clr);
        if (clr.val[0] > 30)
            clr.val[0] = clr.val[0] - 30;
        else
            clr.val[0] = 0;
        //if(clr.val[2]<200)
        //  clr.val[2]=clr.val[2]+20;
        //else
        //  clr.val[0]=0;
        System.out.println("new col: " + clr);
        getDistance(clr, i);
    }
    displayLabel();
}

From source file:nz.ac.auckland.lablet.vision.CamShiftTracker.java

License:Open Source License

/**
 * Finds the dominant colour in an image, and returns two values in HSV colour space to represent similar colours,
 * e.g. so you can keep all colours similar to the dominant colour.
 *
 * How the algorithm works://from  w  ww .j  av a  2s .  c o m
 *
 * 1. Scale the frame down so that algorithm doesn't take too long.
 * 2. Segment the frame into different colours (number of colours determined by k)
 * 3. Find dominant cluster (largest area) and get its central colour point.
 * 4. Get range (min max) to represent similar colours.
 *
 * @param bgr The input frame, in BGR colour space.
 * @param k The number of segments to use (2 works well).
 * @return The min and max HSV colour values, which represent the colours similar to the dominant colour.
 */
private Pair<Scalar, Scalar> getMinMaxHsv(Mat bgr, int k) {
    //Convert to HSV
    Mat input = new Mat();
    Imgproc.cvtColor(bgr, input, Imgproc.COLOR_BGR2BGRA, 3);

    //Scale image
    Size bgrSize = bgr.size();
    Size newSize = new Size();

    if (bgrSize.width > CamShiftTracker.KMEANS_IMG_SIZE || bgrSize.height > CamShiftTracker.KMEANS_IMG_SIZE) {

        if (bgrSize.width > bgrSize.height) {
            newSize.width = CamShiftTracker.KMEANS_IMG_SIZE;
            newSize.height = CamShiftTracker.KMEANS_IMG_SIZE / bgrSize.width * bgrSize.height;
        } else {
            newSize.width = CamShiftTracker.KMEANS_IMG_SIZE / bgrSize.height * bgrSize.width;
            newSize.height = CamShiftTracker.KMEANS_IMG_SIZE;
        }

        Imgproc.resize(input, input, newSize);
    }

    //Image quantization using k-means, see here for details of k-means algorithm: http://bit.ly/1JIvrlB
    Mat clusterData = new Mat();

    Mat reshaped = input.reshape(1, input.rows() * input.cols());
    reshaped.convertTo(clusterData, CvType.CV_32F, 1.0 / 255.0);
    Mat labels = new Mat();
    Mat centres = new Mat();
    TermCriteria criteria = new TermCriteria(TermCriteria.COUNT, 50, 1);
    Core.kmeans(clusterData, k, labels, criteria, 1, Core.KMEANS_PP_CENTERS, centres);

    //Get num hits for each category
    int[] counts = new int[k];

    for (int i = 0; i < labels.rows(); i++) {
        int label = (int) labels.get(i, 0)[0];
        counts[label] += 1;
    }

    //Get cluster index with maximum number of members
    int maxCluster = 0;
    int index = -1;

    for (int i = 0; i < counts.length; i++) {
        int value = counts[i];

        if (value > maxCluster) {
            maxCluster = value;
            index = i;
        }
    }

    //Get cluster centre point hsv
    int r = (int) (centres.get(index, 2)[0] * 255.0);
    int g = (int) (centres.get(index, 1)[0] * 255.0);
    int b = (int) (centres.get(index, 0)[0] * 255.0);
    int sum = (r + g + b) / 3;

    //Get colour range
    Scalar min;
    Scalar max;

    int rg = Math.abs(r - g);
    int gb = Math.abs(g - b);
    int rb = Math.abs(r - b);
    int maxDiff = Math.max(Math.max(rg, gb), rb);

    if (maxDiff < 35 && sum > 120) { //white
        min = new Scalar(0, 0, 0);
        max = new Scalar(180, 40, 255);
    } else if (sum < 50 && maxDiff < 35) { //black
        min = new Scalar(0, 0, 0);
        max = new Scalar(180, 255, 40);
    } else {
        Mat bgrColour = new Mat(1, 1, CvType.CV_8UC3, new Scalar(r, g, b));
        Mat hsvColour = new Mat();

        Imgproc.cvtColor(bgrColour, hsvColour, Imgproc.COLOR_BGR2HSV, 3);
        double[] hsv = hsvColour.get(0, 0);

        int addition = 0;
        int minHue = (int) hsv[0] - colourRange;
        if (minHue < 0) {
            addition = Math.abs(minHue);
        }

        int maxHue = (int) hsv[0] + colourRange;

        min = new Scalar(Math.max(minHue, 0), 60, Math.max(35, hsv[2] - 30));
        max = new Scalar(Math.min(maxHue + addition, 180), 255, 255);
    }

    return new Pair<>(min, max);
}