Example usage for org.opencv.core Mat put

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

Introduction

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

Prototype

public int put(int row, int col, byte[] data) 

Source Link

Usage

From source file:ch.zhaw.facerecognitionlibrary.Helpers.MatOperation.java

License:Open Source License

public static Rect[] rotateFaces(Mat img, Rect[] faces, int angle) {
    Point center = new Point(img.cols() / 2, img.rows() / 2);
    Mat rotMat = Imgproc.getRotationMatrix2D(center, angle, 1);
    rotMat.convertTo(rotMat, CvType.CV_32FC1);
    float scale = img.cols() / img.rows();
    for (Rect face : faces) {
        Mat m = new Mat(3, 1, CvType.CV_32FC1);
        m.put(0, 0, face.x);
        m.put(1, 0, face.y);/* www.  j  av a  2s .c om*/
        m.put(2, 0, 1);
        Mat res = Mat.zeros(2, 1, CvType.CV_32FC1);
        Core.gemm(rotMat, m, 1, new Mat(), 0, res, 0);
        face.x = (int) res.get(0, 0)[0];
        face.y = (int) res.get(1, 0)[0];
        if (angle == 270 || angle == -90) {
            face.x = (int) (face.x * scale - face.width);
            face.x = face.x + face.width / 4;
            face.y = face.y + face.height / 4;
        } else if (angle == 180 || angle == -180) {
            face.x = face.x - face.width;
            face.y = face.y - face.height;
        } else if (angle == 90 || angle == -270) {
            face.y = (int) (face.y * scale - face.height);
            face.x = face.x - face.width / 4;
            face.y = face.y - face.height / 4;
        }
    }
    return faces;
}

From source file:ch.zhaw.facerecognitionlibrary.Helpers.MatXml.java

License:Open Source License

public Mat readMat(String tag) {
    if (isWrite) {
        System.err.println("Try read from file with write flags");
        return null;
    }//from   w  w w .jav  a  2  s.co  m

    NodeList nodelist = doc.getElementsByTagName(tag);
    Mat readMat = null;

    for (int i = 0; i < nodelist.getLength(); i++) {
        Node node = nodelist.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            String type_id = element.getAttribute("type_id");
            if ("opencv-matrix".equals(type_id) == false) {
                System.out.println("Fault type_id ");
            }

            String rowsStr = element.getElementsByTagName("rows").item(0).getTextContent();
            String colsStr = element.getElementsByTagName("cols").item(0).getTextContent();
            String dtStr = element.getElementsByTagName("dt").item(0).getTextContent();
            String dataStr = element.getElementsByTagName("data").item(0).getTextContent();

            int rows = Integer.parseInt(rowsStr);
            int cols = Integer.parseInt(colsStr);
            int type = CvType.CV_8U;

            Scanner s = new Scanner(dataStr);

            if ("f".equals(dtStr)) {
                type = CvType.CV_32F;
                readMat = new Mat(rows, cols, type);
                float fs[] = new float[1];
                for (int r = 0; r < rows; r++) {
                    for (int c = 0; c < cols; c++) {
                        if (s.hasNextFloat()) {
                            fs[0] = s.nextFloat();
                        } else {
                            fs[0] = 0;
                            System.err.println("Unmatched number of float value at rows=" + r + " cols=" + c);
                        }
                        readMat.put(r, c, fs);
                    }
                }
            } else if ("i".equals(dtStr)) {
                type = CvType.CV_32S;
                readMat = new Mat(rows, cols, type);
                int is[] = new int[1];
                for (int r = 0; r < rows; r++) {
                    for (int c = 0; c < cols; c++) {
                        if (s.hasNextInt()) {
                            is[0] = s.nextInt();
                        } else {
                            is[0] = 0;
                            System.err.println("Unmatched number of int value at rows=" + r + " cols=" + c);
                        }
                        readMat.put(r, c, is);
                    }
                }
            } else if ("s".equals(dtStr)) {
                type = CvType.CV_16S;
                readMat = new Mat(rows, cols, type);
                short ss[] = new short[1];
                for (int r = 0; r < rows; r++) {
                    for (int c = 0; c < cols; c++) {
                        if (s.hasNextShort()) {
                            ss[0] = s.nextShort();
                        } else {
                            ss[0] = 0;
                            System.err.println("Unmatched number of int value at rows=" + r + " cols=" + c);
                        }
                        readMat.put(r, c, ss);
                    }
                }
            } else if ("b".equals(dtStr)) {
                readMat = new Mat(rows, cols, type);
                byte bs[] = new byte[1];
                for (int r = 0; r < rows; r++) {
                    for (int c = 0; c < cols; c++) {
                        if (s.hasNextByte()) {
                            bs[0] = s.nextByte();
                        } else {
                            bs[0] = 0;
                            System.err.println("Unmatched number of byte value at rows=" + r + " cols=" + c);
                        }
                        readMat.put(r, c, bs);
                    }
                }
            }
        }
    }
    return readMat;
}

