Message Boards Message Boards

2
|
5221 Views
|
4 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Removal of Complex Background from Image

Posted 9 years ago

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.

TreeImage TargeImage: Image I want to achieve

POSTED BY: Eiji Matsuzaki
4 Replies

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:

enter image description here

Regards -- Henrik

POSTED BY: Henrik Schachner

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

enter image description here

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

removes the small components.

enter image description here

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"}]

enter image description here

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

enter image description here

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

POSTED BY: Marco Thiel

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.

POSTED BY: Marco Thiel
Posted 9 years ago

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

POSTED BY: Eiji Matsuzaki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract