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:Clases.Segmentador.java

public int detectaAreaPterigion(Mat img_ana) {
    int area = 0;
    for (int y = 0; y < img_ana.rows(); y++) {
        for (int x = 0; x < img_ana.cols(); x++) {
            double[] color = img_ana.get(y, x);
            if (color[2] == 255 && color[1] == 255 && color[0] == 0) {
                area++;/*ww w. j av  a2  s.  c o  m*/
            }
        }
    }
    return area;
}

From source file:Clases.Segmentador.java

public double[] TransfHough(Mat edges, int inv, int distMinCir, int umbMin, int umbMax, int radMin,
        int radMax) {

    Mat circles = new Mat();
    double[] data = null;
    //Vector<Mat> circlesList = new Vector<Mat>();
    Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, inv, distMinCir, umbMin, umbMax, radMin,
            radMax);/* w  ww  . j  a  v  a  2s . c  o  m*/
    //Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 200, 1, 10, 20, 40  );
    //Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 50) ;

    System.out.println("#rows " + circles.rows() + " #cols " + circles.cols());
    double x = 0.0;
    double y = 0.0;
    int r = 0;

    for (int i = 0; i < circles.rows(); i++) {
        data = circles.get(i, 0);
        for (int j = 0; j < data.length; j++) {
            x = data[0];
            y = data[1];
            r = (int) data[2];
        }
        Point center = new Point(x, y);

        // circle center
        //            Core.circle(color, center, 1, new Scalar(0, 0, 0), -1);
        // circle outline
        //          Core.circle(color, center, r, new Scalar(0, 255, 0), 1);
        //Ventana ventana8 = new Ventana(convertir((color)),0,2);
        //ventana8.setTitle("Houg");
        /*
         Rect bbox = new Rect((int)Math.abs(x-r), (int)Math.abs(y-r), (int)2*r, (int)2*r);
         Mat croped_image = new Mat(color, bbox);
         Imgproc.resize(croped_image, croped_image, new Size(200,200));
         circlesList.add(croped_image);
         Ventana ventana9 = new Ventana(convertir(croped_image),1,2);
         */
    }

    return data;
}

From source file:Clases.Segmentador.java

public BufferedImage Mat2BufferedImage(Mat m) {
    //source: http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/
    //Fastest code
    //The output can be assigned either to a BufferedImage or to an Image
    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1) {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }//w  w  w .  j  a  va  2 s .  c  o m
    int bufferSize = m.channels() * m.cols() * m.rows();
    byte[] b = new byte[bufferSize];
    m.get(0, 0, b); // get all the pixels
    BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    System.arraycopy(b, 0, targetPixels, 0, b.length);
    return image;
}

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;/*from   w w  w  . j  a  v a 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:classes.TextRecognitionPreparer.java

static Mat replaceColor(Mat image, Scalar color1, Scalar color2) {

    Mat replaced = image.clone();/*from   w  w w .j  av a  2  s .  co m*/

    for (int y = 0; y < image.rows(); y++) {
        for (int x = 0; x < image.cols(); x++) {
            double[] values = image.get(y, x);
            double r = values[0];
            double g = values[1];
            double b = values[2];

            if (b == color1.val[0] && g == color1.val[1] && r == color1.val[2]) {
                values[0] = color2.val[2];
                values[1] = color2.val[1];
                values[2] = color2.val[0];

            }
            replaced.put(y, x, values);

        }
    }

    return replaced;

}

From source file:classes.TextRecognitionPreparer.java

private static Mat reduceColor(Mat image, int div) {

    Mat result = new Mat(image.size(), image.type());

    int rows = image.rows(); // number of lines
    int cols = image.cols(); // number of elements per line

    for (int j = 0; j < rows; j++) {

        for (int i = 0; i < cols; i++) {

            double[] data = image.get(j, i);

            for (int k = 0; k < 3; k++) {
                data[k] = ((int) data[k] / div) * div + div / 2;

            }/*from   www  . jav a 2  s .c  o m*/

            int put = result.put(j, i, data);

        }
    }

    return result;
}

From source file:cmib_4_4.Countour.java

