Example usage for org.opencv.core Mat cols

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

Introduction

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

Prototype

public int cols() 

Source Link

Usage

From source file:com.wallerlab.compcellscope.calcDPCTask.java

License:BSD License

protected Long doInBackground(Mat... matrix_list) {
    //int count = urls.length;
    Mat in1 = matrix_list[0];/*from   w  ww .  j  a va 2  s  .  c o m*/
    Mat in2 = matrix_list[1];
    Mat outputMat = matrix_list[2];

    Mat Mat1 = new Mat(in1.width(), in1.height(), in1.type());
    Mat Mat2 = new Mat(in2.width(), in2.height(), in2.type());
    in1.copyTo(Mat1);
    in2.copyTo(Mat2);

    Imgproc.cvtColor(Mat1, Mat1, Imgproc.COLOR_RGBA2GRAY, 1);
    Imgproc.cvtColor(Mat2, Mat2, Imgproc.COLOR_RGBA2GRAY, 1);

    Mat output = new Mat(Mat1.width(), Mat1.height(), CvType.CV_8UC4);
    Mat dpcSum = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1);
    Mat dpcDifference = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1);
    Mat dpcImgF = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1);

    /*
    Log.d(TAG,String.format("Mat1 format is %.1f-%.1f, type: %d",Mat1.size().width,Mat1.size().height,Mat1.type()));
    Log.d(TAG,String.format("Mat2 format is %.1f-%.1f, type: %d",Mat2.size().width,Mat2.size().height,Mat2.type()));
    */

    // Convert to Floats
    Mat1.convertTo(Mat1, CvType.CV_32FC1);
    Mat2.convertTo(Mat2, CvType.CV_32FC1);
    Core.add(Mat1, Mat2, dpcSum);
    Core.subtract(Mat1, Mat2, dpcDifference);
    Core.divide(dpcDifference, dpcSum, dpcImgF);
    Core.add(dpcImgF, new Scalar(1.0), dpcImgF); // Normalize to 0-2.0
    Core.multiply(dpcImgF, new Scalar(110), dpcImgF); // Normalize to 0-255
    dpcImgF.convertTo(output, CvType.CV_8UC1); // Convert back into RGB
    Imgproc.cvtColor(output, output, Imgproc.COLOR_GRAY2RGBA, 4);

    dpcSum.release();
    dpcDifference.release();
    dpcImgF.release();
    Mat1.release();
    Mat2.release();

    Mat maskedImg = Mat.zeros(output.rows(), output.cols(), CvType.CV_8UC4);
    int radius = maskedImg.width() / 2 + 25;
    Core.circle(maskedImg, new Point(maskedImg.width() / 2, maskedImg.height() / 2), radius,
            new Scalar(255, 255, 255), -1, 8, 0);
    output.copyTo(outputMat, maskedImg);
    output.release();
    maskedImg.release();
    return null;
}

From source file:com.wallerlab.compcellscope.MultiModeViewActivity.java

License:BSD License

