Example usage for org.opencv.core Size Size

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

Introduction

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

Prototype

public Size(double width, double height) 

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;/* ww  w . j  a  va 2s. co  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: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   ww  w  . java 2  s  . com*/
        }
    }
    imshow("Rec Detected", img);
}

From source file:OCV_WarpPerspective.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    int imw = ip.getWidth();
    int imh = ip.getHeight();
    Size size = new Size((double) imw, (double) imh);
    Mat mat = new Mat(3, 3, CvType.CV_64FC1);

    for (int i = 0; i < 3; i++) {
        mat.put(i, 0, new double[] { Double.valueOf(rt.getStringValue(0, i).replaceAll("\"|'", "")) });
        mat.put(i, 1, new double[] { Double.valueOf(rt.getStringValue(1, i).replaceAll("\"|'", "")) });
        mat.put(i, 2, new double[] { Double.valueOf(rt.getStringValue(2, i).replaceAll("\"|'", "")) });
    }/*from w  w w . j  a v a 2  s .  c  om*/

    if (ip.getBitDepth() == 8) {
        byte[] srcdst_ar = (byte[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC1);

        src_mat.put(0, 0, srcdst_ar);
        Imgproc.warpPerspective(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        dst_mat.get(0, 0, srcdst_ar);
    } else if (ip.getBitDepth() == 16) {
        short[] srcdst_ar = (short[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_16UC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_16UC1);

        src_mat.put(0, 0, srcdst_ar);
        Imgproc.warpPerspective(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        dst_mat.get(0, 0, srcdst_ar);
    } else if (ip.getBitDepth() == 24) {
        int[] srcdst_ar = (int[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC3);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC3);

        OCV__LoadLibrary.intarray2mat(srcdst_ar, src_mat, imw, imh);
        Imgproc.warpPerspective(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        OCV__LoadLibrary.mat2intarray(dst_mat, srcdst_ar, imw, imh);
    } else if (ip.getBitDepth() == 32) {
        float[] srcdst_ar = (float[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_32FC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_32FC1);

        src_mat.put(0, 0, srcdst_ar);
        Imgproc.warpPerspective(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        dst_mat.get(0, 0, srcdst_ar);
    } else {
        IJ.error("Wrong image format");
    }
}

From source file:MainEroding.java

public static void main(String[] args) {

    try {/*from ww  w  .j a v  a  2 s .  co  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:frmMain.java

public static void showResult(Mat img) {
    Imgproc.resize(img, img, new Size(640, 480));
    MatOfByte matOfByte = new MatOfByte();
    Highgui.imencode(".jpg", img, matOfByte);
    byte[] byteArray = matOfByte.toArray();
    BufferedImage bufImage = null;
    try {//from  w  w  w .j  av  a2 s .co  m
        InputStream in = new ByteArrayInputStream(byteArray);
        bufImage = ImageIO.read(in);
        JFrame frame = new JFrame();
        frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
        frame.pack();
        frame.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MainGaussian.java

public static void main(String[] args) {

    try {//from ww w  . j  ava  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:Questao2.java

void media() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    String url = escolherUrl();/*from   w ww.  ja  v  a2 s. c  om*/

    /**
     * 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 gaussiano() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    String url = escolherUrl();//from   w w  w. jav  a  2s. 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:OCV_WarpAffine.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    int imw = ip.getWidth();
    int imh = ip.getHeight();
    Size size = new Size((double) imw, (double) imh);
    Mat mat = new Mat(2, 3, CvType.CV_64FC1);

    for (int i = 0; i < 2; i++) {
        mat.put(i, 0, new double[] { Double.valueOf(rt.getStringValue(0, i).replaceAll("\"|'", "")) });
        mat.put(i, 1, new double[] { Double.valueOf(rt.getStringValue(1, i).replaceAll("\"|'", "")) });
        mat.put(i, 2, new double[] { Double.valueOf(rt.getStringValue(2, i).replaceAll("\"|'", "")) });
    }/*from   w w w .  j a v  a 2 s.co  m*/

    if (ip.getBitDepth() == 8) {
        byte[] srcdst_ar = (byte[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC1);

        src_mat.put(0, 0, srcdst_ar);
        Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        dst_mat.get(0, 0, srcdst_ar);
    } else if (ip.getBitDepth() == 16) {
        short[] srcdst_ar = (short[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_16UC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_16UC1);

        src_mat.put(0, 0, srcdst_ar);
        Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        dst_mat.get(0, 0, srcdst_ar);
    } else if (ip.getBitDepth() == 24) {
        int[] srcdst_ar = (int[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC3);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC3);

        OCV__LoadLibrary.intarray2mat(srcdst_ar, src_mat, imw, imh);
        Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        OCV__LoadLibrary.mat2intarray(dst_mat, srcdst_ar, imw, imh);
    } else if (ip.getBitDepth() == 32) {
        float[] srcdst_ar = (float[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_32FC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_32FC1);

        src_mat.put(0, 0, srcdst_ar);
        Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        dst_mat.get(0, 0, srcdst_ar);
    } else {
        IJ.error("Wrong image format");
    }
}

From source file:ThirdTry.java

public static void detectLetter(Mat img, Mat m2) {
    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_8UC1, 1, 0, 3, 1, 0, Imgproc.BORDER_DEFAULT);
    //imshow("Rec img_sobel", img_sobel);
    Imgproc.threshold(m2, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
    //imshow("Rec img_threshold", img_threshold);

    element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 2));

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

    element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12, 12));
    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  a2 s .  c o m*/
        }
    }
    //imshow("Rec Detected", img);
}