From 4e43c94e1284e9ef327bb3b4c33087dbcba3f72b Mon Sep 17 00:00:00 2001 From: akshayk-dev <162972316+akshayk-dev@users.noreply.github.com> Date: Mon, 21 Jul 2025 23:48:22 +0530 Subject: [PATCH 1/2] Update imaging.md Documentation for the Image Foreground Extractor API --- docs/apis/imaging.md | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/docs/apis/imaging.md b/docs/apis/imaging.md index d437d707..e6a2b2fc 100644 --- a/docs/apis/imaging.md +++ b/docs/apis/imaging.md @@ -15,6 +15,7 @@ Imaging features in Windows AI Foundry support the following capabilities: - [**Image Super Resolution**](#what-can-i-do-with-image-super-resolution): scaling and sharpening an image. - [**Image Description**](#what-can-i-do-with-image-description): generating text that describes an image. - [**Image Segmentation**](#what-can-i-do-with-image-segmentation): identifying objects within an image. +- [**Image Foreground Extractor**](#what-can-i-do-with-object-erase): foreground/background segmentation on an input image - [**Object Erase**](#what-can-i-do-with-object-erase): removing objects from an image. For **API details**, see [API ref for AI imaging features](/windows/windows-app-sdk/api/winrt/microsoft.windows.ai.imaging). @@ -353,6 +354,66 @@ ImageObjectExtractorHint hint( ); ``` +## What can I do with Image Foreground Extractor ? + +Image foreground extractor can be used to foreground or background segmentation on an input image. Using this API developers can enable tasks like background removal, sticker generation, et. in their applications. + +### More details on the Image Foreground Extractor + +#### ImageForegroundExtractor.CreateAsync +Use this method to create an ImageForegroundExtractor instance. + +#### ImageForegroundExtractor.GetSoftwareBitmapForegroundMask +Takes a SoftwareBitmap as input and returns a Gray8 SoftwareBitmap as output. + +- **Supported Input Pixel Formats**: Bgra32 +- **Output Pixel Format**: Gray8 + +#### ImageForegroundExtractor.GetImageBufferForegroundMask +Takes an ImageBuffer as input. Returns a Gray8 ImageBuffer as output. + +- **Supported Input Pixel Formats**: Bgra32 +- **Output Pixel Format**: Gray8 + +#### Example + +#### Generating a Mask from a SoftwareBitmap + +```csharp +using Microsoft.Windows.AI.Imaging; +using Microsoft.Windows.AI; + +public class ForegroundExtractor +{ + private ImageForegroundExtractor model; + + private async Task CreateImageForegroundExtractor() + { + if (ImageForegroundExtractor.GetReadyState() == AIFeatureReadyState.NotReady) + { + await ImageForegroundExtractor.EnsureReadyAsync(); + } + model = await ImageForegroundExtractor.CreateAsync(); + } + + public async Task GetForegroundMaskAsync(SoftwareBitmap inputBitmap) + { + if (model == null) + { + await CreateImageForegroundExtractor(); + } + + return model.GetMaskFromSoftwareBitmap(inputBitmap); + } +} + +var softwareBitmap = await Utils.LoadSampleImageAsync("fish.jpg"); +var fgExtractor = new ForegroundExtractor(); +var mask = await fgExtractor.GetForegroundMaskAsync(softwareBitmap); +``` + +This API can be used similarly on an `ImageBuffer` object as well using the `GetMaskFromImageBuffer` method. + ## What can I do with Object Erase? Object Erase can be used to remove objects from images. The model takes both an image and a greyscale mask indicating the object to be removed, erases the masked area from the image, and replaces the erased area with the image background. From afa0cafaf47b6262a058bcbe3a80bfd241a592d8 Mon Sep 17 00:00:00 2001 From: akshayk-dev <162972316+akshayk-dev@users.noreply.github.com> Date: Mon, 4 Aug 2025 23:53:18 +0530 Subject: [PATCH 2/2] Update imaging.md Updated the release notes for ImageForegroundExtractor --- docs/apis/imaging.md | 78 +++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/docs/apis/imaging.md b/docs/apis/imaging.md index e6a2b2fc..d1ff233b 100644 --- a/docs/apis/imaging.md +++ b/docs/apis/imaging.md @@ -356,63 +356,61 @@ ImageObjectExtractorHint hint( ## What can I do with Image Foreground Extractor ? -Image foreground extractor can be used to foreground or background segmentation on an input image. Using this API developers can enable tasks like background removal, sticker generation, et. in their applications. +Image foreground extractor can be used to perform foreground or background segmentation on an input image. This API can be used for tasks like background removal, sticker generation, etc. -### More details on the Image Foreground Extractor +The returned mask is in grayscale-8 format with the pixels of the mask for the foreground having a value of 255 (background pixels having a value of 0). -#### ImageForegroundExtractor.CreateAsync -Use this method to create an ImageForegroundExtractor instance. +### Generating a Mask from a SoftwareBitmap -#### ImageForegroundExtractor.GetSoftwareBitmapForegroundMask -Takes a SoftwareBitmap as input and returns a Gray8 SoftwareBitmap as output. - -- **Supported Input Pixel Formats**: Bgra32 -- **Output Pixel Format**: Gray8 - -#### ImageForegroundExtractor.GetImageBufferForegroundMask -Takes an ImageBuffer as input. Returns a Gray8 ImageBuffer as output. - -- **Supported Input Pixel Formats**: Bgra32 -- **Output Pixel Format**: Gray8 - -#### Example - -#### Generating a Mask from a SoftwareBitmap +1. Ensure the `ImageForegroundExtractor` is ready by calling `GetReadyState()` and waiting for the EnsureReadyAsync method to return successfully. +2. Once the model is ready, you can create an instance of `ImageForegroundExtractor` using the `CreateAsync` method. +3. Pass the input image to ImageForegroundExtractor.GetMaskFromSoftwareBitmap() to obtain the foreground mask. ```csharp using Microsoft.Windows.AI.Imaging; using Microsoft.Windows.AI; -public class ForegroundExtractor +if (ImageForegroundExtractor::GetReadyState() == AIFeatureReadyState.EnsureNeeded) { - private ImageForegroundExtractor model; - - private async Task CreateImageForegroundExtractor() + var result = await ImageObjectRemover.EnsureReadyAsync(); + if (result.Status != PackageDeploymentStatus.CompletedSuccess) { - if (ImageForegroundExtractor.GetReadyState() == AIFeatureReadyState.NotReady) - { - await ImageForegroundExtractor.EnsureReadyAsync(); - } - model = await ImageForegroundExtractor.CreateAsync(); + throw result.ExtendedError; } +} - public async Task GetForegroundMaskAsync(SoftwareBitmap inputBitmap) - { - if (model == null) - { - await CreateImageForegroundExtractor(); - } +var model = await ImageForegroundExtractor.CreateAsync(); + +// Insert your own softwareBitmap here +var foregroundMask = model.GetMaskFromSoftwareBitmap(softwareBitmap); +``` + +```cpp +#include +#include +#include +#include +using namespace winrt::Microsoft::Graphics::Imaging; +using namespace winrt::Microsoft::Windows::AI.Imaging; +using namespace winrt::Windows::Graphics::Imaging; +using namespace winrt::Windows::Foundation; - return model.GetMaskFromSoftwareBitmap(inputBitmap); +if (ImageForegroundExtractor::GetReadyState() == AIFeatureReadyState::NotReady) +{ + auto loadResult = ImageForegroundExtractor::EnsureReadyAsync().get(); + + if (loadResult.Status() != AIFeatureReadyResultState::Success) + { + throw winrt::hresult_error(loadResult.ExtendedError()); } } -var softwareBitmap = await Utils.LoadSampleImageAsync("fish.jpg"); -var fgExtractor = new ForegroundExtractor(); -var mask = await fgExtractor.GetForegroundMaskAsync(softwareBitmap); -``` +auto model = co_await ImageForegroundExtractor::CreateAsync(); + +// Insert your own softwareBitmap here +auto foregroundMask = model.GetMaskFromSoftwareBitmap(softwareBitmap); -This API can be used similarly on an `ImageBuffer` object as well using the `GetMaskFromImageBuffer` method. +``` ## What can I do with Object Erase?