Search in sources :

Example 1 with WatershedVincentSoille1991

use of boofcv.alg.segmentation.watershed.WatershedVincentSoille1991 in project BoofCV by lessthanoptimal.

the class VisualizeWatershedApp method main.

public static void main(String[] args) {
    BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("segment/berkeley_horses.jpg"));
    GrayU8 gray = new GrayU8(image.getWidth(), image.getHeight());
    ConvertBufferedImage.convertFrom(image, gray);
    WatershedVincentSoille1991 alg = FactorySegmentationAlg.watershed(ConnectRule.FOUR);
    alg.process(gray);
    GrayS32 pixelToRegion = alg.getOutput();
    VisualizeRegions.watersheds(pixelToRegion, image, 0);
    alg.removeWatersheds();
    int numRegions = alg.getTotalRegions();
    BufferedImage outRegions = VisualizeRegions.regions(pixelToRegion, numRegions, null);
    ShowImages.showWindow(image, "Watershed");
    ShowImages.showWindow(outRegions, "Regions");
}
Also used : WatershedVincentSoille1991(boofcv.alg.segmentation.watershed.WatershedVincentSoille1991) GrayU8(boofcv.struct.image.GrayU8) GrayS32(boofcv.struct.image.GrayS32) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Example 2 with WatershedVincentSoille1991

use of boofcv.alg.segmentation.watershed.WatershedVincentSoille1991 in project BoofCV by lessthanoptimal.

the class ExampleWatershedWithSeeds method main.

public static void main(String[] args) {
    BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("particles01.jpg"));
    GrayU8 input = ConvertBufferedImage.convertFromSingle(image, null, GrayU8.class);
    // declare working data
    GrayU8 binary = new GrayU8(input.width, input.height);
    GrayS32 label = new GrayS32(input.width, input.height);
    // Try using the mean pixel value to create a binary image then erode it to separate the particles from
    // each other
    double mean = ImageStatistics.mean(input);
    ThresholdImageOps.threshold(input, binary, (int) mean, true);
    GrayU8 filtered = BinaryImageOps.erode8(binary, 2, null);
    int numRegions = BinaryImageOps.contour(filtered, ConnectRule.EIGHT, label).size() + 1;
    // +1 to regions because contour only counts blobs and not the background
    // The labeled image can be used as is.  A precondition for seeded watershed is that all seeds have an
    // ID > 0.  Luckily, a value of 0 was used for background pixels in the contour algorithm.
    WatershedVincentSoille1991 watershed = FactorySegmentationAlg.watershed(ConnectRule.FOUR);
    watershed.process(input, label);
    GrayS32 output = watershed.getOutput();
    BufferedImage outLabeled = VisualizeBinaryData.renderLabeledBG(label, numRegions, null);
    VisualizeRegions.watersheds(output, image, 1);
    // Removing the watersheds and update the region count
    // NOTE: watershed.getTotalRegions() does not return correct results if seeds are used!
    watershed.removeWatersheds();
    numRegions -= 1;
    BufferedImage outRegions = VisualizeRegions.regions(output, numRegions, null);
    ListDisplayPanel gui = new ListDisplayPanel();
    gui.addImage(image, "Watersheds");
    gui.addImage(outRegions, "Regions");
    gui.addImage(outLabeled, "Seeds");
    ShowImages.showWindow(gui, "Watershed", true);
// Additional processing would be needed for this example to be really useful.
// The watersheds can be used to characterize the background while the seed binary image the particles
// From this the particles could be more accurately classified by assigning each pixel one of the two
// just mentioned groups based distance
}
Also used : ListDisplayPanel(boofcv.gui.ListDisplayPanel) WatershedVincentSoille1991(boofcv.alg.segmentation.watershed.WatershedVincentSoille1991) GrayU8(boofcv.struct.image.GrayU8) GrayS32(boofcv.struct.image.GrayS32) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Example 3 with WatershedVincentSoille1991

use of boofcv.alg.segmentation.watershed.WatershedVincentSoille1991 in project BoofCV by lessthanoptimal.

the class FactoryImageSegmentation method watershed.

/**
 * Creates an instance of {@link WatershedVincentSoille1991}.  Watershed works better when initial seeds
 * are provided.  In this adaptation of watershed to {@link boofcv.abst.segmentation.ImageSuperpixels} only the more basic algorithm
 * is used where each local minima is a region, which causes over segmentation.  Watershed also only can process
 * gray scale U8 images.  All other image types are converted into that format.
 *
 * @see WatershedVincentSoille1991
 *
 * @param config Configuration.  If null default is used.
 * @param <T> Image type
 * @return new instance of {@link ImageSuperpixels}
 */
public static <T extends ImageBase<T>> ImageSuperpixels<T> watershed(@Nullable ConfigWatershed config, ImageType<T> imageType) {
    if (config == null)
        config = new ConfigWatershed();
    WatershedVincentSoille1991 watershed = FactorySegmentationAlg.watershed(config.connectRule);
    Watershed_to_ImageSuperpixels ret = new Watershed_to_ImageSuperpixels<>(watershed, config.minimumRegionSize, config.connectRule);
    ret.setImageType(imageType);
    return ret;
}
Also used : WatershedVincentSoille1991(boofcv.alg.segmentation.watershed.WatershedVincentSoille1991)

Aggregations

WatershedVincentSoille1991 (boofcv.alg.segmentation.watershed.WatershedVincentSoille1991)3 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)2 GrayS32 (boofcv.struct.image.GrayS32)2 GrayU8 (boofcv.struct.image.GrayU8)2 BufferedImage (java.awt.image.BufferedImage)2 ListDisplayPanel (boofcv.gui.ListDisplayPanel)1