All Products
Search
Document Center

:Smart matting

Last Updated:Oct 30, 2026

Smart Matting automatically identifies an image's salient subject, separates it from the background, and returns an image of the subject with the background removed. It also offers multiple background options and custom dimension settings to enhance product presentation. This is a synchronous API.

Use cases

  • Enable secondary editing of assets: After separating the subject from the background, you can edit the image further.

  • Improve asset processing efficiency: Quickly extract subjects from various source images for easier background replacement, accelerating the creation of ad visuals and marketing posters.

If you need to chain multiple operations like matting, translation, erasing, and outpainting into a single workflow, we recommend using Smart Layout for orchestration. This API serves as an independent atomic capability.

Features

1. Precise subject recognition

  • AI-powered automatic recognition: Uses a deep learning model to intelligently distinguish between foreground subjects (such as people, products, or objects) and the background, eliminating the need for manual outlining.

  • Adaptable to multiple scenarios: Achieves high-precision segmentation for complex scenes, including fine details like hair strands, transparent objects, and fuzzy edges, with an accuracy of over 95%.

2. Intelligent edge optimization

  • Fine-detail processing: Automatically refines edge aliasing and semi-transparent areas, such as smoke or glass, to ensure natural transitions.

3. Multiple output formats and background controls

  • Transparent background export: Generates images with a transparent background (PNG) or a white background, suitable for e-commerce and design applications.

  • Custom background colors: Allows you to specify a custom color for the background.

Before and after

Original image (input)

Result image (output)

image

image

image

image

image

image

Endpoint

POST /rest/ai/image/matting

For complete request signing instructions, SDK examples in multiple languages including Java, Python, and Go, and online debugging, see the Alibaba Cloud OpenAPI Developer Portal: Smart Matting API (OpenAPI Portal).

Request parameters

Parameter

Type

Required

Description

BackGroundType

String

Yes

The background format for the output image: WHITE_BACKGROUND (white background) or TRANSPARENT (transparent PNG). To specify a custom background color, pass the BgColor parameter as well.

ImageUrl

String

Yes

The URL of the source image. Requirements: JPG, JPEG, PNG, BMP, or WEBP format; resolution between 100×100 and 3000×3000 pixels; file size ≤ 10 MB.

TargetWidth

Number

No

The desired output width in pixels. Valid values: 100 to 5000. If specified, the service automatically crops blank margins and scales the image. If omitted, the image retains its original width and the subject's position remains unchanged.

TargetHeight

Number

No

The desired output height in pixels. Valid values: 100 to 5000. If specified, the service automatically crops blank margins and scales the image. If omitted, the image retains its original height and the subject's position remains unchanged.

BgColor

String

No

A custom background color specified as a BGR triplet (note: BGR, not RGB), separated by commas. For example, "255,255,255" for white or "0,0,255" for red. The default is white. Only pass this parameter if you need a solid background color other than white.

Response fields

Parameter

Type

Description

Code

String

The business status code. Success indicates a successful operation.

RequestId

String

The unique request ID. Provide this ID when contacting technical support for assistance.

Message

String

The response message. On success, the value is "Success".

Success

Boolean

Indicates whether the call was successful.

Data

Object

The returned result object.

Data.ImageUrl

String

A signed OSS URL for the processed image. The result is a transparent PNG if BackGroundType=TRANSPARENT, or an image with a white or custom background otherwise.

Data.Score

Number

A quality score for the matting result. Higher scores indicate greater reliability.

Data.Width

Number

The width of the output image in pixels.

Data.Height

Number

The height of the output image in pixels.

Data.structData

Object

Contains layer information, including the original image URL, the matting result URL, and matting region details.

Data.UsageMap

Object

The usage information.

Data.UsageMap.ProcessedImageCount

Number

The number of images processed.

Complete example

Request example

{
  "ImageUrl": "https://sc02.alicdn.com/kf/H09364d2c7fc942e685d3b0f656261b24Q.png",
  "BackGroundType": "WHITE_BACKGROUND",
  "TargetWidth": 800,
  "TargetHeight": 800,
  "BgColor": "255,255,255"
}

Response example

