Search in sources :

Example 1 with BackgroundModelStationary

use of boofcv.alg.background.BackgroundModelStationary in project BoofCV by lessthanoptimal.

the class ExampleBackgroundRemovalStationary method main.

public static void main(String[] args) {
    String fileName = UtilIO.pathExample("background/street_intersection.mp4");
    // String fileName = UtilIO.pathExample("background/rubixfire.mp4"); // dynamic background
    // String fileName = UtilIO.pathExample("background/horse_jitter.mp4"); // degraded performance because of jitter
    // String fileName = UtilIO.pathExample("tracking/chipmunk.mjpeg"); // Camera moves.  Stationary will fail here
    // Comment/Uncomment to switch input image type
    ImageType imageType = ImageType.single(GrayF32.class);
    // ImageType imageType = ImageType.il(3, InterleavedF32.class);
    // ImageType imageType = ImageType.il(3, InterleavedU8.class);
    ConfigBackgroundGmm configGmm = new ConfigBackgroundGmm();
    // Comment/Uncomment to switch algorithms
    BackgroundModelStationary background = FactoryBackgroundModel.stationaryBasic(new ConfigBackgroundBasic(35, 0.005f), imageType);
    // FactoryBackgroundModel.stationaryGmm(configGmm, imageType);
    MediaManager media = DefaultMediaManager.INSTANCE;
    SimpleImageSequence video = media.openVideo(fileName, background.getImageType());
    // media.openCamera(null,640,480,background.getImageType());
    // Declare storage for segmented image.  1 = moving foreground and 0 = background
    GrayU8 segmented = new GrayU8(video.getNextWidth(), video.getNextHeight());
    BufferedImage visualized = new BufferedImage(segmented.width, segmented.height, BufferedImage.TYPE_INT_RGB);
    ImageGridPanel gui = new ImageGridPanel(1, 2);
    gui.setImages(visualized, visualized);
    ShowImages.showWindow(gui, "Static Scene: Background Segmentation", true);
    double fps = 0;
    // smoothing factor for FPS
    double alpha = 0.01;
    while (video.hasNext()) {
        ImageBase input = video.next();
        long before = System.nanoTime();
        background.updateBackground(input, segmented);
        long after = System.nanoTime();
        fps = (1.0 - alpha) * fps + alpha * (1.0 / ((after - before) / 1e9));
        VisualizeBinaryData.renderBinary(segmented, false, visualized);
        gui.setImage(0, 0, (BufferedImage) video.getGuiImage());
        gui.setImage(0, 1, visualized);
        gui.repaint();
        System.out.println("FPS = " + fps);
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
        }
    }
    System.out.println("done!");
}
Also used : ConfigBackgroundBasic(boofcv.factory.background.ConfigBackgroundBasic) SimpleImageSequence(boofcv.io.image.SimpleImageSequence) BufferedImage(java.awt.image.BufferedImage) ImageType(boofcv.struct.image.ImageType) BackgroundModelStationary(boofcv.alg.background.BackgroundModelStationary) MediaManager(boofcv.io.MediaManager) DefaultMediaManager(boofcv.io.wrapper.DefaultMediaManager) ConfigBackgroundGmm(boofcv.factory.background.ConfigBackgroundGmm) GrayU8(boofcv.struct.image.GrayU8) ImageGridPanel(boofcv.gui.image.ImageGridPanel) ImageBase(boofcv.struct.image.ImageBase)

Example 2 with BackgroundModelStationary

use of boofcv.alg.background.BackgroundModelStationary in project narchy by automenta.

the class ExampleBackgroundRemovalStationary method main.

public static void main(String[] args) {
    WebCam c = new WebCam();
    Tex output = new Tex();
    SpaceGraph.window(new Gridding(c.view(), output.view()), 800, 800);
    // String fileName = UtilIO.pathExample("background/horse_jitter.mp4"); // degraded performance because of jitter
    // String fileName = UtilIO.pathExample("tracking/chipmunk.mjpeg"); // Camera moves.  Stationary will fail here
    // Comment/Uncomment to switch input image type
    ImageType imageType = ImageType.single(GrayF32.class);
    // ImageType imageType = ImageType.il(3, InterleavedF32.class);
    // ImageType imageType = ImageType.il(3, InterleavedU8.class);
    // Configuration for Gaussian model.  Note that the threshold changes depending on the number of image bands
    // 12 = gray scale and 40 = color
    ConfigBackgroundGaussian configGaussian = new ConfigBackgroundGaussian(40, 0.0005f);
    configGaussian.initialVariance = 100;
    configGaussian.minimumDifference = 10f;
    // Comment/Uncomment to switch algorithms
    BackgroundModelStationary background = // FactoryBackgroundModel.stationaryBasic(new ConfigBackgroundBasic(35, 0.005f), imageType);
    FactoryBackgroundModel.stationaryGaussian(configGaussian, imageType);
    // Declare storage for segmented image.  1 = moving foreground and 0 = background
    GrayU8 segmented = new GrayU8(c.width, c.height);
    GrayF32 input = new GrayF32(c.width, c.height);
    BufferedImage segmentedVis = new BufferedImage(c.width, c.height, BufferedImage.TYPE_INT_RGB);
    new Loop(10f) {

        @Override
        public boolean next() {
            BufferedImage img = c.image;
            if (img != null) {
                ConvertBufferedImage.convertFrom(img, input, true);
                // long before = System.nanoTime();
                background.segment(input, segmented);
                background.updateBackground(input);
                byte[] b = segmented.data;
                for (int i = 0; i < b.length; i++) {
                    if (b[i] != 0)
                        b[i] = 127;
                }
                output.update(// segmented
                ConvertBufferedImage.convertTo(segmented, segmentedVis));
            }
            // }
            return true;
        }
    };
}
Also used : Loop(jcog.exe.Loop) Gridding(spacegraph.space2d.container.Gridding) BackgroundModelStationary(boofcv.alg.background.BackgroundModelStationary) GrayF32(boofcv.struct.image.GrayF32) Tex(spacegraph.video.Tex) WebCam(spacegraph.video.WebCam) GrayU8(boofcv.struct.image.GrayU8) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) ImageType(boofcv.struct.image.ImageType) ConfigBackgroundGaussian(boofcv.factory.background.ConfigBackgroundGaussian)

Aggregations

BackgroundModelStationary (boofcv.alg.background.BackgroundModelStationary)2 GrayU8 (boofcv.struct.image.GrayU8)2 ImageType (boofcv.struct.image.ImageType)2 BufferedImage (java.awt.image.BufferedImage)2 ConfigBackgroundBasic (boofcv.factory.background.ConfigBackgroundBasic)1 ConfigBackgroundGaussian (boofcv.factory.background.ConfigBackgroundGaussian)1 ConfigBackgroundGmm (boofcv.factory.background.ConfigBackgroundGmm)1 ImageGridPanel (boofcv.gui.image.ImageGridPanel)1 MediaManager (boofcv.io.MediaManager)1 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)1 SimpleImageSequence (boofcv.io.image.SimpleImageSequence)1 DefaultMediaManager (boofcv.io.wrapper.DefaultMediaManager)1 GrayF32 (boofcv.struct.image.GrayF32)1 ImageBase (boofcv.struct.image.ImageBase)1 Loop (jcog.exe.Loop)1 Gridding (spacegraph.space2d.container.Gridding)1 Tex (spacegraph.video.Tex)1 WebCam (spacegraph.video.WebCam)1