Este exemplo pega uma cena de partida de Robocup e segmenta seus elementos. Um passo futuro seria identificar cada robô pelo seu padrão de cores único e a bolina por seu tamanho e cor. Analizando sequencias de frames da partida é possível acompanhar cada robô e a bolinha, assim podendo desenvolver uma estratégia para o time.

Algorítmo de Segmentação
    1. Carrega a imagem de input.
    2. Transforma pixels verdes em pixels brancos.
    3. Aplica Threshold de intensidade para separar foreground de backgound.
    4. Aplica morphological closing para agrupar partes separadas de um mesmo objeto.
    5. Usa FloodFill segmentation para separar os elementos.
    6. Desenha os segmentos na imagem original.


Publicado originalmente no Stack Overflow:
"How to perform the image segmentation in java?"

Input Image:


Result:
Source:
import static marvin.MarvinPluginCollection.*;

public class SimpleSegmentation {
    public SimpleSegmentation(){
        // 1. Load image
        MarvinImage original = MarvinImageIO.loadImage("./res/robocup.jpg");
        MarvinImage image = original.clone();
        // 2. Change green pixels to white
        filterGreen(image);
        // 3. Use threshold to separate foreground and background.
        MarvinImage bin = MarvinColorModelConverter.rgbToBinary(image, 127);
        // 4. Morphological closing to group separated parts of the same object
        morphologicalClosing(bin.clone(), bin, MarvinMath.getTrueMatrix(30, 30));
        // 5. Use Floodfill segmention to get image segments
        image = MarvinColorModelConverter.binaryToRgb(bin);
        MarvinSegment[] segments = floodfillSegmentation(image);
        // 6. Show the segments in the original image
        for(int i=1; i<segments.length; i++){
            MarvinSegment seg = segments[i];
            original.drawRect(seg.x1, seg.y1, seg.width, seg.height, Color.yellow);
            original.drawRect(seg.x1+1, seg.y1+1, seg.width, seg.height, Color.yellow);
        }
        MarvinImageIO.saveImage(original, "./res/robocup_segmented.png");
    }
    private void filterGreen(MarvinImage image){
        int r,g,b;
        for(int y=0; y<image.getHeight(); y++){
            for(int x=0; x<image.getWidth(); x++){
                r = image.getIntComponent0(x, y);
                g = image.getIntComponent1(x, y);
                b = image.getIntComponent2(x, y);
                if(g > r*1.5 && g > b*1.5){
                    image.setIntColor(x, y, 255,255,255);
        }}}
    }
    public static void main(String[] args) { new SimpleSegmentation();  }
}