Removal of Complex Background from Image

Hello,

What I want to do is to remove complex background from an image attached and only highlight or draw polygons around trees. I have tried and tried to highlight green color of trees to separate from the background and none was successful. I also attached a target image of what I roughly want to achieve. I really appreciate some inputs. Thank you very much.

Hi,

if the image is assigned to the variable img, then

ImageMultiply[
 Erosion[DeleteSmallComponents[
   GeodesicClosing[
    GeodesicOpening[
     AlphaChannel@RemoveBackground[img, {"Foreground", Brown}], 15], 
    10], 100], 8], img]

gives

DeleteSmallComponents[
ImageMultiply[
Erosion[DeleteSmallComponents[
GeodesicClosing[
GeodesicOpening[
AlphaChannel@RemoveBackground[img, {“Foreground”, Brown}], 15],
10], 100], 8], img], 1200]

removes the small components.

If you want a white background use:

RemoveBackground[
 DeleteSmallComponents[
  ImageMultiply[
   Erosion[DeleteSmallComponents[
     GeodesicClosing[
      GeodesicOpening[
       AlphaChannel@RemoveBackground[img, {"Foreground", Brown}], 15],
       10], 100], 8], img], 1200], {"Background", "Uniform"}]

You can colorise the individual trees (note that it doesn’t work right for two close trees in this image):

MorphologicalComponents[
  RemoveBackground[
   DeleteSmallComponents[
    ImageMultiply[
     Erosion[DeleteSmallComponents[
       GeodesicClosing[
        GeodesicOpening[
         AlphaChannel@RemoveBackground[img, {"Foreground", Brown}], 
         15], 10], 100], 8], img], 1200], {"Background", 
    "Uniform"}]] // Colorise

This here would count the trees (at least approximately):

Length[Select[
  ComponentMeasurements[
   MorphologicalComponents[
    RemoveBackground[
     DeleteSmallComponents[
      ImageMultiply[
       Erosion[DeleteSmallComponents[
         GeodesicClosing[
          GeodesicOpening[
           AlphaChannel@RemoveBackground[img, {"Foreground", Brown}], 
           15], 10], 100], 8], img], 1200], {"Background", 
      "Uniform"}]], "Area"], #[[2]] > 1000 &]]

In this case it gives 12 which corresponds to the large coloured areas in the last image. Hence, it only counts well separated trees.

Cheers,

Marco

Hi all,

removing a complex background can be a difficult task; therefore Marco’s nice solution can hardly be shorter.
If one concentrates on the fact that due to snow the background is basically gray and the trees stay (at least a little bit) green, things get much easier (though I am not sure that this approach answers the original question):

greenQ[p : {r_, g_, b_}] := With[{mean = Mean[p]}, If[g > 1.03 mean, 1, 0]]
mask = Erosion[Dilation[ImageApply[greenQ, img], 4], 4]

this gives a “mask” for green trees. Lets check:

Regards – Henrik

Dear Henrik,

that is a fantastic solution! A great and clean separation which captures all details. Very nice. I see that, should Inever have an image processing problem, I should ask you!

Thanks for sharing,

M.

Hi Marco and Henrik,

Both approaches are fantastic to me! I really want to study both of your codings.

Again, thank you very much!

Eiji