From source file:ch.zhaw.facerecognitionlibrary.PreProcessor.Contours.LocalBinaryPattern.java

License:Open Source License

@Override
public PreProcessor preprocessImage(PreProcessor preProcessor) {
    List<Mat> images = preProcessor.getImages();
    List<Mat> processed = new ArrayList<Mat>();
    for (Mat img : images) {
        // Resize for Performance enhancement
        Size size = new Size(preProcessor.getN(), preProcessor.getN());
        Imgproc.resize(img, img, size);//from   w ww.  j  a  v a2s .com
        Mat lbp = new Mat(img.rows() - 2, img.cols() - 2, img.type());
        for (int i = 1; i < img.rows() - 1; i++) {
            for (int j = 1; j < img.cols() - 1; j++) {
                BitSet out = new BitSet(8);
                double cen = img.get(i, j)[0];
                if (img.get(i - 1, j - 1)[0] > cen)
                    out.set(0);
                if (img.get(i - 1, j)[0] > cen)
                    out.set(1);
                if (img.get(i - 1, j + 1)[0] > cen)
                    out.set(2);
                if (img.get(i, j + 1)[0] > cen)
                    out.set(3);
                if (img.get(i + 1, j + 1)[0] > cen)
                    out.set(4);
                if (img.get(i + 1, j)[0] > cen)
                    out.set(5);
                if (img.get(i + 1, j - 1)[0] > cen)
                    out.set(6);
                if (img.get(i, j - 1)[0] > cen)
                    out.set(7);
                int value = 0;
                for (int k = 0; k < out.length(); k++) {
                    int index = out.nextSetBit(k);
                    value += Math.pow(2, out.length() - 1 - index);
                    k = index;
                }
                lbp.put(i - 1, j - 1, value);
            }
        }
        processed.add(lbp);
    }
    preProcessor.setImages(processed);
    return preProcessor;
}

From source file:ch.zhaw.facerecognitionlibrary.PreProcessor.StandardPreprocessing.EyeAlignment.java

License:Open Source License

public PreProcessor preprocessImage(PreProcessor preProcessor) {
    List<Mat> images = preProcessor.getImages();
    List<Mat> processed = new ArrayList<Mat>();
    preProcessor.setEyes();//from  w  ww. j a va2 s  .c o  m
    Eyes[] eyes = preProcessor.getEyes();
    if (eyes == null || eyes[0] == null) {
        return null;
    }
    for (int i = 0; i < images.size(); i++) {
        Mat img = images.get(i);
        Eyes eye = eyes[i];
        double desiredLen = (DESIRED_LEFT_EYE_X - DESIRED_RIGHT_EYE_X) * img.cols();
        double scale = 0.9 * desiredLen / eye.getDist();
        MatOfFloat leftCenter = eye.getLeftCenter();
        MatOfFloat rightCenter = eye.getRightCenter();
        double centerX = ((leftCenter.get(0, 0)[0] + rightCenter.get(0, 0)[0]) / 2);
        double centerY = ((leftCenter.get(1, 0)[0] + rightCenter.get(1, 0)[0]) / 2);
        Mat rotMat = Imgproc.getRotationMatrix2D(new Point(centerX, centerY), eye.getAngle(), scale);
        rotMat.put(2, 0, img.cols() * 0.5 - centerX);
        rotMat.put(2, 1, img.rows() * DESIRED_RIGHT_EYE_Y - centerY);
        Imgproc.warpAffine(img, img, rotMat, new Size(img.cols(), img.rows()));
        processed.add(img);
    }
    preProcessor.setImages(processed);
    return preProcessor;
}

From source file:ch.zhaw.facerecognitionlibrary.Recognition.Eigenfaces.java

License:Open Source License

public String recognize(Mat img, String expectedLabel) {
    // Ignore/*from   w ww.j a va2s  .c  om*/
    img = img.reshape(1, 1);
    // Subtract mean
    img.convertTo(img, CvType.CV_32F);
    Core.subtract(img, Psi, img);
    // Project to subspace
    Mat projected = getFeatureVector(img);
    // Save all points of image for tSNE
    img.convertTo(img, CvType.CV_8U);
    addImage(projected, expectedLabel, true);
    //addImage(projected, expectedLabel);
    Mat distance = new Mat(Omega.rows(), 1, CvType.CV_64FC1);
    for (int i = 0; i < Omega.rows(); i++) {
        double dist = Core.norm(projected.row(0), Omega.row(i), Core.NORM_L2);
        distance.put(i, 0, dist);
    }
    Mat sortedDist = new Mat(Omega.rows(), 1, CvType.CV_8UC1);
    Core.sortIdx(distance, sortedDist, Core.SORT_EVERY_COLUMN + Core.SORT_ASCENDING);
    // Give back the name of the found person
    int index = (int) (sortedDist.get(0, 0)[0]);
    return labelMap.getKey(labelList.get(index));
}