public Mat calcDPC(Mat in1, Mat in2, Mat out) {
    Mat Mat1 = new Mat(in1.width(), in1.height(), in1.type());
    Mat Mat2 = new Mat(in2.width(), in2.height(), in2.type());
    in1.copyTo(Mat1);/*w  ww.  j  a v a2s  . c  om*/
    in2.copyTo(Mat2);

    Imgproc.cvtColor(Mat1, Mat1, Imgproc.COLOR_RGBA2GRAY, 1);
    Imgproc.cvtColor(Mat2, Mat2, Imgproc.COLOR_RGBA2GRAY, 1);

    Mat output = new Mat(Mat1.width(), Mat1.height(), CvType.CV_8UC4);
    Mat dpcSum = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1);
    Mat dpcDifference = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1);
    Mat dpcImgF = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1);

    /*
    Log.d(TAG,String.format("Mat1 format is %.1f-%.1f, type: %d",Mat1.size().width,Mat1.size().height,Mat1.type()));
    Log.d(TAG,String.format("Mat2 format is %.1f-%.1f, type: %d",Mat2.size().width,Mat2.size().height,Mat2.type()));
    */

    // Convert to Floats
    Mat1.convertTo(Mat1, CvType.CV_32FC1);
    Mat2.convertTo(Mat2, CvType.CV_32FC1);
    Core.add(Mat1, Mat2, dpcSum);
    Core.subtract(Mat1, Mat2, dpcDifference);
    Core.divide(dpcDifference, dpcSum, dpcImgF);
    Core.add(dpcImgF, new Scalar(1.0), dpcImgF); // Normalize to 0-2.0
    Core.multiply(dpcImgF, new Scalar(110), dpcImgF); // Normalize to 0-255
    dpcImgF.convertTo(output, CvType.CV_8UC1); // Convert back into RGB
    Imgproc.cvtColor(output, output, Imgproc.COLOR_GRAY2RGBA, 4);

    dpcSum.release();
    dpcDifference.release();
    dpcImgF.release();
    Mat1.release();
    Mat2.release();

    Mat maskedImg = Mat.zeros(output.rows(), output.cols(), CvType.CV_8UC4);
    int radius = maskedImg.width() / 2 + 25;
    Core.circle(maskedImg, new Point(maskedImg.width() / 2, maskedImg.height() / 2), radius,
            new Scalar(255, 255, 255), -1, 8, 0);
    output.copyTo(out, maskedImg);
    output.release();
    maskedImg.release();
    return out;
}

From source file:com.wallerlab.processing.utilities.ImageUtils.java

License:BSD License

public static Bitmap toBitmap(Mat mat) {
    Bitmap bmp = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(mat, bmp);//from  w  w  w  .  j  ava2 s . c  o  m
    return bmp;
}

From source file:com.wallerlab.processing.utilities.ImageUtils.java

License:BSD License

public static Mat circularShift(Mat mat, int x, int y) {
    int w = mat.cols();
    int h = mat.rows();
    Mat result = Mat.zeros(h, w, CvType.CV_32FC4);

    int shiftR = x % w;
    int shiftD = y % h;
    //java modulus gives negative results for negative numbers
    if (shiftR < 0)
        shiftR += w;/*w w  w .j a va 2  s.  com*/
    if (shiftD < 0)
        shiftD += h;

    /* extract 4 submatrices
              |---| shiftR
     ______________
    |         |   |
    |    1    | 2 |
    |_________|___|  ___ shiftD
    |         |   |   |
    |    3    | 4 |   |
    |         |   |   |
    |_________|___|  _|_
     */
    Mat shift1 = mat.submat(0, h - shiftD, 0, w - shiftR);
    Mat shift2 = mat.submat(0, h - shiftD, w - shiftR, w);
    Mat shift3 = mat.submat(h - shiftD, h, 0, w - shiftR);
    Mat shift4 = mat.submat(h - shiftD, h, w - shiftR, w);

    /* and rearrange
     ______________
    |   |         |
    | 4 |    3    |
    |   |         |
    |___|_________|
    |   |         |
    | 2 |    1    |
    |___|_________|
     */
    shift1.copyTo(new Mat(result, new Rect(shiftR, shiftD, w - shiftR, h - shiftD)));
    shift2.copyTo(new Mat(result, new Rect(0, shiftD, shiftR, h - shiftD)));
    shift3.copyTo(new Mat(result, new Rect(shiftR, 0, w - shiftR, shiftD)));
    shift4.copyTo(new Mat(result, new Rect(0, 0, shiftR, shiftD)));

    return result;
}

From source file:contador_de_moedas.Reconhecimento.java

public Mat CriaMascara(Point pt, double raio, Mat img_Original) {
    // Cria uma img toda preta com as mesmas dimenses da original
    Mat mask = zeros(img_Original.rows(), img_Original.cols(), CV_8U);
    // Cria um circulo branco preenchido
    Imgproc.circle(mask, pt, (int) raio, new Scalar(255, 255, 255), -1, 8, 0);
    return (mask);
}

