Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to perform the image segmentation in java?

hi guys i am an infant for image processing technique in java , i have decided to develop one project in image processing so i need what are the algorithms are followed and also which one is easier to develop please some one guide me it may be great for me.....and also which technology is best for image processing java or Matlab? guide me...

like image 765
ilan Avatar asked Nov 21 '25 03:11

ilan


1 Answers

I think the best image processing tool for you depends on the kind of project you're working on.

If you're working on a research project that needs productivity, quick validation and writting reports, Matlab and similar tools are the best option. On the other hand, if you're developing a software product, Java, C++, C, Objective-C, etc is more indicated. Matlab solutions are not easy to deliver and maintain in production.

Since you asked how to do image segmentation in Java, I'll provide an example using Java and Marvin Image Processing Framework. As suggested by @Asif Sharif, FloodFill segmentation is a good strategy and I used it!

INPUT IMAGE: enter image description here

OUTPUT IMAGE: enter image description here

HOW IT WORKS:

  1. Load input image.
  2. Change green pixels to white pixels.
  3. Apply intensity thresholding for separating foreground from background.
  4. Apply morphological closing to group separated parts of the same object
  5. Use FloodFill segmentation to get the segments.
  6. Draw the segments coordinates in the original image.

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();  }
}
like image 135
Gabriel Ambrósio Archanjo Avatar answered Nov 23 '25 19:11

Gabriel Ambrósio Archanjo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!