public static void main(String args[]) {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat image = Highgui.imread("input1.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    Mat image1 = Highgui.imread("input1.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    Mat image4 = Highgui.imread("input1.jpg");
    Imgproc.threshold(image1, image1, 0, 255, THRESH_OTSU);
    Imgproc.Canny(image1, image1, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU,
            Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
    Mat image2 = Mat.zeros(image.rows() + 2, image.cols() + 2, CV_8U);
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(image1, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    for (int i = 0; i < contours.size(); i++) {

        if (Imgproc.contourArea(contours.get(i)) > 100) {

            Rect rect = Imgproc.boundingRect(contours.get(i));
            Imgproc.floodFill(image1, image2, new Point(150, 150), new Scalar(255));
            Rect rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
            Mat image_roi_rgb = new Mat(image4, rectCrop);
            Highgui.imwrite("crop2.jpg", image_roi_rgb);
            if (rect.height > 28) {

                Core.rectangle(image, new Point(rect.x, rect.y),
                        new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255));
            }/*from   www.j a v  a  2  s  . c  o  m*/
        }
    }
    Highgui.imwrite("falciparum2.jpg", image);

}

From source file:cmib_4_4.Feature.java

public int[] featureVector(Mat image1) {

    Mat image = new Mat();
    Size sz = new Size(30, 30);
    Imgproc.resize(image1, image, sz);/*from  w  w  w .  java2 s .  com*/
    int size = (int) (image1.total() * image1.channels());
    int size2 = (image.width() * image.height());
    double[][] spec1 = new double[size2][3];

    FeatureVector A = new FeatureVector();
    int k = 0;
    for (int i = 0; i < image.rows(); i++) {

        for (int j = 0; j < image.cols(); j++) {

            //image.get(i, j, rgb);
            double[] rgb = image.get(i, j);
            double[] a = A.cartToSpec(rgb[0], rgb[1], rgb[2]);
            double x = Math.toRadians(90);
            spec1[k][0] = a[0] / x;
            spec1[k][1] = a[1] / x;
            spec1[k][2] = a[2] / x;
            //System.out.println(rgb[0]);
            //System.out.println(spec1[k][2]);
            k++;
        }
    }

    int[][] b = new int[11][11];
    for (int i = 0; i < 11; i++) {

        for (int j = 0; j < 11; j++) {

            b[i][j] = 0;
        }
    }

    for (int i = 0; i < 900; i++) {

        int x1 = (int) (Math.round(spec1[i][1] * 10));
        int y1 = (int) (Math.round(spec1[i][2] * 10));

        b[x1][y1] = b[x1][y1] + 1;
        //System.out.println(x1+"and"+y1);
    }
    int l = 0;
    int[] c = new int[121];
    for (int i = 0; i < 11; i++) {

        for (int j = 0; j < 11; j++) {

            c[l] = b[i][j];
            l++;
            //System.out.println(c[l-1]);
        }
    }
    return c;

}

From source file:cmib_4_4.FeatureVector.java

public Mat removeNoisePixcels(Mat noiseGreyImage, Mat rbgImage) {

    Mat image = noiseGreyImage;
    Mat image1 = rbgImage;/*from  w  w  w. j  a va 2s  .c  o  m*/

    int size = (int) (image.total() * image.channels());
    byte[] get = new byte[size];
    byte[] temp1 = new byte[size];
    int size1 = (int) (image1.total() * image1.channels());
    byte[] rgb1 = new byte[size1];

    for (int i = 0; i < image.rows(); i++) {

        for (int j = 0; j < image.cols(); j++) {

            image.get(i, j, get);
            if (get[0] == -1) {

                image.put(i, j, 1);
            }
            image.get(i, j, get);
            //System.out.println(get[0]);

            if (get[0] == 1) {

                if (i == 0 & j == 0) {

                    byte[] a1 = new byte[1];
                    byte[] a2 = new byte[1];
                    byte[] a3 = new byte[1];
                    image.get(i, j + 1, a1);
                    image.get(i + 1, j + 1, a2);
                    image.get(i + 1, j, a3);

                    if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) {

                        //image1.put(j, i, 0.0);
                        //System.out.println("1");
                    } else {

                        image.put(i, j, 0);
                    }
                }

                else if (i == 0 & j == image.cols()) {

                    byte[] a1 = new byte[1];
                    byte[] a2 = new byte[1];
                    byte[] a3 = new byte[1];
                    image.get(i, j - 1, a1);
                    image.get(i + 1, j - 1, a2);
                    image.get(i + 1, j, a3);

                    if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) {

                        //image1.put(j, i, 1.0);
                        // System.out.println("2");
                    } else {

                        image.put(i, j, 0);
                    }
                } else if (i == image.rows() & j == image.cols()) {

                    byte[] a1 = new byte[1];
                    byte[] a2 = new byte[1];
                    byte[] a3 = new byte[1];
                    image.get(i - 1, j, a1);
                    image.get(i - 1, j - 1, a2);
                    image.get(i, j - 1, a3);

                    if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) {

                        // image1.put(j, i, 1.0);
                        // System.out.println("3");
                    } else {

                        image.put(i, j, 0);
                    }
                } else if (j == 0 & i == image.rows()) {

                    byte[] a1 = new byte[1];
                    byte[] a2 = new byte[1];
                    byte[] a3 = new byte[1];
                    image.get(i, j + 1, a1);
                    image.get(i - 1, j + 1, a2);
                    image.get(i - 1, j, a3);

                    if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) {

                        //image1.put(j, i, 1.0);
                        // System.out.println("4");
                    } else {

                        image.put(i, j, 0);
                    }
                }

                else if (j == 0) {

                    double[] a1 = image.get(i - 1, j);
                    double[] a2 = image.get(i - 1, j + 1);
                    double[] a3 = image.get(i, j + 1);
                    double[] a4 = image.get(i + 1, j + 1);
                    double[] a5 = image.get(i + 1, j);

                    if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1)
                            | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) {

                        //image1.put(j, i, 1.0);
                        // System.out.println("5");
                    } else {

                        image.put(i, j, 0);
                    }
                } else if (i == 0) {

                    byte[] a1 = new byte[1];
                    byte[] a2 = new byte[1];
                    byte[] a3 = new byte[1];
                    byte[] a4 = new byte[1];
                    byte[] a5 = new byte[1];

                    image.get(i, j - 1, a1);
                    image.get(i + 1, j - 1, a2);
                    image.get(i + 1, j, a3);
                    image.get(i + 1, j + 1, a4);
                    image.get(i, j + 1, a5);

                    if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1)
                            | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) {

                        //image1.put(j, i, 1.0);
                        // System.out.println("6");
                    } else {

                        image.put(i, j, 0);
                    }
                } else if (j == image.cols()) {

                    byte[] a1 = new byte[1];
                    byte[] a2 = new byte[1];
                    byte[] a3 = new byte[1];
                    byte[] a4 = new byte[1];
                    byte[] a5 = new byte[1];

                    image.get(i - 1, j, a1);
                    image.get(i - 1, j - 1, a2);
                    image.get(i, j - 1, a3);
                    image.get(i + 1, j - 1, a4);
                    image.get(i + 1, j, a5);

                    if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1)
                            | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) {

                        //image1.put(j, i, 1.0);
                        // System.out.println("7");
                    } else {

                        image.put(i, j, 0);
                    }
                } else if (i == image.rows()) {

                    byte[] a1 = new byte[1];
                    byte[] a2 = new byte[1];
                    byte[] a3 = new byte[1];
                    byte[] a4 = new byte[1];
                    byte[] a5 = new byte[1];

                    image.get(i, j + 1, a1);
                    image.get(i - 1, j + 1, a2);
                    image.get(i - 1, j, a3);
                    image.get(i - 1, j - 1, a4);
                    image.get(i, j - 1, a5);

                    if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1)
                            | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) {

                        //image1.put(j, i, 1.0);
                        // System.out.println("8");
                    } else {

                        image.put(i, j, 0);
                    }
                }

                else {

                    byte[] a1 = new byte[1];
                    byte[] a2 = new byte[1];
                    byte[] a3 = new byte[1];
                    byte[] a4 = new byte[1];
                    byte[] a5 = new byte[1];
                    byte[] a6 = new byte[1];
                    byte[] a7 = new byte[1];
                    byte[] a8 = new byte[1];

                    image.get(i - 1, j, a1);
                    image.get(i - 1, j - 1, a2);
                    image.get(i, j - 1, a3);
                    image.get(i + 1, j - 1, a4);
                    image.get(i + 1, j, a5);
                    image.get(i + 1, j + 1, a6);
                    image.get(i, j + 1, a7);
                    image.get(i - 1, j + 1, a8);

                    if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1)
                            | (a3[0] == 1 & a4[0] == -1 & a5[0] == -1)
                            | (a4[0] == -1 & a5[0] == -1 & a6[0] == -1)
                            | (a5[0] == -1 & a6[0] == 1 & a7[0] == -1)
                            | (a6[0] == -1 & a7[0] == -1 & a8[0] == -1)) {

                        //image1.put(j, i, 1.0);
                        // System.out.println("9");
                    } else {

                        image.put(i, j, 0);
                    }
                }

            }

        }
    }
    ////////////////////// Obtain RGB final blood cell image /////////////////////////////
    for (int i = 0; i < image.rows(); i++) {

        for (int j = 0; j < image.cols(); j++) {

            image.get(i, j, temp1);
            image1.get(i, j, rgb1);

            if (temp1[0] == -1) {

                image.put(i, j, 1);
            }

            image.get(i, j, temp1);
            //System.out.println(temp1[0]);
            byte r = (byte) (rgb1[0] * temp1[0]);
            byte g = (byte) (rgb1[1] * temp1[0]);
            byte b = (byte) (rgb1[2] * temp1[0]);

            image1.put(i, j, new byte[] { r, g, b });

        }

    }
    /////////////////////////////////////////////////////////////////////////////////////
    return image1;
}