.NET: Background Remover

Hello,

We are interested in buying your Aspose.Imaging product. There are couple of issues which we would like to discuss first before proceeding ahead with the purchase.

  • Our primary objective is to use remove background. But the demo app provided on your website to remove background is very slow. Is it something to do with the demo? We don’t want to have this much of delay while doing it in our application.

  • We also tried downloading examples project from Aspose.Imaging-for-.NET but the remove background example did not work as expected. We would like to run the code first to make sure it works well with our images. In our scenario, we will provide image as blob (byte array) and after removing the background, it will be converted back to byte array.

We would really appreciate helping us address the above issues.

Thank you,
Sameer

@sameer.ahmed What kind of issue did you have with the example project? I just checked, the examples project works fine, you just need to remove the File.Delete part to check the result. Since Image.Load method requires either a file path or a stream as an input and in your scenario you use byte array, I suggest you to use MemoryStream for input.
The method you are interested in is in the 4th test features group in the examples console application and the code is in the GraphCutAutoMasking.cs file, feel free to modify it.
So your test code should look kinda like this (slightly modified code from the examples project):

byte[] source = new byte[1024];
MaskingResult results;
using (MemoryStream stream = new MemoryStream(source))
using (RasterImage image = (RasterImage)Image.Load(stream))
{
    // To use Graph Cut with auto calculated strokes, AutoMaskingGraphCutOptions is used.
    AutoMaskingGraphCutOptions options = new AutoMaskingGraphCutOptions
    {
        // Indicating that a new calculation of the default strokes should be performed during the image decomposition.
        CalculateDefaultStrokes = true,
        // Setting post-process feathering radius based on the image size.
        FeatheringRadius = (Math.Max(image.Width, image.Height) / 500) + 1,
        Method = SegmentationMethod.GraphCut,
        Decompose = false,
        ExportOptions =
            new PngOptions()
            {
                ColorType = PngColorType.TruecolorWithAlpha,
                Source = new FileCreateSource("tempFile")
            },
        BackgroundReplacementColor = Color.Transparent
    };

    results = new ImageMasking(image).Decompose(options);

    using (RasterImage resultImage = (RasterImage)results[1].GetImage())
    {
        resultImage.Save(dataDir + "output.png", new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });
    }
}

Hi @Alexey.Karpenko, thank you for your response. I was running the sample example i.e. GraphCutAutoMasking and I also did comment out the File.Delete line. To run the example, I am not even reading from byte array. I am reading from an image and after the above code runs successfully, there is no output.png file in the folder.

Not sure if this could be helpful but when debugging the value in resultImage, one property (RawDataSettings) has an error.
image.png (9.8 KB)

@sameer.ahmed Maybe there was no “/” on the end of the dataDir between it and the “output.png” and so the output image is in the parent directory? It’s safer to use Path.Combine to get an output file path.
With this modification, fully running code looks like this, you just need to change the input file:

public static void Run()
{
    Console.WriteLine("Running example GraphCutAutoMasking");

    // The path to the documents directory.
    string dataDir = @"C:\temp";
    string inputFile = Path.Combine(dataDir, "input.jpg");
    string outputFile = Path.Combine(dataDir, "output.png");
    
    MaskingResult results;
    using (RasterImage image = (RasterImage)Image.Load(inputFile))
    {
        // To use Graph Cut with auto calculated strokes, AutoMaskingGraphCutOptions is used.
        AutoMaskingGraphCutOptions options = new AutoMaskingGraphCutOptions
        {
            // Indicating that a new calculation of the default strokes should be performed during the image decomposition.
            CalculateDefaultStrokes = true,
            // Setting post-process feathering radius based on the image size.
            FeatheringRadius = (Math.Max(image.Width, image.Height) / 500) + 1,
            Method = SegmentationMethod.GraphCut,
            Decompose = false,
            ExportOptions =
                new PngOptions()
                {
                    ColorType = PngColorType.TruecolorWithAlpha,
                    Source = new FileCreateSource("tempFile")
                },
            BackgroundReplacementColor = Color.Transparent
        };

        results = new ImageMasking(image).Decompose(options);

        using (RasterImage resultImage = (RasterImage)results[1].GetImage())
        {
            resultImage.Save(outputFile, new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });
        }

        Console.WriteLine("Finished example GraphCutAutoMasking");
    }
}

And here you can check examples on how to change masking results:

@Alexey.Karpenko, thanks for the help. I was able to run the example and get output results. The resulting image from the GraphCutAutoMasking sample code does not meet our expected results. Is there a way it can be improved? We are looking at generating results like remove.bg.

@sameer.ahmed Yes, you can use following methods from the mentioned article to improve the result.
This one utilizing Imaging.Cloud API:

Or one of this ones with some manual help:

@Alexey.Karpenko, how/where to get the credentials for following line to test Imaging.Cloud API example:

ImagingApi api = new ImagingApi(“AppSid”, “AppKey”, “BaseURL”);

The manual help examples will not be helpful in our use case scenario.

@sameer.ahmed
Please use following guide to register Aspose.Imaging Cloud account.

I have looked at the Imaging.Cloud API example, an image needs to be uploaded to the cloud. Due to our security restrictions, we can’t do that. Is there a possibility to perform the same operation on the machine? If not then what could be other alternate solutions? I have tested other examples but results are not as per our expectations.

@sameer.ahmed
Unfortunately for now haven’t equivalent on-premise solution, but you may take into consideration that cloud API allows set up your cloud storage (i.e. dropbox or Google drive) to keep your files.
Otherwise you may wait till we complete publishing of Aspose.Imaging Cloud self-hosted configuration,
where you can use Local storage and use of Aspose.Imaging Cloud for evaluation in meantime.