Search in sources :

Example 16 with ImageBase

use of boofcv.struct.image.ImageBase in project BoofCV by lessthanoptimal.

the class TestGPixelMath method createInputParam.

@Override
protected Object[][] createInputParam(Method candidate, Method validation) {
    Class<?>[] param = validation.getParameterTypes();
    String name = candidate.getName();
    ImageBase inputA = createImage(param[0], null);
    ImageBase inputB = null, output = null;
    Object[][] ret = new Object[1][param.length];
    if (name.equals("abs")) {
        output = createImage(param[1], null);
        ret[0][0] = inputA;
        ret[0][1] = output;
    } else if (name.equals("invert")) {
        output = createImage(param[1], null);
        ret[0][0] = inputA;
        ret[0][1] = output;
    } else if (name.equals("divide") && param.length == 3) {
        output = createImage(param[param.length - 1], null);
        if (ImageBase.class.isAssignableFrom(param[1])) {
            ret[0][0] = inputA;
            ret[0][1] = inputB = createImage(param[1], null);
            ret[0][2] = output;
        } else {
            ret[0][0] = inputA;
            ret[0][1] = 3;
            ret[0][2] = output;
        }
    } else if (name.equals("divide") && param.length == 5) {
        output = createImage(param[param.length - 1], null);
        ret[0][0] = inputA;
        ret[0][1] = 3;
        ret[0][2] = -1;
        ret[0][3] = 5;
        ret[0][4] = output;
    } else if (name.equals("multiply") && param.length == 3) {
        output = createImage(param[param.length - 1], null);
        if (ImageBase.class.isAssignableFrom(param[1])) {
            ret[0][0] = inputA;
            ret[0][1] = inputB = createImage(param[1], null);
            ret[0][2] = output;
        } else {
            ret[0][0] = inputA;
            ret[0][1] = 3;
            ret[0][2] = output;
        }
    } else if (name.equals("multiply") && param.length == 5) {
        output = createImage(param[param.length - 1], null);
        ret[0][0] = inputA;
        ret[0][1] = 3;
        ret[0][2] = -20;
        ret[0][3] = 12;
        ret[0][4] = output;
    } else if (name.equals("plus") && param.length == 3) {
        output = createImage(param[param.length - 1], null);
        ret[0][0] = inputA;
        ret[0][1] = 3;
        ret[0][2] = output;
    } else if (name.equals("plus") && param.length == 5) {
        output = createImage(param[param.length - 1], null);
        ret[0][0] = inputA;
        ret[0][1] = 3;
        ret[0][2] = -10;
        ret[0][3] = 12;
        ret[0][4] = output;
    } else if (name.equals("minus") && param.length == 3) {
        output = createImage(param[param.length - 1], null);
        boolean first = ImageBase.class.isAssignableFrom(param[0]);
        if (inputA == null)
            inputA = createImage(param[1], null);
        ret[0][0] = first ? inputA : 3;
        ret[0][1] = first ? 3 : inputA;
        ret[0][2] = output;
    } else if (name.equals("minus") && param.length == 5) {
        output = createImage(param[param.length - 1], null);
        boolean first = ImageBase.class.isAssignableFrom(param[0]);
        if (inputA == null)
            inputA = createImage(param[1], null);
        ret[0][0] = first ? inputA : 3;
        ret[0][1] = first ? 3 : inputA;
        ret[0][2] = -10;
        ret[0][3] = 12;
        ret[0][4] = output;
    } else if (name.equals("log")) {
        inputB = createImage(param[1], null);
        ret[0][0] = inputA;
        ret[0][1] = inputB;
    } else if (name.equals("pow2")) {
        inputB = createImage(param[1], null);
        ret[0][0] = inputA;
        ret[0][1] = inputB;
    } else if (name.equals("sqrt")) {
        inputB = createImage(param[1], null);
        ret[0][0] = inputA;
        ret[0][1] = inputB;
    } else if (name.equals("add")) {
        inputB = createImage(param[1], null);
        output = createImage(param[2], null);
        ret[0][0] = inputA;
        ret[0][1] = inputB;
        ret[0][2] = output;
    } else if (name.equals("subtract")) {
        inputB = createImage(param[1], null);
        output = createImage(param[2], null);
        ret[0][0] = inputA;
        ret[0][1] = inputB;
        ret[0][2] = output;
    } else if (name.equals("boundImage")) {
        ret[0][0] = inputA;
        ret[0][1] = 2;
        ret[0][2] = 8;
    } else if (name.equals("diffAbs")) {
        inputB = createImage(param[1], null);
        output = createImage(param[2], null);
        ret[0][0] = inputA;
        ret[0][1] = inputB;
        ret[0][2] = output;
    } else if (name.equals("averageBand")) {
        inputA = createImage(param[0], param[1]);
        output = createImage(param[1], null);
        ret[0][0] = inputA;
        ret[0][1] = output;
    }
    fillRandom(inputA);
    fillRandom(inputB);
    fillRandom(output);
    return ret;
}
Also used : ImageBase(boofcv.struct.image.ImageBase)

Example 17 with ImageBase

use of boofcv.struct.image.ImageBase in project BoofCV by lessthanoptimal.

the class TestGPixelMath method compareResults.

@Override
protected void compareResults(Object targetResult, Object[] targetParam, Object validationResult, Object[] validationParam) {
    int which;
    if (targetParam[targetParam.length - 1] instanceof ImageBase) {
        which = targetParam.length - 1;
    } else {
        which = 0;
    }
    ImageBase t = (ImageBase) targetParam[which];
    ImageBase v = (ImageBase) validationParam[which];
    // if it is full of zeros something went wrong
    boolean foundNotZero = false;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (GeneralizedImageOps.get(t, j, i, 0) != 0) {
                foundNotZero = true;
                break;
            }
        }
    }
    assertTrue(foundNotZero);
    BoofTesting.assertEquals(t, v, 0);
}
Also used : ImageBase(boofcv.struct.image.ImageBase)

Example 18 with ImageBase

use of boofcv.struct.image.ImageBase in project BoofCV by lessthanoptimal.

the class TestGPixelMath method all_planar_images.

/**
 * Tests all functions with inputs from planar images
 */
@Test
public void all_planar_images() {
    int total = 0;
    Method[] methods = GPixelMath.class.getMethods();
    for (Method m : methods) {
        if (!Modifier.isStatic(m.getModifiers()))
            continue;
        Class[] param = m.getParameterTypes();
        if (param.length < 1)
            continue;
        // create input arguments
        Object[] inputs = new Object[param.length];
        for (int i = 0; i < inputs.length; i++) {
            if (param[i] == ImageBase.class) {
                inputs[i] = new Planar(GrayF32.class, width, height, 2);
                GImageMiscOps.fillUniform((ImageBase) inputs[i], rand, -100, 100);
            }
        }
        // specialized inputs for individual functions
        String name = m.getName();
        if (name.equals("divide") && param.length == 3) {
            if (!ImageBase.class.isAssignableFrom(param[1])) {
                inputs[1] = 3;
            }
        } else if (name.equals("divide") && param.length == 5) {
            inputs[1] = 3;
            inputs[2] = -1;
            inputs[3] = 5;
        } else if (name.equals("multiply") && param.length == 3) {
            if (!ImageBase.class.isAssignableFrom(param[1])) {
                inputs[1] = 3;
            }
        } else if (name.equals("multiply") && param.length == 5) {
            inputs[1] = 3;
            inputs[2] = -20;
            inputs[3] = 12;
        } else if (name.equals("plus") && param.length == 3) {
            inputs[1] = 3;
        } else if (name.equals("plus") && param.length == 5) {
            inputs[1] = 3;
            inputs[2] = -10;
            inputs[3] = 12;
        } else if (name.equals("minus") && param.length == 3) {
            boolean first = ImageBase.class.isAssignableFrom(param[0]);
            inputs[first ? 1 : 0] = 3;
        } else if (name.equals("minus") && param.length == 5) {
            boolean first = ImageBase.class.isAssignableFrom(param[0]);
            inputs[first ? 1 : 0] = 3;
            inputs[2] = -10;
            inputs[3] = 12;
        } else if (name.equals("boundImage")) {
            inputs[1] = 2;
            inputs[2] = 8;
        } else if (name.equals("averageBand")) {
            continue;
        }
        try {
            // create the expected results
            Object[] inputsByBand = copy(inputs);
            invokeByBand(m, inputsByBand);
            // invoke this function
            m.invoke(null, inputs);
            // compare against each other
            for (int i = 0; i < inputs.length; i++) {
                if (Planar.class == inputs[i].getClass()) {
                    BoofTesting.assertEquals((ImageBase) inputs[i], (ImageBase) inputsByBand[i], 1e-4);
                }
            }
            total++;
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    assertEquals(21, total);
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) GrayF32(boofcv.struct.image.GrayF32) Planar(boofcv.struct.image.Planar) ImageBase(boofcv.struct.image.ImageBase) Test(org.junit.Test)

Example 19 with ImageBase

use of boofcv.struct.image.ImageBase in project BoofCV by lessthanoptimal.

the class ExampleBackgroundRemovalMoving method main.

public static void main(String[] args) {
    // Example with a moving camera.  Highlights why motion estimation is sometimes required
    String fileName = UtilIO.pathExample("tracking/chipmunk.mjpeg");
    // Camera has a bit of jitter in it.  Static kinda works but motion reduces false positives
    // String fileName = UtilIO.pathExample("background/horse_jitter.mp4");
    // 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);
    // Configure the feature detector
    ConfigGeneralDetector confDetector = new ConfigGeneralDetector();
    confDetector.threshold = 10;
    confDetector.maxFeatures = 300;
    confDetector.radius = 6;
    // Use a KLT tracker
    PointTracker tracker = FactoryPointTracker.klt(new int[] { 1, 2, 4, 8 }, confDetector, 3, GrayF32.class, null);
    // This estimates the 2D image motion
    ImageMotion2D<GrayF32, Homography2D_F64> motion2D = FactoryMotion2D.createMotion2D(500, 0.5, 3, 100, 0.6, 0.5, false, tracker, new Homography2D_F64());
    ConfigBackgroundBasic configBasic = new ConfigBackgroundBasic(30, 0.005f);
    // 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(12, 0.001f);
    configGaussian.initialVariance = 64;
    configGaussian.minimumDifference = 5;
    // Note that GMM doesn't interpolate the input image. Making it harder to model object edges.
    // However it runs faster because of this.
    ConfigBackgroundGmm configGmm = new ConfigBackgroundGmm();
    configGmm.initialVariance = 1600;
    configGmm.significantWeight = 1e-1f;
    // Comment/Uncomment to switch background mode
    BackgroundModelMoving background = FactoryBackgroundModel.movingBasic(configBasic, new PointTransformHomography_F32(), imageType);
    // FactoryBackgroundModel.movingGaussian(configGaussian, new PointTransformHomography_F32(), imageType);
    // FactoryBackgroundModel.movingGmm(configGmm,new PointTransformHomography_F32(), imageType);
    background.setUnknownValue(1);
    MediaManager media = DefaultMediaManager.INSTANCE;
    SimpleImageSequence video = media.openVideo(fileName, background.getImageType());
    // media.openCamera(null,640,480,background.getImageType());
    // ====== Initialize Images
    // storage for segmented image.  Background = 0, Foreground = 1
    GrayU8 segmented = new GrayU8(video.getNextWidth(), video.getNextHeight());
    // Grey scale image that's the input for motion estimation
    GrayF32 grey = new GrayF32(segmented.width, segmented.height);
    // coordinate frames
    Homography2D_F32 firstToCurrent32 = new Homography2D_F32();
    Homography2D_F32 homeToWorld = new Homography2D_F32();
    homeToWorld.a13 = grey.width / 2;
    homeToWorld.a23 = grey.height / 2;
    // Create a background image twice the size of the input image.  Tell it that the home is in the center
    background.initialize(grey.width * 2, grey.height * 2, homeToWorld);
    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, "Detections", true);
    double fps = 0;
    // smoothing factor for FPS
    double alpha = 0.01;
    while (video.hasNext()) {
        ImageBase input = video.next();
        long before = System.nanoTime();
        GConvertImage.convert(input, grey);
        if (!motion2D.process(grey)) {
            throw new RuntimeException("Should handle this scenario");
        }
        Homography2D_F64 firstToCurrent64 = motion2D.getFirstToCurrent();
        ConvertMatrixData.convert(firstToCurrent64, firstToCurrent32);
        background.segment(firstToCurrent32, input, segmented);
        background.updateBackground(firstToCurrent32, input);
        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) {
        }
    }
}
Also used : ConfigBackgroundBasic(boofcv.factory.background.ConfigBackgroundBasic) BackgroundModelMoving(boofcv.alg.background.BackgroundModelMoving) SimpleImageSequence(boofcv.io.image.SimpleImageSequence) PointTransformHomography_F32(boofcv.alg.distort.PointTransformHomography_F32) ConfigGeneralDetector(boofcv.abst.feature.detect.interest.ConfigGeneralDetector) Homography2D_F32(georegression.struct.homography.Homography2D_F32) Homography2D_F64(georegression.struct.homography.Homography2D_F64) BufferedImage(java.awt.image.BufferedImage) ImageType(boofcv.struct.image.ImageType) ConfigBackgroundGaussian(boofcv.factory.background.ConfigBackgroundGaussian) GrayF32(boofcv.struct.image.GrayF32) MediaManager(boofcv.io.MediaManager) DefaultMediaManager(boofcv.io.wrapper.DefaultMediaManager) ConfigBackgroundGmm(boofcv.factory.background.ConfigBackgroundGmm) GrayU8(boofcv.struct.image.GrayU8) ImageGridPanel(boofcv.gui.image.ImageGridPanel) PointTracker(boofcv.abst.feature.tracker.PointTracker) FactoryPointTracker(boofcv.factory.feature.tracker.FactoryPointTracker) ImageBase(boofcv.struct.image.ImageBase)

