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:OCV_GetRotationMatrix2D.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    Mat mat = Imgproc.getRotationMatrix2D(new Point(center_x, center_y), angle, scale);

    if (mat == null || mat.rows() <= 0 || mat.cols() <= 0) {
        IJ.showMessage("Output is null or error");
        return;/*ww w  .  ja  v  a 2 s . c  om*/
    }

    ResultsTable rt = OCV__LoadLibrary.GetResultsTable(true);
    rt.incrementCounter();
    rt.addValue("Column01", String.valueOf(mat.get(0, 0)[0]));
    rt.addValue("Column02", String.valueOf(mat.get(0, 1)[0]));
    rt.addValue("Column03", String.valueOf(mat.get(0, 2)[0]));
    rt.incrementCounter();
    rt.addValue("Column01", String.valueOf(mat.get(1, 0)[0]));
    rt.addValue("Column02", String.valueOf(mat.get(1, 1)[0]));
    rt.addValue("Column03", String.valueOf(mat.get(1, 2)[0]));
    rt.show("Results");
}

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   w  ww .  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  om

        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:MainEroding.java

public static void main(String[] args) {

    try {/*  w w  w . ja va  2  s.  c  o  m*/

        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 {/*  w  w  w . j a  va  2s.c  om*/
        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:MainShapeConversion.java

public static void main(String[] args) {

    try {//from   ww w  .j av  a  2  s  .co  m

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        File input = new File("D://teste.png");

        BufferedImage image = ImageIO.read(input);

        byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();

        Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);

        mat.put(0, 0, data);

        Mat mat1 = new Mat(image.getWidth(), image.getHeight(), CvType.CV_8UC3);

        Core.flip(mat, mat1, -1); //-1 invert , 1 normal

        byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int) (mat1.elemSize())];

        mat1.get(0, 0, data1);

        BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), 5);

        image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);

        File output = new File("D://hsv.jpg");

        ImageIO.write(image1, "jpg", output);

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

    }
}

From source file:Video.java

public BufferedImage matToBufferedImage(Mat matrix) {
    int cols = matrix.cols();
    int rows = matrix.rows();
    int elemSize = (int) matrix.elemSize();
    byte[] data = new byte[cols * rows * elemSize];
    int type;//from w  w  w. j ava 2s. c  o m

    matrix.get(0, 0, data);

    switch (matrix.channels()) {
    case 1:
        type = BufferedImage.TYPE_BYTE_GRAY;
        break;

    case 3:
        type = BufferedImage.TYPE_3BYTE_BGR;

        // bgr to rgb
        byte b;
        for (int i = 0; i < data.length; i = i + 3) {
            b = data[i];
            data[i] = data[i + 2];
            data[i + 2] = b;
        }
        break;

    default:
        return null;
    }

    BufferedImage image = new BufferedImage(cols, rows, type);
    image.getRaster().setDataElements(0, 0, cols, rows, data);

    return image;
}

From source file:MainThresholding.java

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

    try {//from w ww.  j  a v a 2  s . co 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 {//from   w ww.j a  va2  s  .  c  o  m

        //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  w  w.j av  a 2  s  .co m

    // 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");
}