Example usage for org.opencv.core Mat ones

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

Introduction

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

Prototype

public static Mat ones(int rows, int cols, int type) 

Source Link

Usage

From source file:Questao2.java

void maximo() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    String url = escolherUrl();/* w ww  .  j  a  va  2s  . c o m*/
    /**
     * Transforma imagem em matriz para facilitar manipulacao
     */
    Mat img = Imgcodecs.imread(url);
    /**
     * Cria matriz destinoo
     */
    Mat dst = new Mat();
    /**
     * Cria imagem com o valor 1 em todos os pixels de
     * tamnho escolhido pelo usuario
     */
    System.out.println("Digite as dimenses da mscara: ");
    System.out.print("X: ");
    int mx = in.nextInt();
    System.out.print("Y: ");
    int my = in.nextInt();
    Mat one = Mat.ones(mx, my, CvType.CV_32F);
    /**
     * Aplica o filtro maximo utilizando a matriz one como mascara
     * dilate(imagem original, imagem destino, mascara)
     */
    Imgproc.dilate(img, dst, one);
    /**
     * Salva o resultado na matriz maximo.jpg
     */
    Imgcodecs.imwrite("maximo.jpg", dst);
    showResult("maximo.jpg");
}

From source file:Questao2.java

void minimo() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    String url = escolherUrl();/*  ww w .j ava2 s .  c  o m*/
    /**
     * Transforma imagem em matriz para facilitar manipulacao
     */
    Mat img = Imgcodecs.imread(url);
    /**
     * Cria matriz de destino
     */
    Mat dst = new Mat();
    /**
     * Cria matriz com o valor 1 em todos os pixels de
     * tamanho definido pelo usuario
     */
    System.out.println("Digite as dimenses da mscara: ");
    System.out.print("X: ");
    int mx = in.nextInt();
    System.out.print("Y: ");
    int my = in.nextInt();
    Mat one = Mat.ones(mx, my, CvType.CV_32F);
    /**
     * Aplica o filtro minimo
     * erode(imagem original, imagem destino, matriz de mascara)
     */
    Imgproc.erode(img, dst, one);
    /**
     * Salva resultado em minimo.jpg
     */
    Imgcodecs.imwrite("minimo.jpg", dst);
    showResult("minimo.jpg");
}

From source file:MainBox.java

public static void main(String[] args) {

    try {//from w w w. j a  va 2 s . c o  m

        int kernelSize = 9; //ao aumentar o valor a imagem fica mais embassada

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat source = Highgui.imread("D://teste_gray.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);

        Mat destination = new Mat(source.rows(), source.cols(), source.type());

        Mat kernel = Mat.ones(kernelSize, kernelSize, CvType.CV_32F);

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

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

                double[] m = kernel.get(j, j);

                for (int k = 0; k < m.length; k++) {
                    m[k] = m[k] / (kernelSize * kernelSize);
                }
                kernel.put(i, j, m);
            }
        }

        Imgproc.filter2D(source, destination, -1, kernel);

        Highgui.imwrite("D://Box.jpg", destination);

    } catch (Exception e) {
        System.out.println("Exception: " + e.getMessage());
    }
}

From source file:com.github.mbillingr.correlationcheck.ImageProcessor.java

License:Open Source License

public List<Point> extractPoints() {
    Mat gray = new Mat();//work_width, work_height, CvType.CV_8UC1);
    Mat binary = new Mat();

    Mat kernel = Mat.ones(3, 3, CvType.CV_8UC1);

    debugreset();//  w  ww .ja  v a  2  s .c om

    Mat image = load_transformed();
    working_image = image.clone();
    debugsave(image, "source");

    Imgproc.cvtColor(image, gray, Imgproc.COLOR_RGB2GRAY);
    debugsave(gray, "grayscale");

    Imgproc.GaussianBlur(gray, gray, new Size(15, 15), 0);
    debugsave(gray, "blurred");

    //Imgproc.equalizeHist(gray, gray);
    //debugsave(gray, "equalized");

    Imgproc.adaptiveThreshold(gray, binary, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV,
            129, 5);
    //Imgproc.threshold(gray, binary, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
    //Imgproc.threshold(gray, binary, 128, 255, Imgproc.THRESH_BINARY_INV);
    debugsave(binary, "binary");

    Imgproc.morphologyEx(binary, binary, Imgproc.MORPH_CLOSE, kernel);
    debugsave(binary, "closed");

    Imgproc.morphologyEx(binary, binary, Imgproc.MORPH_OPEN, kernel);
    debugsave(binary, "opened");

    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(binary, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); // is binary is now changed
    Imgproc.drawContours(image, contours, -1, new Scalar(0, 0, 255), 3);
    debugsave(image, "contours");

    List<PointAndArea> points = new ArrayList<>();

    for (MatOfPoint cnt : contours) {
        MatOfPoint2f c2f = new MatOfPoint2f();
        c2f.fromArray(cnt.toArray());
        RotatedRect rr = Imgproc.minAreaRect(c2f);

        double area = Imgproc.contourArea(cnt);

        if (rr.size.width / rr.size.height < 3 && rr.size.height / rr.size.width < 3 && rr.size.width < 64
                && rr.size.height < 64 && area > 9 && area < 10000) {
            points.add(new PointAndArea((int) area, rr.center));
        }
    }

    List<Point> final_points = new ArrayList<>();

    Collections.sort(points);
    Collections.reverse(points);
    int prev = -1;
    for (PointAndArea p : points) {
        Log.i("area", Integer.toString(p.area));
        if (prev == -1 || p.area >= prev / 2) {
            prev = p.area;
            Imgproc.circle(image, p.point, 10, new Scalar(0, 255, 0), 5);
            final_points.add(new Point(1 - p.point.y / work_height, 1 - p.point.x / work_width));
        }
    }
    debugsave(image, "circles");

    return final_points;
}

From source file:ctPrincipal.Filtros.java

private String maximo(int mx, int my) {
    Mat img = Imgcodecs.imread(imgS);//from  w  w w  . ja  va  2s . c  om
    Mat one = Mat.ones(mx, my, CvType.CV_32F);
    Imgproc.dilate(img, output, one);
    Imgcodecs.imwrite("OutputImg/maximo.jpg", output);
    return "OutputImg/maximo.jpg";
}

From source file:ctPrincipal.Filtros.java

private String minimo(int mx, int my) {
    Mat img = Imgcodecs.imread(imgS);//from  ww w  . ja va 2s  .co  m
    Mat one = Mat.ones(mx, my, CvType.CV_32F);
    Imgproc.erode(img, output, one);
    Imgcodecs.imwrite("OutputImg/minimo.jpg", output);
    return "OutputImg/minimo.jpg";
}