Example usage for org.opencv.core Mat type

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

Introduction

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

Prototype

public int type() 

Source Link

Usage

From source file:ImagetoPDF.java

public static void enhance(String fileName) throws IOException {

    Mat source = Imgcodecs.imread(fileName, Imgcodecs.CV_LOAD_IMAGE_COLOR);
    Mat destination = new Mat(source.rows(), source.cols(), source.type());
    //    Imgproc.cvtColor(source, destination, Imgproc.COLOR_BGR2GRAY);
    Imgproc.cvtColor(source, source, Imgproc.COLOR_BGR2GRAY);
    Mat imageMat = source;//from  ww w . j  a  v a2 s  . c  o  m
    Imgproc.GaussianBlur(imageMat, imageMat, new Size(3, 3), 0);
    Imgproc.adaptiveThreshold(imageMat, imageMat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 5,
            4);
    Imgcodecs.imwrite(fileName, imageMat);
}

From source file:MainBorder.java

public static void main(String[] args) {

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

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat source = Highgui.imread("D:\\teste.png", Highgui.CV_LOAD_IMAGE_COLOR);

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

        int top, bottom, left, right;

        int borderType;

        //initialize arguments for the filter border

        top = (int) (0.05 * source.rows());
        bottom = (int) (0.05 * source.rows());

        left = (int) (0.15 * source.cols());
        right = (int) (0.15 * source.cols());

        destination = source;

        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_WRAP); //borda com a imagem
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_CONSTANT); //borda preta
        Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_DEFAULT); //borda transparente
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_ISOLATED); // borda escura??
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REFLECT); //borda com reflexo da prpria imagem
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REFLECT101);
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REFLECT_101);
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REPLICATE); //borda efeito movimento
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_TRANSPARENT); //borda no funciona

        Highgui.imwrite("D:\\borderWrap.jpg", destination);

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

}

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   ww w.jav a  2  s  . co  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:MainEroding.java

public static void main(String[] args) {

    try {/*  w w w. jav  a 2 s . c om*/

        int erosion_size = 2;
        //int dilation_size = 5;

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat source = Highgui.imread("D://teste.png", Highgui.CV_LOAD_IMAGE_COLOR);

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

        destination = source;

        Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
                new Size(2 * erosion_size + 1, 2 * erosion_size + 1));

        Imgproc.erode(source, destination, element);

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

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

}

From source file:MainGaussian.java

public static void main(String[] args) {

    try {/*from  ww  w . j av a 2 s.c o m*/
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat source = Highgui.imread("D://teste.png", Highgui.CV_LOAD_IMAGE_COLOR);

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

        Imgproc.GaussianBlur(source, destination, new Size(45, 45), 0);
        //Imgproc.GaussianBlur(source, destination, new Size(65,65), 0);
        //Imgproc.GaussianBlur(source, destination, new Size(25,1), 0);
        Highgui.imwrite("D://Gaussian45.jpg", destination);

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

From source file:MainThresholding.java

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    try {/*from w  w w .j  ava  2  s .c o m*/

        Mat source = Highgui.imread("D://teste.png", Highgui.CV_LOAD_IMAGE_COLOR);

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

        destination = source;

        //Imgproc.threshold(source, destination, 125, 255, Imgproc.THRESH_TOZERO);
        //Imgproc.threshold(source, destination, 125, 255, Imgproc.THRESH_BINARY);
        //Imgproc.threshold(source, destination, 125, 255, Imgproc.THRESH_BINARY_INV);
        //Imgproc.threshold(source, destination, 125, 255, Imgproc.THRESH_TOZERO_INV);
        Imgproc.threshold(source, destination, 125, 255, Imgproc.THRESH_TRUNC);

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

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

}

From source file:PutFiltro.java

public static Mat mainBorda(Mat source) {
    Mat destination = new Mat(source.rows(), source.cols(), source.type());
    try {/*w  ww  .j av a 2s . c om*/

        //System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        /*
             Mat source = Highgui.imread("D:\\teste.png",
        Highgui.CV_LOAD_IMAGE_COLOR);
        */

        int top, bottom, left, right;

        int borderType;

        //initialize arguments for the filter border

        top = (int) (0.05 * source.rows());
        bottom = (int) (0.05 * source.rows());

        left = (int) (0.15 * source.cols());
        right = (int) (0.15 * source.cols());

        destination = source;

        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_WRAP); //borda com a imagem
        Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_CONSTANT); //borda preta
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_DEFAULT); //borda transparente
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_ISOLATED); // borda escura??
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REFLECT); //borda com reflexo da prpria imagem
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REFLECT101);
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REFLECT_101);
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REPLICATE); //borda efeito movimento
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_TRANSPARENT); //borda no funciona

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

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

