Removing background from image (MATLAB, Java, or Python preferred)

3.1k views Asked by At

What I would like to do is take an image of several plants and remove the background lines and noise from the image. The end result will then hopefully be able to be turned into a binary image where every 1 value is just part of the plant. Is there any way for me to do this automatically without having to use a manual threshold? One solution I've looked into is just estimating the value of what is not noise and then subtracting, but this results in removing parts of plants. An example image for input is: https://i.stack.imgur.com/jJVv7.png

2

There are 2 answers

5
lennon310 On BEST ANSWER
I=im2bw(I,graythresh(I));
imshow(I)

enter image description here

0
Gabriel Archanjo On

Segment each leaf is not an easy task since the boundaries are not that clear. The solution shown below segment a few leafs, but not all of them. It's a simple solution that may be a good start point.

The algorithm, implemented using Marvin Framework:

  1. Invert the image colors
  2. Binarize
  3. Morphological Erosion
  4. Morphological dilation

Below the original image, binary image and some leafs segmented.

enter image description here

Source code:

public class RemoveBackground {

    public RemoveBackground(){
        // 1. Load plug-ins
        MarvinImagePlugin erode = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.morphological.erosion");
        MarvinImagePlugin dilate = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.morphological.dilation");
        MarvinImagePlugin invert = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.invert");

        // 2. Set plug-ins attributes
        boolean[][] m = MarvinMath.getTrueMatrix(15,15);
        erode.setAttribute("matrix", m);
        dilate.setAttributes("matrix", m);

        // 3. Load and process the image
        MarvinImage image = MarvinImageIO.loadImage("./res/flowers.png");
        invert.process(image.clone(), image);
        MarvinImage binImage = MarvinColorModelConverter.rgbToBinary(image, 127);
        MarvinImageIO.saveImage(binImage, "./res/flowers_bin.png");
        erode.process(binImage.clone(), binImage);
        dilate.process(binImage.clone(), binImage);
        MarvinImageIO.saveImage(binImage, "./res/flowers_out.png");
    }

    public static void main(String[] args) {
        new RemoveBackground();
    }
}