Example usage for org.opencv.core Mat Mat

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

Introduction

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

Prototype

public Mat() 

Source Link

Usage

From source file:ImageReade.java

public static void main(String[] args) {

    // Load an image file and display it in a window.
    Mat m1 = Highgui.imread("H:\\error1.jpg");
    imshow("Original", m1);

    // Do some image processing on the image and display in another window.
    Mat m2 = new Mat();
    Imgproc.bilateralFilter(m1, m2, -1, 50, 10);
    Imgproc.Canny(m2, m2, 10, 200);// www .j  av  a 2  s  . c  om
    imshow("Edge Detected", m2);
    detectLetter(m1);

}

From source file:ImageReade.java

public static void detectLetter(Mat img) {
    ArrayList<Rect> boundRect = new ArrayList<>();
    Mat img_gray, img_sobel, img_threshold, element;
    img_gray = new Mat();
    img_sobel = new Mat();
    img_threshold = new Mat();
    element = new Mat();
    Imgproc.cvtColor(img, img_gray, Imgproc.COLOR_BGRA2GRAY);
    imshow("Rec img_gray", img_gray);
    Imgproc.Sobel(img_gray, img_sobel, CvType.CV_8U, 1, 0, 3, 1, 0, Imgproc.BORDER_DEFAULT);
    imshow("Rec img_sobel", img_sobel);
    Imgproc.threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
    imshow("Rec img_threshold", img_threshold);

    element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(16, 6));

    Imgproc.morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element);
    imshow("Rec img_threshold second", img_threshold);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    //Imgproc.findContours(img_threshold, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
    Imgproc.findContours(img_threshold, contours, new Mat(), 0, 1);

    for (int i = 0; i < contours.size(); i++) {
        System.out.println(Imgproc.contourArea(contours.get(i)));
        //            if (Imgproc.contourArea(contours.get(i)) > 100) {
        //                //Imgproc.approxPolyDP( contours.get(i), contours_poly[i], 3, true );
        //                Rect rect = Imgproc.boundingRect(contours.get(i));
        //                System.out.println(rect.height);
        //                if (rect.width > rect.height) {
        //                    //System.out.println(rect.x +","+rect.y+","+rect.height+","+rect.width);
        //                    Core.rectangle(img, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
        //                }
        //                    
        //                    
        //            }
        if (Imgproc.contourArea(contours.get(i)) > 100) {
            MatOfPoint2f mMOP2f1 = new MatOfPoint2f();
            MatOfPoint2f mMOP2f2 = new MatOfPoint2f();
            contours.get(i).convertTo(mMOP2f1, CvType.CV_32FC2);
            Imgproc.approxPolyDP(mMOP2f1, mMOP2f2, 3, true);
            mMOP2f2.convertTo(contours.get(i), CvType.CV_32S);
            Rect rect = Imgproc.boundingRect(contours.get(i));
            if (rect.width > rect.height) {
                Core.rectangle(img, new Point(rect.x, rect.y),
                        new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255));
            }/*from   w  w  w . j a  v a  2s .c om*/
        }
    }
    imshow("Rec Detected", img);
}

From source file:Questao1.java

public Questao1() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    this.img1 = "lena.jpg";
    this.img2 = "baboon.jpg";

    this.image2 = Imgcodecs.imread(img2);
    this.image1 = Imgcodecs.imread(img1);

    this.image1bin = new Mat();
    this.image2bin = new Mat();

    this.output = new Mat(256, 256, CvType.CV_8UC3);

    showResult(img1);/*from   w  w  w  . j a v  a 2s .  c  om*/
    showResult(img2);

    in = new Scanner(System.in);
    do {
        System.out.println("Escolha a operao: ");
        System.out.println("1 - AND");
        System.out.println("2 - OR");
        System.out.println("3 - XOR");
        System.out.println("4 - NOT");
        System.out.println("5 - SOMA");
        System.out.println("6 - SUBTRAO");
        System.out.println("7 - MULTIPLICAO");
        System.out.println("8 - DIVISO");
        System.out.println("0 - SAIR");

        while ((op = in.nextInt()) != 0) {
            realizarOperacoes();
        }
        if (op == 0) {
            return;
        }
    } while (op < 0 || op > 8);
}

From source file:OCV_CntrlUvcCamera.java

License:Open Source License