From source file:controller.GhostDetection.java

public static int[][] detect(Mat imgR, Mat imgO, JointPDF jointPDF) {

    double count_blue, count_green, count_red;
    final double threshold = 0.00001;
    int[][] ghostPixel = new int[imgO.rows()][imgO.cols()];

    // Tresholding
    for (int i = 0; i < imgR.rows(); i++) {
        for (int j = 0; j < imgR.cols(); j++) {
            double[] rgbR = imgR.get(i, j);
            double[] rgbO = imgO.get(i, j);

            count_blue = jointPDF.getPDF_blue()[(int) rgbO[0]][(int) rgbR[0]];
            count_green = jointPDF.getPDF_green()[(int) rgbO[1]][(int) rgbR[1]];
            count_red = jointPDF.getPDF_red()[(int) rgbO[2]][(int) rgbR[2]];

            if (count_blue < threshold || count_green < threshold || count_red < threshold) {
                ghostPixel[i][j] = 0;// w w  w .j a  v a  2s  .  c o m
            } else {
                ghostPixel[i][j] = 1;
            }
        }
    }
    //        for (int i = 0; i < 3; i++) {
    //            for (int j = 0; j < 5; j++) {
    //                double ghost = ghostPixel[i][j];
    //                System.out.print(ghost+";");
    //            }
    //            System.out.println("");
    //        }
    return ghostPixel;
}

From source file:cpsd.ImageGUI.java

private void cannyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cannyButtonActionPerformed
    try {// w w  w .  j  ava  2s  .co  m
        Mat source = ImageClass.getInstance().getImage();

        Mat destination = new Mat(source.rows(), source.cols(), source.type());
        threshold(source, destination, 50, 255, CV_THRESH_BINARY);
        // ImageClass.getInstance().setImage(destination);
        // Imgproc.GaussianBlur(destination,destination,new org.opencv.core.Size(0,0),10);
        // fastNlMeansDenoising(destination,destination,3,7,21);
        // source = ImageClass.getInstance().getImage();
        Imgproc.Canny(destination, destination, 0.05, 0.15, 3, true);
        ImageClass.getInstance().setImage(destination);
    } catch (NullPointerException e) {
        System.err.println("..........Please load a valid Image..........");
    }
    displayImage(); // TODO add your handling code here:
}

From source file:cpsd.ImageGUI.java

