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: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. ja  v a  2s  .  com*/
    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 Point drawRectangleOnPreview(Mat img, Rect face, boolean front_camera) {
    if (front_camera) {
        int topLeftX = (int) (img.cols() - (face.tl().x + face.width));
        int bottomRightX = (int) (img.cols() - (face.br().x) + face.width);
        Point tl = new Point(topLeftX, face.tl().y);
        Point br = new Point(bottomRightX, face.br().y);
        Imgproc.rectangle(img, tl, br, FACE_RECT_COLOR, THICKNESS);
        return tl;
    } else {//from   w  ww.  j a  v a  2  s  .  c  om
        Imgproc.rectangle(img, face.tl(), face.br(), FACE_RECT_COLOR, THICKNESS);
        return face.tl();
    }
}

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 w  w w . j a  v a2s  . co  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 {/*  w  w  w .j  a va  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);/*from   ww w . j ava  2  s.c o  m*/
                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);/*from w w  w. j ava 2  s. c  o 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 ww  .  ja  v a  2  s  . c  om
    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.SupportVectorMachine.java

License:Open Source License

private String imageToSvmString(Mat img, String label) {
    int iLabel = 0;
    if (method == TRAINING) {
        if (labelMap.containsKey(label)) {
            iLabel = labelMap.getValue(label);
        } else {//from www  .  j  av a  2s .  c  o m
            iLabel = labelMap.size() + 1;
            labelMap.put(label, iLabel);
        }
    } else {
        if (labelMapTest.containsKey(label)) {
            iLabel = labelMapTest.getValue(label);
        } else {
            iLabel = labelMapTest.size() + 1;
            labelMapTest.put(label, iLabel);
        }
    }
    String result = String.valueOf(iLabel);
    img = getFeatureVector(img);
    for (int i = 0; i < img.cols(); i++) {
        result = result + " " + i + ":" + img.get(0, i)[0];
    }
    result += "\n";
    return result;
}

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