Bemærk
Adgang til denne side kræver godkendelse. Du kan prøve at logge på eller ændre mapper.
Adgang til denne side kræver godkendelse. Du kan prøve at ændre mapper.
You can use ImageForegroundExtractor to segment the foreground of an input image and enable features such as background removal and sticker generation.
The returned mask is in greyscale-8 format. Pixel values range from 0 to 255, where 0 represents background pixels, 255 represents foreground pixels, and intermediate values indicate a blend of foreground and background pixels.
For API details, see API ref for AI imaging features.
For content moderation details, see Content safety with generative AI APIs.
Important
Package Manifest Requirements: To use Windows AI imaging APIs, your app must be packaged as an MSIX package with the systemAIModels capability declared in your Package.appxmanifest. Additionally, ensure your manifest's MaxVersionTested attribute is set to a recent Windows version (e.g., 10.0.26226.0 or later) to properly support the Windows AI features. Using older values may cause "Not declared by app" errors when loading the model.
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.26226.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.26226.0" />
</Dependencies>
Generating a mask from a bitmap image
- Call GetReadyState and wait for EnsureReadyAsync to complete successfully to confirm that the ImageForegroundExtractor object is ready.
- After the model is ready, call CreateAsync to instantiate an ImageForegroundExtractor object.
- Call GetMaskFromSoftwareBitmap with the input image to generate the foreground mask.
using Microsoft.Windows.AI.Imaging;
using Microsoft.Windows.AI;
if (ImageForegroundExtractor.GetReadyState() == AIFeatureReadyState.NotReady)
{
var result = await ImageForegroundExtractor.EnsureReadyAsync();
if (result.Status != AIFeatureReadyResultState.Success)
{
throw result.ExtendedError;
}
}
var model = await ImageForegroundExtractor.CreateAsync();
// Insert your own softwareBitmap here.
var foregroundMask = model.GetMaskFromSoftwareBitmap(softwareBitmap);
#include <winrt/Microsoft.Graphics.Imaging.h>
#include <winrt/Microsoft.Windows.AI.Imaging.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Foundation.h>
using namespace winrt::Microsoft::Graphics::Imaging;
using namespace winrt::Microsoft::Windows::AI.Imaging;
using namespace winrt::Windows::Graphics::Imaging;
using namespace winrt::Windows::Foundation;
if (ImageForegroundExtractor::GetReadyState() == AIFeatureReadyState::NotReady)
{
auto loadResult = ImageForegroundExtractor::EnsureReadyAsync().get();
if (loadResult.Status() != AIFeatureReadyResultState::Success)
{
throw winrt::hresult_error(loadResult.ExtendedError());
}
}
auto model = co_await ImageForegroundExtractor::CreateAsync();
// Insert your own softwareBitmap here.
auto foregroundMask = model.GetMaskFromSoftwareBitmap(softwareBitmap);