This project has moved. For the latest updates, please go here.

adapting imagemagick background removal script to Magick.NET

Oct 10, 2016 at 12:14 PM
Hi All,

The script I'm desperatly trying to adapt is the one found in the original online documentation here: http://www.imagemagick.org/Usage/masking/#bg_remove
(see Recovering Semi-Transparent Edges further down)
Original IM script is http://www.imagemagick.org/Usage/scripts/bg_removal

But I want to stay with magick.NET !
Any help please ? or other .net script achieving the similar with some flexibility/parameters ?

Any big help would be much appreciated ; Thanks,
Fabrice

PS: I have posted that on stackoverflow some time ago already, without too much success http://stackoverflow.com/questions/38094657/magick-net-background-removal-script
Coordinator
Oct 10, 2016 at 5:29 PM
What have you tried so far and where did you get stuck? I could translate the whole script for you but that's probably gonna take me a couple hours.

p.s. Are you aware that this script requires you to know the background color?
Oct 11, 2016 at 7:06 PM
Hi Dirk,

If for you, helping in imagemagick + main author of Magick.net, it takes several hourS ....
.. then it is out (for the moment) of my possibilities. I already spent days on other basic tasks with magick.net (the documentation ... well ... is far beyond the tons of possibilities to put it positively :) )

It is clear that the piping+scripting style of that IM script:
convert "$image" \
  \( -clone 0 $bgnd -compose Difference -composite \
     $grayscale \
     -channel B -evaluate 'set' 0 +channel \) \
  \( -clone 1 -fill blue -fuzz $fzout%  $fill \
     -channel B -separate +channel \) \
  \( -clone 1 -fill blue -fuzz $fzin%  $fill \
     -channel B -separate +channel -negate \) \
  \( -clone 2,3 -negate -compose multiply -composite \) \
  $showmasks \
  \( -clone 0,3 +matte -compose CopyOpacity -composite \) \
  \( -clone 1 -channel R -separate +channel \
     -clone 4 +matte -compose CopyOpacity -composite \
     \( +clone -blur 0x30 +matte \) +swap -compose Over -composite \
     +matte -normalize \
     -clone 4 -compose multiply -composite  \
     -background $background -alpha shape \
     -clone 5 -compose over -composite \) \
  -delete 0--2 "$image_out"
is much more complicated when done operation by operation in c# style. I was not even sure if all these operations were implemented in your Library. Are they btw ?

My current approach was basically :
image.FloodFill(System.Drawing.Color.Transparent, 0, 0);
applied on 1 to 4 corners. So
  • Do you see a better method, with some code ?
    OR
  • Do you see with the floodfill, how I can smooth the edges ? (because it fact if works already good except the hard transition - so "simply" being able to put some pixel of transition would just make it very very good). Any line of code for that ?
Thanks, for your help,
Fabrice
Coordinator
Oct 15, 2016 at 9:53 PM
I think you will get the best result when you translate the script but that will be quite a lot of work. All of the operations are available in Magick.NET and most of them are methods on the MagickImage collection. For example -clone 0 at the start creates a clone of the input image and that will translate to this:
using (MagickImage image = new MagickImage("$image"))
{
  using (MagickImage clone0 = image.Clone())
  {
  }
}
Oct 17, 2016 at 8:41 AM
thanks Dirk, but this "best result" suggestion worth zero if I can not achieve it. As said in previous message I have not (yet) enough understanding of all IM methods & already spent days on IM & Magick.net try/errors loops to compensate my understanding & docs.
So I will not follow that route.
I will either search for some help to soften the edges of my current FloodFill or look for another script.
BR,
Fabrice