Example usage for org.opencv.core Mat rows

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

Introduction

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

Prototype

public int rows() 

Source Link

Usage

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

License:Open Source License

public Eyes getEyes(Mat img) {
    double halfWidth = img.cols() / 2;
    double height = img.rows();
    double[] values = new double[4];
    values[0] = 0;/*from  www  .  ja va  2 s. c o m*/
    values[1] = 0;
    values[2] = halfWidth;
    values[3] = height;
    Rect rightHalf = new Rect(values);
    values[0] = halfWidth;
    Rect leftHalf = new Rect(values);
    MatOfRect rightEyes = new MatOfRect();
    MatOfRect leftEyes = new MatOfRect();

    Mat rightHalfImg = img.submat(rightHalf);
    rightEyeDetector.detectMultiScale(rightHalfImg, rightEyes);
    Mat leftHalfImg = img.submat(leftHalf);
    leftEyeDetector.detectMultiScale(leftHalfImg, leftEyes);

    if (rightEyes.empty() || leftEyes.empty() || rightEyes.toArray().length > 1
            || leftEyes.toArray().length > 1) {
        return null;
    }

    Rect rightEye = rightEyes.toArray()[0];
    Rect leftEye = leftEyes.toArray()[0];

    MatOfFloat rightPoint = new MatOfFloat(rightEye.x + rightEye.width / 2, rightEye.y + rightEye.height / 2);
    MatOfFloat leftPoint = new MatOfFloat(img.cols() / 2 + leftEye.x + leftEye.width / 2,
            leftEye.y + leftEye.height / 2);

    MatOfFloat diff = new MatOfFloat();
    Core.subtract(leftPoint, rightPoint, diff);
    double angle = Core.fastAtan2(diff.toArray()[1], diff.toArray()[0]);
    double dist = Core.norm(leftPoint, rightPoint, Core.NORM_L2);
    Eyes eyes = new Eyes(dist, rightPoint, leftPoint, angle);
    return eyes;
}

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

License:Open Source License

public String saveMatToImage(MatName m, String path) {
    String fullpath = path + m.getName() + ".png";
    Mat mat = m.getMat();
    Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(mat, bitmap);//w ww.  j a  v  a 2s . c  om
    File file = new File(fullpath);
    try {
        FileOutputStream os = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
        os.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fullpath;
}

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);//from www.j ava2s . c  o  m
        m.put(1, 0, face.y);
        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 void writeMat(String tag, Mat mat) {
    try {//from  ww  w  .  j  a v  a 2  s  .c o m
        if (isWrite == false) {
            System.err.println("Try write to file with no write flags");
            return;
        }

        Element matrix = doc.createElement(tag);
        matrix.setAttribute("type_id", "opencv-matrix");
        rootElement.appendChild(matrix);

        Element rows = doc.createElement("rows");
        rows.appendChild(doc.createTextNode(String.valueOf(mat.rows())));

        Element cols = doc.createElement("cols");
        cols.appendChild(doc.createTextNode(String.valueOf(mat.cols())));

        Element dt = doc.createElement("dt");
        String dtStr;
        int type = mat.type();
        if (type == CvType.CV_32F) { // type == CvType.CV_32FC1
            dtStr = "f";
        } else if (type == CvType.CV_32S) { // type == CvType.CV_32SC1
            dtStr = "i";
        } else if (type == CvType.CV_16S) { // type == CvType.CV_16SC1
            dtStr = "s";
        } else if (type == CvType.CV_8U) { // type == CvType.CV_8UC1
            dtStr = "b";
        } else {
            dtStr = "unknown";
        }
        dt.appendChild(doc.createTextNode(dtStr));

        Element data = doc.createElement("data");
        String dataStr = dataStringBuilder(mat);
        data.appendChild(doc.createTextNode(dataStr));

        // append all to matrix
        matrix.appendChild(rows);
        matrix.appendChild(cols);
        matrix.appendChild(dt);
        matrix.appendChild(data);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

private String dataStringBuilder(Mat mat) {
    StringBuilder sb = new StringBuilder();
    int rows = mat.rows();
    int cols = mat.cols();
    int type = mat.type();

    if (type == CvType.CV_32F) {
        float fs[] = new float[1];
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                mat.get(r, c, fs);/*  w w w  . ja  v a 2s .  c  om*/
                sb.append(String.valueOf(fs[0]));
                sb.append(' ');
            }
            sb.append('\n');
        }
    } else if (type == CvType.CV_32S) {
        int is[] = new int[1];
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                mat.get(r, c, is);
                sb.append(String.valueOf(is[0]));
                sb.append(' ');
            }
            sb.append('\n');
        }
    } else if (type == CvType.CV_16S) {
        short ss[] = new short[1];
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                mat.get(r, c, ss);
                sb.append(String.valueOf(ss[0]));
                sb.append(' ');
            }
            sb.append('\n');
        }
    } else if (type == CvType.CV_8U) {
        byte bs[] = new byte[1];
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                mat.get(r, c, bs);
                sb.append(String.valueOf(bs[0]));
                sb.append(' ');
            }
            sb.append('\n');
        }
    } else {
        sb.append("unknown type\n");
    }

    return sb.toString();
}

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);//  w  w w . j  a va  2  s. co m
        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  w w . ja va  2s  . 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: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;/*from w  w  w .ja v a  2s .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++;//ww w . jav  a 2s.c o m
            }
            img_ana.put(y, x, color);
        }
    }
    return area;
}

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++;//from   w  w  w .j a  va 2  s .  c  o m
            }
        }
    }
    return area;
}