Example 20 with ImageBase

use of boofcv.struct.image.ImageBase 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)

Aggregations

ImageBase (boofcv.struct.image.ImageBase)54 ImageType (boofcv.struct.image.ImageType)14 Test (org.junit.Test)13 Se3_F64 (georegression.struct.se.Se3_F64)5 BufferedImage (java.awt.image.BufferedImage)5 SimpleImageSequence (boofcv.io.image.SimpleImageSequence)4 GrayU8 (boofcv.struct.image.GrayU8)4 MediaManager (boofcv.io.MediaManager)3 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)3 DefaultMediaManager (boofcv.io.wrapper.DefaultMediaManager)3 KernelBase (boofcv.struct.convolve.KernelBase)3 Point2D_F64 (georegression.struct.point.Point2D_F64)3 ConfigBackgroundBasic (boofcv.factory.background.ConfigBackgroundBasic)2 ConfigBackgroundGmm (boofcv.factory.background.ConfigBackgroundGmm)2 ImageGridPanel (boofcv.gui.image.ImageGridPanel)2 ImagePanel (boofcv.gui.image.ImagePanel)2 GrayF32 (boofcv.struct.image.GrayF32)2 Planar (boofcv.struct.image.Planar)2 File (java.io.File)2 FDistort (boofcv.abst.distort.FDistort)1