From source file:Questao2.java

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

    Mat original_Bgr = image.clone();

    // cria uma imagem e inicializa com valores aleatorios
    Mat mGaussian_noise = new Mat(original_Bgr.size(), original_Bgr.type());
    System.out.print("Valor Principal: ");
    int mean = in.nextInt();
    System.out.print("Desvio Padro: ");
    int desv = in.nextInt();
    // randn(matriz destino, mean value, desvio padrao)
    randn(mGaussian_noise, mean, desv);// w  ww.j  a va2 s . com

    // aplicacao do ruido: original(clone) + mGaussian_noise
    for (int m = 0; m < original_Bgr.rows(); m++) {
        for (int n = 0; n < original_Bgr.cols(); n++) {
            double[] val = new double[3];
            for (int i = 0; i < original_Bgr.get(m, n).length; i++) {
                val[i] = original_Bgr.get(m, n)[i] + mGaussian_noise.get(m, n)[i];
            }
            original_Bgr.put(m, n, val);
        }
    }

    // normalize(matriz entrada, matriz saida, valor minimo, valor maximo, tipo de normalizacao, tipo da imagem de saida)
    normalize(original_Bgr, original_Bgr, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC3);

    // salva resultado do ruido gaussiano na imagem "gaussian.jpg"
    Imgcodecs.imwrite("gaussian.jpg", original_Bgr);
    showResult("gaussian.jpg");
}

From source file:Questao2.java

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

    // obtem clone da matriz original
    Mat saltPepper_img = image.clone();
    // cria matriz para o ruido e inicializa com valor aleatorios
    Mat mSaltPepper_noise = new Mat(saltPepper_img.size(), saltPepper_img.type());
    // randn(matriz destino, valor principal (espectativa), desvio padrao)
    randn(mSaltPepper_noise, 0, 255);//from w w  w.  ja  v a 2s .  co m
    System.out.print("Valor Mnimo: ");
    int min = in.nextInt();
    System.out.print("Valor Mximo: ");
    int max = in.nextInt();
    // utilizando da matriz de numeros aleatorios, verifica valores
    // muito baixos e os substituem por zero na matriz resultante (copia da original)
    // e os valores muito altos sao substituidos por 255
    for (int m = 0; m < saltPepper_img.rows(); m++) {
        for (int n = 0; n < saltPepper_img.cols(); n++) {
            double[] val = new double[3];
            if (mSaltPepper_noise.get(m, n)[0] < min && mSaltPepper_noise.get(m, n)[1] < min
                    && mSaltPepper_noise.get(m, n)[2] < min) {
                for (int i = 0; i < saltPepper_img.get(m, n).length; i++) {
                    val[i] = 0;
                }
                saltPepper_img.put(m, n, val);
            }
            if (mSaltPepper_noise.get(m, n)[0] > max && mSaltPepper_noise.get(m, n)[1] > max
                    && mSaltPepper_noise.get(m, n)[2] > max) {
                for (int i = 0; i < saltPepper_img.get(m, n).length; i++) {
                    val[i] = 255;
                }
                saltPepper_img.put(m, n, val);
            }
        }
    }

    // normalize(matriz entrada, matriz saida, valor minimo, valor maximo, tipo de normalizacao, tipo da imagem de saida)
    normalize(saltPepper_img, saltPepper_img, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC3);

    /**
     * Salva imagem resultante em saltapepper.jpg
     */
    Imgcodecs.imwrite("saltpepper.jpg", saltPepper_img);
    showResult("saltpepper.jpg");
}

From source file:MainBox.java

public static void main(String[] args) {

    try {//  w w  w.  ja  v  a  2  s .com

        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());
    }
}