private void AnalyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnalyzeButtonActionPerformed
    try {//from   www .java  2 s. c  o  m
        double pixelArea = 1;
        double imageSize = 1;
        if (magnification == 50)
            pixelArea = 1.2996;
        else if (magnification == 100)
            pixelArea = 0.329476;
        else if (magnification == 200)
            pixelArea = 0.08162;
        else {
            imageSize = pow(10, 10) * pow(magnification, -2);
            pixelArea = (imageSize) / (resolution1 * resolution2);
        }
        Mat source = ImageClass.getInstance().getImage();
        Mat destination = new Mat(source.rows(), source.cols(), source.type());
        //Imgproc.adaptiveThreshold(source,destination,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY_INV,13,2);
        //Imgproc.GaussianBlur(destination,destination,new org.opencv.core.Size(0,0),5);
        threshold(source, destination, 30, 255, CV_THRESH_BINARY);
        distanceTransform(destination, destination, CV_DIST_L2, 3);
        normalize(destination, destination, 0, 1, NORM_MINMAX);
        threshold(destination, destination, 0.5, 1, CV_THRESH_BINARY);
        destination.convertTo(destination, CV_8U);
        /*ImageClass.getInstance().setImage(destination);
        displayImage();*/
        ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        MatOfInt4 hierarchy = new MatOfInt4();
        // Rect roi = new Rect(100,100,destination.cols()-100,destination.rows()-100);
        //  Mat imageROI = destination.submat(roi);
        /*ImageClass.getInstance().setImage(imageROI);
        displayImage();*/
        //Imgproc.Canny(source,destination,0.05,0.15);
        Imgproc.findContours(destination, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
        int total = contours.size();
        int count = 0;

        //System.out.println(total);
        MatOfPoint[] cntrs = contours.toArray(new MatOfPoint[contours.size()]);
        ArrayList<Double> value = new ArrayList<Double>();
        double temp = 0;
        for (int i = 0; i < contours.size(); i++) {
            if (contourArea(cntrs[i]) > 1/*&& contourArea(cntrs[i])<3000*/) {
                temp = 2 * Math.sqrt((contourArea(cntrs[i]) * (pixelArea)) / Math.PI);
                //temp = contourArea(cntrs[i]);
                if (temp > 0) {
                    value.add(count, temp);
                    System.out.println("area of contour " + count++ + " is : " + contourArea(cntrs[i]));
                }
            }
        }
        System.out.println("total number of contours : " + count);
        double[] values = new double[count];
        for (int i = 0; i < count; i++) {
            //temp = value.get(i);//2*Math.sqrt((value.get(i)*127.024)/Math.PI);
            //if(temp>0)
            values[i] = value.get(i);
            //System.out.println("the diameter of particle "+i+ "is "+values[i]);
            // values[i]=(contourArea(cntrs[i])*127.024)/100;
        }
        //int number = 300;
        /*HistogramDataset dataset = new HistogramDataset();
        dataset.setType(HistogramType.FREQUENCY);
        XYSeries series = new XYSeries("Particle Size Distribution");
        try{
        dataset.addSeries("Histogram1",values,number,0,300);
                
        for(int i=0;i<300;i++){
            if(dataset.getYValue(0,i)>0)
           series.add(dataset.getXValue(0,i),dataset.getYValue(0,i));
        }
        // XYDataset xydataset = new XYSeriesCollection(series);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        XYDataset xydataset = new XYSeriesCollection(series);
        String plotTitle = "Particle Size Distribution";
        String xAxis = "particle diameter in microns";
        String yAxis = "particle count";
        PlotOrientation orientation = PlotOrientation.VERTICAL;
        boolean show = true;
        boolean toolTips = true;
        boolean urls = false;
        JFreeChart chart1 = ChartFactory.createXYLineChart(plotTitle,xAxis,yAxis,xydataset,orientation,show,toolTips,urls);
        JFreeChart chart2 = ChartFactory.createHistogram(plotTitle,xAxis,yAxis,dataset,orientation,show,toolTips,urls);
                
        int width1 = 500;
        int height1 = 500;
        ChartFrame frame1 = new ChartFrame("Coal PSD",chart1);
        frame1.setSize(width1,height1);
        frame1.setVisible(true);
        frame1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        int width2 = 500;
        int height2 = 500;
        ChartFrame frame2 = new ChartFrame("Coal PSD",chart2);
        frame2.setSize(width2,height2);
        frame2.setVisible(true);
        frame2.setDefaultCloseOperation(DISPOSE_ON_CLOSE);*/
    } catch (NullPointerException e) {
        System.err.println("..........Please load a valid Image..........");
    }
    // TODO add your handling code here:
}

From source file:cpsd.ImageGUI.java

private void rgb2Gray() {
    //String str = path.getText().toString(); 
    Mat source = ImageClass.getInstance().getImage();
    Mat destination = new Mat(source.rows(), source.cols(), CV_8UC1);
    Imgproc.cvtColor(source, destination, COLOR_RGB2GRAY);
    ImageClass.getInstance().setImage(destination);
}

From source file:cpsd.ImageGUI.java

private void zoomImage(double zoomlevel) {
    Mat source = ImageClass.getInstance().getImage();
    Mat destination = new Mat((int) (source.rows() * zoomlevel), (int) (source.cols() * zoomlevel),
            source.type());//from ww  w .j  a v a 2 s  .c o m
    Imgproc.resize(source, destination, destination.size(), zoomlevel, zoomlevel, INTER_CUBIC);
    ImageClass.getInstance().setImage(destination);
    zoomSlider.setValue((int) (zoomlevel * 10));
}