{
  "Data": {
    "ImageUrl": "http://dashscope-7c2c.oss-cn-shanghai.aliyuncs.com/7d/4a/20260525/f3547444/52033655bd054737960a87518655d935_202605251926_5.959999978542328.jpg?Expires=1782300370&OSSAccessKeyId=LTAI5tPxpiCM2hjmWrFXrym1&Signature=XskJ9GEdikVhbBz7DfgbESj5xJ0%3D",
    "Score": 5.959999978542328,
    "Height": 800,
    "UsageMap": {
      "ProcessedImageCount": 1
    },
    "Width": 800,
    "structData": {
            "message": {
                "matting_results": {
                    "origin_url": "https://img.alicdn.com/imgextra/i3/O1CN01nG4vzu2AIxB7YWE9O_!!6000000008181-0-tps-1500-1500.jpg",
                    "score": "0.8999999761581421",
                    "file": "data/general_matting_online/nhci_aigc/c21baea1df714751a02beed6f1a94602_202610211757_0.8999999761581421.png",
                    "extra_info": "",
                    "pass": "0",
                    "bbox": "102,377,1500,1253",
                    "plain_url": "http://aib-image.oss-ap-southeast-1.aliyuncs.com/data%2Fgeneral_matting_online%2Fnhci_aigc%2Fc21baea1df714751a02beed6f1a94602_202610211757_0.8999999761581421_plain.png?OSSAccessKeyId=LTAI5t75Zk154w1Ne1UmpHKJ&Expires=2792576662&Signature=5%2FCVs88ORcu724fV9XPDtgd%2Bl6E%3D",
                    "image_shape": [
                        1500,
                        1500
                    ],
                    "url": "http://aib-image.oss-ap-southeast-1.aliyuncs.com/data%2Fgeneral_matting_online%2Fnhci_aigc%2Fc21baea1df714751a02beed6f1a94602_202610211757_0.8999999761581421.png?OSSAccessKeyId=LTAI5t75Zk154w1Ne1UmpHKJ&Expires=2792576662&Signature=IbR7G6lF2eK0oQQEDVqaF2yKEK0%3D"
                },
                "status_info": {
                    "code": 0,
                    "message": ""
                }
            }
        }
  },
  "RequestId": "7BEDF90E-D04E-1430-BDAE-18A31F84A348",
  "Message": "Success",
  "Code": "success",
  "Success": true
}

Recommendations

  • Transparent vs. white background: For e-commerce product images that require a white background, pass WHITE_BACKGROUND. For secondary composition (such as adding a marketing background), we recommend passing TRANSPARENT for greater flexibility in client-side processing.

  • Resolution and quality: The closer the source image resolution is to the 3000×3000 pixel limit, the finer the details of edges like hair or transparent objects will be. For very small images (such as 100×100 pixels), we recommend upscaling them before matting.

  • Color channel order: The BgColor parameter uses BGR order, not the more common RGB. Ensure you convert the format when porting code from libraries other than OpenCV.

  • Quality score: A low Score usually indicates that the source image is too complex (for example, a solid-colored product against a solid-colored background). In such cases, we recommend manual review or using a different source image.

Error codes

Error code

Description

InvalidParameter

ImageUrl is empty, the image format or resolution is out of range, or BgColor is not a valid BGR triplet.

InputContentBlocked

The input image is blocked by content moderation.

DownstreamUnavailable

Failed to download the image, or a downstream vision model is temporarily unavailable. Retry the request using an exponential backoff strategy.

FreeQuotaExhausted

You have exhausted your free trial quota. To continue, purchase a resource plan or disable the "stop when quota is exhausted" setting.

InternalError

An internal server-side error occurred. Please save the RequestId and contact technical support.

For a complete list of error codes, see Error Codes.

Limitations

  • Invocation mode: This is a synchronous API with a default QPS of 5 and a 30-second request timeout.

  • Input image: JPG, JPEG, PNG, BMP, or WEBP format. Resolution must be between 100×100 and 3000×3000 pixels. File size must be 10 MB or less.

  • Output: The service outputs a JPG for solid backgrounds or a PNG with an alpha channel for transparent backgrounds. The result is delivered as a signed OSS URL in the Data.ImageUrl field, which is typically valid for 7 days.

  • Billing: $0.0005 per image, charged based on the ProcessedImageCount field. For details, see Material Optimization Billing.

FAQ

How does the performance of Smart Matting vary across different types of images?

It performs best on e-commerce product images where the subject is large, complete, and clearly defined, whether there is a single subject or multiple distinct subjects. Accuracy may be lower for images with special materials like transparent objects or for images where the subject is not well-defined, such as step-by-step diagrams, close-up detail shots, or SKU composite images.

Does matting reduce the resolution or quality of the subject?

No. If you do not specify a target width and height, the output image has the same dimensions as the original, and the quality of the matted subject is unaffected. If you set a target width and height that differ from the original dimensions, the result is resized to the target dimensions, which involves scaling the subject. This may affect quality if the target size is significantly different from the original.