Example usage for org.opencv.core Mat reshape

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

Introduction

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

Prototype

public Mat reshape(int cn) 

Source Link

Usage

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  w  w .  j  a va 2 s .  c  o  m

    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 List<Mat> showClusters(Mat cutout, Mat labels, Mat centers) {
    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()));
    }/*from  w w  w.j  a v a2  s  . c  o  m*/

    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:com.wallerlab.processing.tasks.ComputeRefocusTask.java

License:BSD License

private Bitmap[] computeFocus(float z) {
    int width = mDataset.WIDTH - 2 * mDataset.XCROP;
    int height = mDataset.HEIGHT - 2 * mDataset.YCROP;

    Mat result = new Mat(height, width, CvType.CV_32FC4);
    Mat result8 = new Mat(height, width, CvType.CV_8UC4);

    Mat dpc_result_tb = new Mat(height, width, CvType.CV_32FC4);
    Mat dpc_result_tb8 = new Mat(height, width, CvType.CV_8UC4);

    Mat dpc_result_lr = new Mat(height, width, CvType.CV_32FC4);
    Mat dpc_result_lr8 = new Mat(height, width, CvType.CV_8UC4);

    Mat img;// www  .j  av a  2 s . c o  m
    Mat img32 = new Mat(height, width, CvType.CV_32FC4);
    Mat shifted;

    for (int idx = 0; idx < mDataset.fileCount; idx++) {
        img = ImageUtils.toMat(BitmapFactory.decodeByteArray(fileByteList[idx], 0, fileByteList[idx].length));
        img = img.submat(mDataset.YCROP, mDataset.HEIGHT - mDataset.YCROP, mDataset.XCROP,
                mDataset.WIDTH - mDataset.XCROP);
        img.convertTo(img32, result.type());

        // Grab actual hole number from filename
        String fName = mDataset.fileList[idx].toString();
        String hNum = fName.substring(fName.indexOf("_scanning_") + 10, fName.indexOf(".jpeg"));
        int holeNum = Integer.parseInt(hNum);
        //Log.d(TAG,String.format("BF Scan Header is: %s", hNum));

        // Calculate these based on array coordinates
        int xShift = (int) Math.round(z * tanh_lit[holeNum]);
        int yShift = (int) Math.round(z * tanv_lit[holeNum]);

        shifted = ImageUtils.circularShift(img32, yShift, xShift);

        if (mDataset.leftList.contains(holeNum)) //add LHS
        {
            Core.add(dpc_result_lr, shifted, dpc_result_lr);
        } else //subtract RHS
        {
            Core.subtract(dpc_result_lr, shifted, dpc_result_lr);
        }

        if (mDataset.topList.contains(holeNum)) //add Top
        {
            Core.add(dpc_result_tb, shifted, dpc_result_tb);
        } else //subtract bottom
        {
            Core.subtract(dpc_result_tb, shifted, dpc_result_tb);
        }

        Core.add(result, shifted, result);

        float progress = ((idx + 1) / (float) mDataset.fileCount);
        onProgressUpdate((int) (progress * 100), -1);
        Log.d(TAG, String.format("progress: %f", progress));
    }

    Core.MinMaxLocResult minMaxLocResult1 = Core.minMaxLoc(result.reshape(1));
    result.convertTo(result8, CvType.CV_8UC4, 255 / minMaxLocResult1.maxVal);

    Core.MinMaxLocResult minMaxLocResult2 = Core.minMaxLoc(dpc_result_lr.reshape(1));
    dpc_result_lr.convertTo(dpc_result_lr8, CvType.CV_8UC4,
            255 / (minMaxLocResult2.maxVal - minMaxLocResult2.minVal),
            -minMaxLocResult2.minVal * 255.0 / (minMaxLocResult2.maxVal - minMaxLocResult2.minVal));

    Core.MinMaxLocResult minMaxLocResult3 = Core.minMaxLoc(dpc_result_tb.reshape(1));
    dpc_result_tb.convertTo(dpc_result_tb8, CvType.CV_8UC4,
            255 / (minMaxLocResult3.maxVal - minMaxLocResult3.minVal),
            -minMaxLocResult3.minVal * 255.0 / (minMaxLocResult3.maxVal - minMaxLocResult3.minVal));

    /*
    Log.d(TAG,String.format("result_min: %f, max: %f",minMaxLocResult1.minVal,minMaxLocResult1.maxVal));
    Log.d(TAG,String.format("lr_min: %f, max: %f",minMaxLocResult2.minVal,minMaxLocResult2.maxVal));
    Log.d(TAG,String.format("tb_min: %f, max: %f",minMaxLocResult3.minVal,minMaxLocResult3.maxVal));
    */

    // remove transparency in DPC images
    Scalar alphaMask = new Scalar(new double[] { 1.0, 1.0, 1.0, 255.0 });

    Core.multiply(dpc_result_lr8, alphaMask, dpc_result_lr8);
    Core.multiply(dpc_result_tb8, alphaMask, dpc_result_tb8);

    if (!mDataset.USE_COLOR_DPC) {
        Imgproc.cvtColor(dpc_result_lr8, dpc_result_lr8, Imgproc.COLOR_BGR2GRAY);
        Imgproc.cvtColor(dpc_result_tb8, dpc_result_tb8, Imgproc.COLOR_BGR2GRAY);
    }

    /*
    // Cut off edges in DPC images
    Point centerPt = new Point();
    centerPt.x = Math.round((float)width/2.0);
    centerPt.y = Math.round((float)height/2.0);
    Mat circleMat = new Mat(dpc_result_lr8.size(), dpc_result_lr8.type());
    Scalar color = new Scalar(255);
    Core.circle(circleMat, centerPt, 200, color);
    //Core.bitwise_and(circleMat, dpc_result_lr8, dpc_result_lr8);
    //Core.bitwise_and(circleMat, dpc_result_tb8, dpc_result_tb8);
    * 
    * 
    */

    Bitmap[] outputBitmaps = new Bitmap[3];
    outputBitmaps[0] = ImageUtils.toBitmap(result8);
    outputBitmaps[1] = ImageUtils.toBitmap(dpc_result_lr8);
    outputBitmaps[2] = ImageUtils.toBitmap(dpc_result_tb8);

    return outputBitmaps;
}