简体   繁体   中英

Extract object (signature) from white background image (A4 paper) using C# / .NET?

How can I extract an object (Signature) from a white background image (A4 Paper) taken using a mobile camera in C#/.NET and crop it if possible?

I am trying ImageMagick library, but the out put is not 100% correct, I tried to manipulate the values without luck:

    string GetSignature(string signature) {
        string withoutBackground = "signature_no_bg.png";
        using (var image = new MagickImage(signature))
        {
            image.Transparent(MagickColors.White);
            // -alpha set
            image.Alpha(AlphaOption.Set);
            // -channel RGBA (don't think you need this)
            // -fuzz 50% 
            image.ColorFuzz = new Percentage(40);
            // -fill none
            image.Settings.FillColor = MagickColors.None;
            // -floodfill +0+0 white 
            image.FloodFill(MagickColors.White, 0, 0);

            image.Write(Server.MapPath(withoutBackground));
        }

        return withoutBackground;
    }

Using the above code, the following image:

文本

was converted to:

文本

Another option was to https://www.remove.bg/tools-api , it worked perfectly but its a bit expensive.

Any suggestions to enhance my ImageMagick code or to use another kind of libraries?

I'm not experienced with ImageMagick, but I would try the following steps as a simple(st) alternative to filling:

  1. Convert to greyscale
  2. Threshold the image
  3. Use the thresholded image as mask to extract the signature from the original RGB Image.

You will have to try how well it performs on your own images though. You can also try to include some blurring on the greyscale image before thresholding against noise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM