Search in sources :

Example 1 with Webcam

use of com.github.sarxos.webcam.Webcam in project BoofCV by lessthanoptimal.

the class CameraCalibration method handleWebcam.

/**
 * Captures calibration data live using a webcam and a GUI to assist the user
 */
public void handleWebcam() {
    final Webcam webcam = openSelectedCamera();
    if (desiredWidth > 0 && desiredHeight > 0)
        UtilWebcamCapture.adjustResolution(webcam, desiredWidth, desiredHeight);
    webcam.open();
    // close the webcam gracefully on exit
    Runtime.getRuntime().addShutdownHook(new Thread() {

        public void run() {
            if (webcam.isOpen()) {
                System.out.println("Closing webcam");
                webcam.close();
            }
        }
    });
    ComputeGeometryScore quality = new ComputeGeometryScore(zeroSkew, detector.getLayout());
    AssistedCalibrationGui gui = new AssistedCalibrationGui(webcam.getViewSize());
    JFrame frame = ShowImages.showWindow(gui, "Webcam Calibration", true);
    GrayF32 gray = new GrayF32(webcam.getViewSize().width, webcam.getViewSize().height);
    if (desiredWidth > 0 && desiredHeight > 0) {
        if (gray.width != desiredWidth || gray.height != desiredHeight)
            System.err.println("Actual camera resolution does not match desired.  Actual: " + gray.width + " " + gray.height + "  Desired: " + desiredWidth + " " + desiredHeight);
    }
    AssistedCalibration assisted = new AssistedCalibration(detector, quality, gui, OUTPUT_DIRECTORY, IMAGE_DIRECTORY);
    assisted.init(gray.width, gray.height);
    BufferedImage image;
    while ((image = webcam.getImage()) != null && !assisted.isFinished()) {
        ConvertBufferedImage.convertFrom(image, gray);
        try {
            assisted.process(gray, image);
        } catch (RuntimeException e) {
            System.err.println("BUG!!! saving image to crash_image.png");
            UtilImageIO.saveImage(image, "crash_image.png");
            throw e;
        }
    }
    webcam.close();
    if (assisted.isFinished()) {
        frame.setVisible(false);
        inputDirectory = new File(OUTPUT_DIRECTORY, IMAGE_DIRECTORY).getPath();
        outputFileName = new File(OUTPUT_DIRECTORY, "intrinsic.yaml").getPath();
        handleDirectory();
    }
}
Also used : AssistedCalibrationGui(boofcv.app.calib.AssistedCalibrationGui) GrayF32(boofcv.struct.image.GrayF32) AssistedCalibration(boofcv.app.calib.AssistedCalibration) Webcam(com.github.sarxos.webcam.Webcam) ComputeGeometryScore(boofcv.app.calib.ComputeGeometryScore) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) ProgressMonitorThread(boofcv.io.ProgressMonitorThread)

Example 2 with Webcam

use of com.github.sarxos.webcam.Webcam in project narchy by automenta.

the class RasterHierarchy method process.

/**
 * Invoke to start the main processing loop.
 */
public void process() {
    Webcam webcam = UtilWebcamCapture.openDefault(frameWidth, frameHeight);
    // adjust the window size and let the GUI know it has changed
    Dimension actualSize = webcam.getViewSize();
    setPreferredSize(actualSize);
    setMinimumSize(actualSize);
    window.setMinimumSize(actualSize);
    window.setPreferredSize(actualSize);
    window.setVisible(true);
    BufferedImage input;
    while (running) {
        /*
                 * Uncomment this section to scan the focal point across the frame
                 * automatically - just for demo purposes.
                 */
        /*
                int xx = this.focusPoint.getX();
                int yy = this.focusPoint.getY();
                xx += 1;

                if(xx > frameWidth)
                {
                    xx = 0;
                    yy += 1;
                    if (yy > frameHeight)
                        yy = 0;
                }

                this.setFocus(xx, yy);
                */
        input = webcam.getImage();
        // synchronized (workImage) {
        // copy the latest image into the work buffer
        // Graphics2D g2 = workImage.createGraphics();
        buffered = this.rasterizeImage(input);
        // }
        repaint();
    }
}
Also used : Webcam(com.github.sarxos.webcam.Webcam) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.core.image.ConvertBufferedImage)

Example 3 with Webcam

use of com.github.sarxos.webcam.Webcam in project narchy by automenta.

the class WebcamTrack method main.

public static void main(String[] args) {
    // tune the tracker for the image size and visual appearance
    ConfigGeneralDetector configDetector = new ConfigGeneralDetector(-1, 8, 1);
    PkltConfig configKlt = new PkltConfig(3, new int[] { 1, 2, 4, 8 });
    PointTracker<ImageFloat32> tracker = FactoryPointTracker.klt(configKlt, configDetector, ImageFloat32.class, null);
    // Open a webcam at a resolution close to 640x480
    Webcam webcam = UtilWebcamCapture.openDefault(640, 480);
    // Create the panel used to display the image and
    ImagePanel gui = new ImagePanel();
    gui.setPreferredSize(webcam.getViewSize());
    ShowImages.showWindow(gui, "KLT Tracker");
    int minimumTracks = 100;
    while (true) {
        BufferedImage image = webcam.getImage();
        ImageFloat32 gray = ConvertBufferedImage.convertFrom(image, (ImageFloat32) null);
        tracker.process(gray);
        List<PointTrack> tracks = tracker.getActiveTracks(null);
        // Spawn tracks if there are too few
        if (tracks.size() < minimumTracks) {
            tracker.spawnTracks();
            tracks = tracker.getActiveTracks(null);
            minimumTracks = tracks.size() / 2;
        }
        // Draw the tracks
        Graphics2D g2 = image.createGraphics();
        for (PointTrack t : tracks) {
            VisualizeFeatures.drawPoint(g2, (int) t.x, (int) t.y, Color.RED);
        }
        gui.setBufferedImageSafe(image);
    }
}
Also used : PointTrack(boofcv.abst.feature.tracker.PointTrack) ImageFloat32(boofcv.struct.image.ImageFloat32) PkltConfig(boofcv.alg.tracker.klt.PkltConfig) ConfigGeneralDetector(boofcv.abst.feature.detect.interest.ConfigGeneralDetector) Webcam(com.github.sarxos.webcam.Webcam) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.core.image.ConvertBufferedImage) ImagePanel(boofcv.gui.image.ImagePanel)

Example 4 with Webcam

use of com.github.sarxos.webcam.Webcam in project BoofCV by lessthanoptimal.

the class ExampleTrackingKlt method main.

public static void main(String[] args) {
    // tune the tracker for the image size and visual appearance
    ConfigPointDetector configDetector = new ConfigPointDetector();
    configDetector.type = PointDetectorTypes.SHI_TOMASI;
    configDetector.general.radius = 8;
    configDetector.general.threshold = 1;
    ConfigPKlt configKlt = new ConfigPKlt(3);
    PointTracker<GrayF32> tracker = FactoryPointTracker.klt(configKlt, configDetector, GrayF32.class, null);
    // Open a webcam at a resolution close to 640x480
    Webcam webcam = UtilWebcamCapture.openDefault(640, 480);
    // Create the panel used to display the image and feature tracks
    ImagePanel gui = new ImagePanel();
    gui.setPreferredSize(webcam.getViewSize());
    ShowImages.showWindow(gui, "KLT Tracker", true);
    int minimumTracks = 100;
    while (true) {
        BufferedImage image = webcam.getImage();
        GrayF32 gray = ConvertBufferedImage.convertFrom(image, (GrayF32) null);
        tracker.process(gray);
        List<PointTrack> tracks = tracker.getActiveTracks(null);
        // Spawn tracks if there are too few
        if (tracks.size() < minimumTracks) {
            tracker.spawnTracks();
            tracks = tracker.getActiveTracks(null);
            minimumTracks = tracks.size() / 2;
        }
        // Draw the tracks
        Graphics2D g2 = image.createGraphics();
        for (PointTrack t : tracks) {
            VisualizeFeatures.drawPoint(g2, (int) t.pixel.x, (int) t.pixel.y, Color.RED);
        }
        gui.setImageUI(image);
    }
}
Also used : GrayF32(boofcv.struct.image.GrayF32) PointTrack(boofcv.abst.tracker.PointTrack) Webcam(com.github.sarxos.webcam.Webcam) ConfigPKlt(boofcv.alg.tracker.klt.ConfigPKlt) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) ConfigPointDetector(boofcv.abst.feature.detect.interest.ConfigPointDetector) ImagePanel(boofcv.gui.image.ImagePanel)

Example 5 with Webcam

use of com.github.sarxos.webcam.Webcam in project BoofCV by lessthanoptimal.

the class ExampleWebcamObjectTracking method process.

/**
 * Invoke to start the main processing loop.
 */
public void process() {
    Webcam webcam = UtilWebcamCapture.openDefault(desiredWidth, desiredHeight);
    // adjust the window size and let the GUI know it has changed
    Dimension actualSize = webcam.getViewSize();
    setPreferredSize(actualSize);
    setMinimumSize(actualSize);
    window.setMinimumSize(actualSize);
    window.setPreferredSize(actualSize);
    window.setVisible(true);
    // create
    T input = tracker.getImageType().createImage(actualSize.width, actualSize.height);
    workImage = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_RGB);
    while (true) {
        BufferedImage buffered = webcam.getImage();
        if (buffered == null)
            break;
        ConvertBufferedImage.convertFrom(webcam.getImage(), input, true);
        // mode is read/written to by the GUI also
        int mode = this.mode;
        boolean success = false;
        if (mode == 2) {
            Rectangle2D_F64 rect = new Rectangle2D_F64();
            rect.setTo(point0.x, point0.y, point1.x, point1.y);
            UtilPolygons2D_F64.convert(rect, target);
            success = tracker.initialize(input, target);
            this.mode = success ? 3 : 0;
        } else if (mode == 3) {
            success = tracker.process(input, target);
        }
        synchronized (lockGUI) {
            // copy the latest image into the work buffered
            Graphics2D g2 = workImage.createGraphics();
            g2.drawImage(buffered, 0, 0, null);
            // visualize the current results
            if (mode == 1) {
                drawSelected(g2);
            } else if (mode == 3) {
                if (success) {
                    drawTrack(g2);
                }
            }
        }
        repaint();
    }
}
Also used : Rectangle2D_F64(georegression.struct.shapes.Rectangle2D_F64) Webcam(com.github.sarxos.webcam.Webcam) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Aggregations

Webcam (com.github.sarxos.webcam.Webcam)13 BufferedImage (java.awt.image.BufferedImage)9 ImagePanel (boofcv.gui.image.ImagePanel)4 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)4 ConvertBufferedImage (boofcv.core.image.ConvertBufferedImage)3 GrayF32 (boofcv.struct.image.GrayF32)3 Rectangle2D_F64 (georegression.struct.shapes.Rectangle2D_F64)2 ConfigGeneralDetector (boofcv.abst.feature.detect.interest.ConfigGeneralDetector)1 ConfigPointDetector (boofcv.abst.feature.detect.interest.ConfigPointDetector)1 PointTrack (boofcv.abst.feature.tracker.PointTrack)1 PointTrack (boofcv.abst.tracker.PointTrack)1 ConfigPKlt (boofcv.alg.tracker.klt.ConfigPKlt)1 PkltConfig (boofcv.alg.tracker.klt.PkltConfig)1 AssistedCalibration (boofcv.app.calib.AssistedCalibration)1 AssistedCalibrationGui (boofcv.app.calib.AssistedCalibrationGui)1 ComputeGeometryScore (boofcv.app.calib.ComputeGeometryScore)1 ProgressMonitorThread (boofcv.io.ProgressMonitorThread)1 ImageFloat32 (boofcv.struct.image.ImageFloat32)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1