Remove Background

Jul 11, 2014 at 6:40 PM
Edited Jul 11, 2014 at 6:53 PM
I've seen some examples (https://magick.codeplex.com/discussions/459021 or https://magick.codeplex.com/discussions/461154) of how to remove a background but I can't seem to figure out how to remove a background using the imagemagick suggested method (http://www.imagemagick.org/Usage/masking/#bg_remove).
-alpha set -channel RGBA -fuzz 20% \
-fill none -floodfill +0+0 white \
Any help converting this command to c# would be greatly appreciated.
Coordinator
Jul 14, 2014 at 3:47 PM
Edited Jul 14, 2014 at 3:47 PM
Your commands translates to the example below. But it does not work correctly due to a bug in ImageMagick. This will be fixed the next release of Magick.NET.
using (MagickImage image = new MagickImage("cyclops.png"))
{
  image.Alpha(AlphaOption.Set); // -alpha set
  image.ColorFuzz = 20; // -fuzz 20%
  // You don't need to specify -channel RGBA 
  image.FloodFill(MagickColor.Transparent, 0, 0, new MagickColor("#FFF")); // -fill none -floodfill +0+0 white
  image.Write("cyclops_flood_1.png");
}
Aug 2, 2014 at 11:28 AM
Has this been made available in the release you made yesterday?

As I need to handle background removing as well and just tried your exact code as posted in here and it didn't work
Coordinator
Aug 4, 2014 at 12:04 PM
This works in the current release. I have e-mailed kratuna a working example.
Nov 4, 2014 at 2:02 PM
Hi, I've been using this same technique to remove white edge background from a "Target" image, and for some reason, sometimes, the ColorFuzz is not applied.

After a couple of tests, I noticed the issue was intermittent, using same unmodified code. So I put the code in a loop, and indeed concluded its intermittent:
for (int x = 0; x < 20; x++)
{
    using (MagickImage image = new MagickImage(@"C:\Demo\Target.jpeg"))
    {
        image.Alpha(AlphaOption.Set);
        image.ColorFuzz = 20;
        image.FloodFill(MagickColor.Transparent, 0, 0, new MagickColor("#FFF"));
        image.Write(@"C:\Demo\Output\Output_" + x + ".png");
    }
}
Here's the SOURCE image.
And the RESULTS.

As you can see, results #1,3,5,7,9,11 are OK (the ColorFuzz was applied, edges are smooth), but not in the others.

I'm using the latest version "Magick.NET 7.0.0.0006".

Can someone reproduce this issue? Any clue what the problem could be?

Thank you.
Coordinator
Nov 4, 2014 at 8:31 PM
Edited Nov 4, 2014 at 9:01 PM
I cannot reproduce your problem. I have recently made some changes to color class of Magick++ in IM7. I am suspecting this has fixed it. A block of memory was not 'zero-ed' and that can lead to unexpected values and that would explain this behavior. I contacted you trough CodePlex. Can you reply to that message and tell me which version of Magick.NET you are using Q8/16 x86/x64/AnyCPU so I can send you a development build.
Nov 5, 2014 at 12:08 AM
Thanks again for the quick reply, and fix!

I confirm the Dev version you sent me indeed fixes the issue, ColorFuzz now always applied. I'll be using this Dev DLL until your next release.