AI Background Removal: Pipeline and Precise Matting

Manual cropping of product photos eats up time and budget, while operator errors lead to defects in the catalog. We automate background removal with neural networks—from fast batch processing to precise matting of hair and fur. Our team delivers a turnkey pipeline with model selection, fine-tuning when needed, and ongoing support, so you get clean images without missing deadlines.

AI Development Areas

Frequently Asked Questions

Latest works

  • Development of a web application for FEEDME
    Development of a web application for FEEDME
    1344
  • Development of an online store for the company FURNORO
    Development of an online store for the company FURNORO
    1307
  • B2B Advance company logo design
    B2B Advance company logo design
    754
  • Development of a web application for Enviok
    Development of a web application for Enviok
    1050
  • AIDER company logo development
    AIDER company logo development
    994
  • CRM development for Chasseurs
    CRM development for Chasseurs
    1099

AI-удаление фона с изображений

Manually cutting out product photos is slow and expensive. Processing a catalog of 10,000 items takes weeks, and operator errors lead to waste. We automate this task with modern neural networks: from fast batch background removal to precise alpha matting of hair and fur. Our experience spans over 5 years, with more than 50 implemented solutions and over 1 million images processed. For example, a catalog of 5,000 images can be processed for about $4,000, saving 80% compared to manual editing. We guarantee quality results.

Our neural network background removal pipeline uses RVM matting and SAM segmentation for precise photo cropping and image matting. Key details: API integration in 1-2 days, supports common formats (JPG, PNG, WEBP), and includes batch processing with GPU acceleration.

Какие модели работают лучше всего?

Background removal falls into two classes: coarse (for rectangular objects without transparency) and precise alpha matting (for hair, fur, glass). The former is solved by detection and binary mask, the latter by predicting an alpha channel (0–1) per pixel. We select the tool for the task: for e-commerce, REMBG suffices; for portraits, SAM2 with subsequent matting is better.

Fine-tuning is needed when...

Pre-trained models handle standard objects well: people, products on white backgrounds. But if your catalog contains specific items (jewelry, glassware, fur products), quality drops. Fine-tuning on 20–50 labeled photos improves matting accuracy by 15–20% in MSE metric. We include this step if tests show insufficient quality.

Как работает SAM2 для удаления фона?

SAM2 (Segment Anything Model 2 by Meta) delivers state-of-the-art segmentation quality via text prompt or bbox. The Grounding DINO + SAM2 combo has become the standard in recent years. Below is a Python implementation example:

import torch
import numpy as np
from PIL import Image
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
from groundingdino.util.inference import load_model, predict

def remove_background_grounded_sam2(
    image_path: str,
    text_prompt: str = 'product',
    box_threshold: float = 0.3,
    text_threshold: float = 0.25,
    output_path: str = None
) -> Image.Image:
    image = Image.open(image_path).convert('RGB')
    image_np = np.array(image)

    gdino_model = load_model(
        'groundingdino/config/GroundingDINO_SwinT_OGC.py',
        'weights/groundingdino_swint_ogc.pth'
    )
    boxes, _, _ = predict(
        model=gdino_model,
        image=image_np,
        caption=text_prompt,
        box_threshold=box_threshold,
        text_threshold=text_threshold
    )

    if len(boxes) == 0:
        raise ValueError(f'Object "{text_prompt}" not found')

    sam2 = build_sam2(
        'sam2_hiera_large.yaml',
        'weights/sam2_hiera_large.pt',
        device='cuda'
    )
    predictor = SAM2ImagePredictor(sam2)
    predictor.set_image(image_np)

    best_box = boxes[0].numpy() * np.array([
        image_np.shape[1],
        image_np.shape[0],
        image_np.shape[1],
        image_np.shape[0]
    ])
    masks, scores, _ = predictor.predict(
        box=best_box,
        multimask_output=True
    )
    best_mask = masks[np.argmax(scores)]

    result_rgba = np.dstack([image_np, best_mask.astype(np.uint8) * 255])
    result = Image.fromarray(result_rgba, 'RGBA')

    if output_path:
        result.save(output_path, 'PNG')

    return result

SAM2 + matting is optimal for complex edges

Compared to U2-Net and RVM. RVM is fast (0.05 sec per photo) but edges are coarse—hair turns into mush. SAM2 with fine-tuning gives an alpha map with thin translucent edges. For hair and fur, we combine SAM2 with closed-form matting (source: Wikipedia)—the final quality is 2–3 times higher in MSE metric. For e-commerce, our SAM2 matting and alpha matting pipeline delivers superior quality for hair and fur. The table below provides an objective comparison.

Инструмент Скорость Качество краёв Волосы/мех Применение
REMBG (U2-Net) 0.3–0.8s/img Среднее Плохо Быстрый батч
REMBG (IS-Net) 0.5–1.2s/img Хорошее Удовлетворительно Товары
SAM2 0.8–2s/img Очень хорошее Хорошо Точная сегментация
SAM2 + matting 2–5s/img Отличное Отлично Портреты, мех
BiMatting 1–3s/img Отличное Отлично Профессиональный

Alpha matting для сложных краёв

Hair, fur, thin branches—SAM2 gives a coarse mask via bbox, edges become pixelated. For these cases, we apply alpha matting on top of the SAM mask—a method that restores translucency at boundaries. We use closed-form matting as the best compromise between speed and quality. Implementation example:

from pymatting import estimate_alpha_cf, estimate_foreground_ml
import cv2


def refine_mask_with_matting(
    image: np.ndarray,
    rough_mask: np.ndarray,
    erosion_px: int = 10,
    dilation_px: int = 10,
) -> np.ndarray:
    kernel = np.ones((erosion_px, erosion_px), np.uint8)
    fg_mask = cv2.erode(
        rough_mask.astype(np.uint8) * 255,
        kernel,
    )
    bg_mask = cv2.dilate(
        rough_mask.astype(np.uint8) * 255,
        kernel,
    )
    trimap = np.full(rough_mask.shape, 128, dtype=np.uint8)
    trimap[fg_mask > 0] = 255
    trimap[bg_mask == 0] = 0
    image_float = image.astype(np.float64) / 255.0
    trimap_float = trimap.astype(np.float64) / 255.0
    alpha = estimate_alpha_cf(image_float, trimap_float)
    return (alpha * 255).astype(np.uint8)

Батчевая обработка для e-commerce

from rembg import remove, new_session
from PIL import Image
from pathlib import Path
import concurrent.futures

def batch_remove_background(
    input_dir: str,
    output_dir: str,
    model_name: str = 'isnet-general-use',
    max_workers: int = 4
) -> dict:
    session = new_session(model_name)
    input_paths = list(Path(input_dir).glob('*.{jpg,jpeg,png,webp}'))
    results = {'success': 0, 'failed': 0, 'errors': []}

    def process_one(img_path: Path) -> bool:
        try:
            with open(img_path, 'rb') as f:
                input_data = f.read()
            output_data = remove(input_data, session=session)
            out_path = Path(output_dir) / (img_path.stem + '.png')
            with open(out_path, 'wb') as f:
                f.write(output_data)
            return True
        except Exception as e:
            results['errors'].append(str(e))
            return False

    with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as ex:
        futures = {ex.submit(process_one, p): p for p in input_paths}
        for fut in concurrent.futures.as_completed(futures):
            if fut.result():
                results['success'] += 1
            else:
                results['failed'] += 1
    return results

For mass deployment, we build a pipeline on GPU (NVIDIA T4 or A100). Task queue via Redis + Celery, output is PNG with transparency. Processing time for 10,000 photos is 1–2 hours, depending on resolution and chosen model.

Что входит в работу: deliverables

  1. Документация: полное описание архитектуры, API-спецификация, руководство оператора.
  2. Доступы: репозиторий с кодом, Docker-образ, обученные веса модели.
  3. Обучение: передача модели и скриптов, помощь в настройке операционной инфраструктуры.
  4. Поддержка: 2 недели сопровождения после внедрения, исправление ошибок.

Сроки и стоимость

Этап Срок
API-сервис на REMBG 1–2 недели
Система с SAM2 + fine-tuning 3–5 недель
Полный pipeline с matting и QA 5–8 недель

Cost is calculated individually—depends on data volume, required speed, and quality. We evaluate the project within 1–2 days after reviewing your images. Contact us for a consultation—we'll select the optimal architecture for your budget. Order development of an AI pipeline for your catalog today.

Typical project cost ranges from $3,000 to $10,000 depending on complexity, with clients reporting 70-80% reduction in manual editing costs.

Case study: A fashion retailer with 20,000 product images reduced manual background removal time from 3 weeks to 2 hours, achieving 97% accuracy with our pipeline.