Search in sources :

Example 1 with OpenCVFrameConverter

use of org.bytedeco.javacv.OpenCVFrameConverter in project javacv by bytedeco.

the class PrincipalComponentAnalysis method execute.

private void execute(String[] args) throws Exception {
    // If no params provided, compute the default image
    BufferedImage bufferedImage = args.length >= 1 ? ImageIO.read(new File(args[0])) : ImageIO.read(this.getClass().getResourceAsStream("shapes2.jpg"));
    System.out.println("Image type: " + bufferedImage.getType());
    // Convert BufferedImage to Mat and create AutoCloseable objects
    try (Mat matrix = new OpenCVFrameConverter.ToMat().convert(new Java2DFrameConverter().convert(bufferedImage));
        Mat mask = new Mat();
        Mat gray = new Mat();
        Mat denoised = new Mat();
        Mat bin = new Mat();
        Mat hierarchy = new Mat();
        MatVector contours = new MatVector()) {
        printMat(matrix);
        cvtColor(matrix, gray, COLOR_BGR2GRAY);
        // Normalize
        GaussianBlur(gray, denoised, new Size(5, 5), 0);
        threshold(denoised, mask, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
        normalize(gray, gray, 0, 255, NORM_MINMAX, -1, mask);
        // Convert image to binary
        threshold(gray, bin, 150, 255, THRESH_BINARY);
        // Find contours
        findContours(bin, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);
        long contourCount = contours.size();
        System.out.println("Countour count " + contourCount);
        for (int i = 0; i < contourCount; ++i) {
            // Calculate the area of each contour
            Mat contour = contours.get(i);
            double area = contourArea(contour);
            // Ignore contours that are too small or too large
            if (area > 128 && area < 8192) {
                principalComponentAnalysis(contour, i, matrix);
            }
        }
        CanvasFrame canvas = new CanvasFrame("PrincipalComponentAnalysis", 1);
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        canvas.setCanvasSize(320, 240);
        OpenCVFrameConverter converter = new OpenCVFrameConverter.ToIplImage();
        canvas.showImage(converter.convert(matrix));
    }
}
Also used : BufferedImage(java.awt.image.BufferedImage) OpenCVFrameConverter(org.bytedeco.javacv.OpenCVFrameConverter) File(java.io.File) Java2DFrameConverter(org.bytedeco.javacv.Java2DFrameConverter) CanvasFrame(org.bytedeco.javacv.CanvasFrame)

Example 2 with OpenCVFrameConverter

use of org.bytedeco.javacv.OpenCVFrameConverter in project javacv by bytedeco.

the class BlobDemo method ShowImage.

public static void ShowImage(IplImage image, String caption, int width, int height) {
    // gamma=1
    CanvasFrame canvas = new CanvasFrame(caption, 1);
    canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    canvas.setCanvasSize(width, height);
    OpenCVFrameConverter converter = new OpenCVFrameConverter.ToIplImage();
    canvas.showImage(converter.convert(image));
}
Also used : OpenCVFrameConverter(org.bytedeco.javacv.OpenCVFrameConverter) CanvasFrame(org.bytedeco.javacv.CanvasFrame)

Aggregations

CanvasFrame (org.bytedeco.javacv.CanvasFrame)2 OpenCVFrameConverter (org.bytedeco.javacv.OpenCVFrameConverter)2 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 Java2DFrameConverter (org.bytedeco.javacv.Java2DFrameConverter)1