@Override
public void run(ImageProcessor arg0) {
    boolean bret = true;

    // ----- stop dialog during continuous grabbing -----
    diag_free = new JDialog(diag_free, title, false);
    JButton but_stop_cont = new JButton("Stop");

    but_stop_cont.addMouseListener(new MouseAdapter() {
        @Override//from w  w  w.  ja va  2  s  .  c  o m
        public void mouseClicked(MouseEvent e) {
            flag_fin_loop = true;
            diag_free.dispose();
        }
    });

    diag_free.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            flag_fin_loop = true;
        }
    });

    diag_free.add(but_stop_cont);
    diag_free.setSize(100, 75);
    // ----- end of stop dialog -----

    // initialize camera
    VideoCapture src_cap = new VideoCapture();
    Mat src_mat = new Mat();
    bret = src_cap.open(device);

    if (!bret) {
        IJ.error("Camera initialization is failed.");
        diag_free.dispose();
        return;
    }

    src_cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
    src_cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);

    // Setting the image display window
    width = (int) src_cap.get(CV_CAP_PROP_FRAME_WIDTH);
    height = (int) src_cap.get(CV_CAP_PROP_FRAME_HEIGHT);

    ImagePlus impDsp = IJ.createImage(title, width, height, 1, 24);

    int[] impdsp_intarray = (int[]) impDsp.getChannelProcessor().getPixels();

    impDsp.show();
    impDsp.setRoi(0, 0, impDsp.getWidth(), impDsp.getHeight());

    // show stop dialog
    diag_free.setVisible(true);

    // run
    for (;;) {
        if (flag_fin_loop) {
            break;
        }

        // grab
        impDsp.startTiming();
        bret = src_cap.read(src_mat);
        IJ.showTime(impDsp, impDsp.getStartTime(), title + " : ");

        if (!bret) {
            IJ.error("Error occurred in grabbing.");
            diag_free.dispose();
            break;
        }

        if (src_mat.empty()) {
            IJ.error("Mat is empty.");
            diag_free.dispose();
            break;
        }

        // display
        if (src_mat.type() == CvType.CV_8UC3) {
            OCV__LoadLibrary.mat2intarray(src_mat, impdsp_intarray, width, height);
        } else {
            IJ.error("Color camera is supported only.");
            diag_free.dispose();
            break;
        }

        impDsp.draw();

        // wait
        wait(wait_time);
    }

    diag_free.dispose();

    if (src_cap.isOpened()) {
        src_cap.release();
    }
}

From source file:Questao2.java

void media() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    String url = escolherUrl();/*w w w.  ja v  a  2 s .com*/

    /**
     * Transforma imagem em matriz para facilitar manipulacao
     */
    Mat img = Imgcodecs.imread(url);
    /**
     * Cria matriz de destino
     */
    Mat dst = new Mat();
    /**
     * Aplicacao do filtro da media
     * blur(matriz original, matriz destino, tamanho da mascara)
     */

    System.out.println("Escolha as dimenses da mscara: ");
    System.out.print("X: ");
    int mx = in.nextInt();
    System.out.print("Y: ");
    int my = in.nextInt();

    Imgproc.blur(img, dst, new Size(mx, my));
    /**
     * Salva o resultado em media.jpg
     */
    Imgcodecs.imwrite("media.jpg", dst);
    showResult("media.jpg");
}

From source file:Questao2.java

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

    String url = escolherUrl();/*from www  .  j ava 2 s  .c  om*/
    /**
     * Transforma imagem em matriz para facilitar manipulacao
     */
    Mat img = Imgcodecs.imread(url);
    /**
     * Cria matriz de destino
     */
    Mat dst = new Mat();
    /**
     * Aplica filtro da mediana
     * medianBlur(imagem original, imagem destino, ksize)
     */
    System.out.print("K-size: ");
    int ksize = in.nextInt();
    Imgproc.medianBlur(img, dst, ksize);
    /**
     * Salva resultado em mediana.jpg
     */
    Imgcodecs.imwrite("mediana.jpg", dst);
    showResult("mediana.jpg");
}

From source file:Questao2.java

void gaussiano() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    String url = escolherUrl();//from w  w  w .  jav a 2 s. c o  m

    /**
     * Transforma imagem em matriz para facilitar manipulacao
     */
    Mat img = Imgcodecs.imread(url);
    /**
     * Cria matriz de destino
     */
    Mat dst = new Mat();
    /**
     * Aplica o filtro gaussiano
     * GaussianBlur(imagem original, imgagem destino, tamanho da mascara, sygmaX, sygmaY)
     */
    System.out.println("Dimenses da mscara: ");
    System.out.print("X: ");
    int mx = in.nextInt();
    System.out.print("Y: ");
    int my = in.nextInt();

    System.out.println("Valores Sygma: ");
    System.out.print("X: ");
    int sx = in.nextInt();
    System.out.print("Y: ");
    int sy = in.nextInt();
    Imgproc.GaussianBlur(img, dst, new Size(mx, my), sx, sy);
    /**
     * Salva resultado em gaussian-blur.jpg
     */
    Imgcodecs.imwrite("gaussian-blur.jpg", dst);
    showResult("gaussian-blur.jpg");
}

From source file:Questao2.java

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

    String url = escolherUrl();/*from   w w w .  j av  a  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();//from   w  w w .j a va2s . 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:OCV__LoadLibrary.java

License:Open Source License

private boolean isLoadOpenCV() {
    try {//from   w ww  .  j a  v  a 2s . c  o  m
        if (dummy != null) {
            //IJ.showMessage("dummy is release");
            dummy.release();
        }

        //IJ.error("dummy = new Mat()");
        dummy = new Mat();

        return true;
    } catch (Throwable ex) {
        //IJ.error("ERR : " + ex.getMessage());
        return false;
    }
}