Search in sources :

Example 1 with CanvasFrame

use of org.bytedeco.javacv.CanvasFrame 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 CanvasFrame

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

the class RealSense2DepthMeasuring method main.

public static void main(String[] args) throws FrameGrabber.Exception {
    final RealSense2FrameGrabber rs2 = new RealSense2FrameGrabber();
    // list all cameras
    for (RealSense2FrameGrabber.RealSense2DeviceInfo info : rs2.getDeviceInfos()) {
        System.out.printf("Device: %s %s %s Locked: %b\n", info.getName(), info.getFirmware(), info.getSerialNumber(), info.isLocked());
    }
    // enable the color & depth stream of the realsense camera
    rs2.enableColorStream(640, 480, 30);
    rs2.enableDepthStream(640, 480, 30);
    // here are more examples of streams:
    /*
        rs2.enableColorStream(640, 480, 30); // color stream
        rs2.enableIRStream(640, 480, 90); // ir stream
        rs2.enableStream(new RealSense2FrameGrabber.RealSenseStream(
                RS2_STREAM_INFRARED,
                2,
                new Size(640, 480),
                30,
                RS2_FORMAT_Y8
        )); // second ir stream
        */
    // start realsense camera
    rs2.start();
    // start frame to view the stream
    CanvasFrame canvasFrame = new CanvasFrame("RealSense");
    canvasFrame.setCanvasSize(rs2.getImageWidth(), rs2.getImageHeight());
    // add mouse listener to see the depth at the clicked point
    canvasFrame.getCanvas().addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            try {
                System.out.println("Depth: " + rs2.getDistance(e.getX(), e.getY()));
            } catch (FrameGrabber.Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    // run canvas
    while (canvasFrame.isVisible()) {
        // trigger camera to capture images
        rs2.trigger();
        // display images -> grab will return the first stream added
        // use rs2.grabDepth(), rs2.grabColor() and rs2.grabIR() for the other streams
        Frame frame = rs2.grab();
        if (frame == null) {
            System.err.println("Frame is null!");
            break;
        }
        // display frame
        canvasFrame.showImage(frame);
    }
    // close realsense camera
    rs2.stop();
    rs2.release();
    canvasFrame.dispose();
}
Also used : RealSense2FrameGrabber(org.bytedeco.javacv.RealSense2FrameGrabber) CanvasFrame(org.bytedeco.javacv.CanvasFrame) Frame(org.bytedeco.javacv.Frame) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) CanvasFrame(org.bytedeco.javacv.CanvasFrame)

Example 3 with CanvasFrame

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

the class ImageSegmentation method imshow.

// I wrote a custom imshow method for problems using the OpenCV original one
private static void imshow(String txt, Mat img) {
    CanvasFrame canvasFrame = new CanvasFrame(txt);
    canvasFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    canvasFrame.setCanvasSize(img.cols(), img.rows());
    canvasFrame.showImage(new OpenCVFrameConverter.ToMat().convert(img));
}
Also used : CanvasFrame(org.bytedeco.javacv.CanvasFrame)

Example 4 with CanvasFrame

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

the class BioInspiredRetina method showImage.

private void showImage(Mat matrix) {
    CanvasFrame canvasFrame = new CanvasFrame("Retina demonstration", 1);
    canvasFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    canvasFrame.setCanvasSize(640, 480);
    Canvas canvas = canvasFrame.getCanvas();
    canvasFrame.getContentPane().removeAll();
    ScrollPane scrollPane = new ScrollPane();
    scrollPane.add(canvas);
    canvasFrame.add(scrollPane);
    canvasFrame.showImage(new OpenCVFrameConverter.ToMat().convert(matrix));
}
Also used : CanvasFrame(org.bytedeco.javacv.CanvasFrame)

Example 5 with CanvasFrame

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

the class DeepLearningFaceDetection method main.

public static void main(String[] args) {
    VideoCapture capture = new VideoCapture();
    capture.set(CAP_PROP_FRAME_WIDTH, 1280);
    capture.set(CAP_PROP_FRAME_HEIGHT, 720);
    if (!capture.open(0)) {
        System.out.println("Can not open the cam !!!");
    }
    Mat colorimg = new Mat();
    CanvasFrame mainframe = new CanvasFrame("Face Detection", CanvasFrame.getDefaultGamma() / 2.2);
    mainframe.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    mainframe.setCanvasSize(600, 600);
    mainframe.setLocationRelativeTo(null);
    mainframe.setVisible(true);
    while (true) {
        while (capture.read(colorimg) && mainframe.isVisible()) {
            detectAndDraw(colorimg);
            mainframe.showImage(converter.convert(colorimg));
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
}
Also used : CanvasFrame(org.bytedeco.javacv.CanvasFrame)

Aggregations

CanvasFrame (org.bytedeco.javacv.CanvasFrame)8 Frame (org.bytedeco.javacv.Frame)2 OpenCVFrameConverter (org.bytedeco.javacv.OpenCVFrameConverter)2 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 ShortBuffer (java.nio.ShortBuffer)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 AudioFormat (javax.sound.sampled.AudioFormat)1 DataLine (javax.sound.sampled.DataLine)1 LineUnavailableException (javax.sound.sampled.LineUnavailableException)1 Mixer (javax.sound.sampled.Mixer)1 TargetDataLine (javax.sound.sampled.TargetDataLine)1 FFmpegFrameRecorder (org.bytedeco.javacv.FFmpegFrameRecorder)1 Exception (org.bytedeco.javacv.FrameRecorder.Exception)1 Java2DFrameConverter (org.bytedeco.javacv.Java2DFrameConverter)1 OpenCVFrameGrabber (org.bytedeco.javacv.OpenCVFrameGrabber)1 RealSense2FrameGrabber (org.bytedeco.javacv.RealSense2FrameGrabber)1