From source file:Clases.Analizador.java

public BufferedImage analizarCatarata(Mat img) {
    double[] pupila = segmentador.segmentarPupila(img);
    double[] clase = { 99.8125, 150.7500, 150.8125 };
    double areaPupila = Math.PI * Math.pow(pupila[2], 2);
    int acumulador = 0;
    //Core.circle(img, new Point(pupila[0], pupila[1]), (int) pupila[2], new Scalar(255, 0, 0), 3);
    //Ventana v1 = new Ventana(convertir(max), 1, 0);
    //Ventana v2 = new Ventana(convertir(img), 3, 0);
    Size sizeA = img.size();/*from   www . jav a  2  s . co  m*/
    Mat max = img.clone();

    double[] data;
    for (int i = 0; i < sizeA.height; i++) {
        for (int j = 0; j < sizeA.width; j++) {
            if ((Math.pow(pupila[0] - j, 2) + Math.pow(pupila[1] - i, 2) <= Math.pow(pupila[2], 2))) {
                data = img.get(i, j);
                double res = Math.sqrt(Math.pow(clase[0] - data[2], 2) + Math.pow(clase[1] - data[1], 2)
                        + Math.pow(clase[2] - data[0], 2));
                if (res < 60) {
                    data[0] = 0;
                    data[1] = 255;
                    data[2] = 0;
                    acumulador++;
                }
                max.put(i, j, data);
            }
        }
    }
    resultadoCatarata = (acumulador * 100) / areaPupila;
    resultadoCatarata = (double) ((int) (resultadoCatarata * 100.00) / 100);
    //Core.circle(imagen, new Point(pupila[0], pupila[1]), (int) pupila[2], new Scalar(255, 0, 0), 3);
    //Ventana v1 = new Ventana(segmentador.convertir(max), 0, 0);
    //Ventana v2 = new Ventana(convertir(imagen), 2, 0);
    return segmentador.Mat2BufferedImage(max);
}

From source file:Clases.Segmentador.java

public Mat detectaPterigion(Mat img_ana, int val1, int val2, int val3) {
    int r = 200, g = 125, b = 115;
    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] >= r - val1 && color[2] <= r + val1 && color[1] >= g - val2 && color[1] <= g + val2
                    && color[0] >= b - val3 && color[0] <= b + val3) {
                color[0] = 0;/*  ww w. j  a  v  a  2 s . co m*/
                color[1] = 255;
                color[2] = 255;
            }
            img_ana.put(y, x, color);
        }
    }
    return img_ana;
}

From source file:Clases.Segmentador.java

public int detectaEsclerotica(Mat img_ana, int val1, int val2, int val3) {
    int r = 190, g = 187, b = 181, 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] >= r - val1 && color[2] <= r + val1 && color[1] >= g - val2 && color[1] <= g + val2
                    && color[0] >= b - val3 && color[0] <= b + val3) {
                area++;//from  w w w.  ja v a2 s  .  com
            }
            img_ana.put(y, x, color);
        }
    }
    return area;
}

From source file:Clases.Segmentador.java

public Mat dameLaDona(Mat img1) {
    double[] iris = segmentarIris(img1);
    double[] pupila = segmentarPupila(img1);

    Size sizeA = img1.size();/*from   www .j a  v a2 s  .  c  o m*/
    Mat max = img1.clone();

    double[] data;
    for (int i = 0; i < sizeA.height; i++) {
        for (int j = 0; j < sizeA.width; j++) {
            if ((Math.pow(iris[0] - j, 2) + Math.pow(iris[1] - i, 2) >= Math.pow(iris[2], 2))
                    || (Math.pow(pupila[0] - j, 2) + Math.pow(pupila[1] - i, 2) <= Math.pow(pupila[2], 2))) {
                data = img1.get(i, j);
                data[0] = 255;
                data[1] = 255;
                data[2] = 255;
                max.put(i, j, data);
            }

        }
    }
    return max;
}

From source file:Clases.Segmentador.java

public Mat limitaIris(Mat img1) {

    double[] iris = segmentarIris(img1);
    iris[2] = iris[2] * 0.8;/*from w w w.j av  a  2 s  . co  m*/
    Size sizeA = img1.size();
    Mat max = img1.clone();

    double[] data;
    for (int i = 0; i < sizeA.height; i++) {
        for (int j = 0; j < sizeA.width; j++) {
            if ((Math.pow(iris[0] - j, 2) + Math.pow(iris[1] - i, 2) >= Math.pow(iris[2], 2))) {
                data = img1.get(i, j);
                data[0] = 255;
                data[1] = 255;
                data[2] = 255;
                max.put(i, j, data);
            }

        }
    }
    return max;
}