AI Hairstyle Try-On: Segmentation, Coloring, Generation

We design and deploy artificial intelligence systems: from prototype to production-ready solutions. Our team combines expertise in machine learning, data engineering and MLOps to make AI work not in the lab, but in real business.
Showing 1 of 1All 1566 services
AI Hairstyle Try-On: Segmentation, Coloring, Generation
Medium
~2-4 weeks
Frequently Asked Questions

AI Development Areas

AI Solution Development Stages

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1317
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1226
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    925
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1156
  • image_logo-advance_0.webp
    B2B Advance company logo design
    620
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    894

A client walks into a salon, shows a Pinterest haircut photo, and within 10 seconds sees themselves with that hairstyle on screen. No 'shall we try?', no test dyes. Virtual hairstyle try-on is one of the hardest computer vision problems. Hair has semi-transparent edges, thin strands down to 1 pixel, and any mask inaccuracy gives a 'plastic' look. Over 15+ projects for beauty-tech, we have refined a pipeline combining alpha matte segmentation, diffusion generation, and identity preservation. The result: realistic try-on of any hairstyle, be it color change or drastic shape change. Contact us to discuss your project — we'll help you find the optimal solution.

How hair segmentation works

Accurate hair mask is the foundation of everything. The problem: the hair/background boundary is blurry, semi-transparent, with individual strands 1–3 pixels wide. Standard semantic segmentation gives coarse edges that kill realism when overlaying.

Specialized models:

  • Hair-SAM / Matting Anything — segmentation with alpha matte: not a binary mask but float32 values 0..1 per pixel. On edges — fractional values representing transparency.
  • MODNet — lightweight matting model, 35fps on mobile. Optimized specifically for portraits. 3x faster than classical methods (e.g., DIM).
  • ViTMatte — transformer-based matting, higher quality on complex strands. According to the ViTMatte paper, it outperforms predecessors on Intersection over Union.
from matting import load_model, estimate_foreground_ml

model = load_model('vitmate_vit_b_pretrained.pth')

def segment_hair(image, trimap):
    # trimap: 0=definitely background, 128=border zone, 255=definitely hair
    alpha, foreground = model(image, trimap)
    return alpha  # float32 mask, 0..1

Why alpha matte is critical

Binary masks do not convey semi-transparency — resulting in sharp steps on edges. Alpha matte gives per-pixel transparency, which is crucial for overlaying hairstyles: the result looks natural, not like a sticker.

Overlaying the new hairstyle

Two approaches:

Texture transfer — take a 3D model or 2D reference of the target hairstyle, deform it to the user's head shape (face shape analysis via 3DMM), overlay with blending. Works for similar lengths and shapes. Problem: if the user has long straight hair and the target is a short bob — the original hair must be removed under the new hairstyle, requiring scalp inpainting.

Generative approach (diffusion models) — more powerful:

  1. Segment hair and scalp
  2. Describe the desired hairstyle in text: "short bob, brown, with bangs"
  3. Run inpainting through Stable Diffusion with ControlNet (conditioning on face shape and head position)
  4. Preserve the face (face preservation via IP-Adapter or InstantID)

The main challenge: preserve the person's identity when changing hairstyle. Without face preservation, the model may generate a different person with the desired hairstyle.

Why generative approach is better

The generative approach enables drastic shape changes, unlike texture transfer which is limited by original length. We use ViTMatte for matting and Stable Diffusion for inpainting — this allows creating hairstyles the person never had, with identity preservation.

Hair coloring

Simpler than shape change: change the color within the segmented hair mask. Tools:

  • Selective color transfer in LAB color space: preserve L-channel (texture), change AB (color)
  • Neural color transfer via AdaIN for complex effects (ombre, balayage, highlights)
  • Balayage/ombre: gradient mask + separate color transfer by gradient from roots to tips
import cv2
import numpy as np

def recolor_hair(image, hair_mask, target_color_lab):
    lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB).astype(float)
    # Calculate average hair color
    hair_pixels = lab[hair_mask > 128]
    mean_ab = hair_pixels[:, 1:].mean(axis=0)
    # Shift AB channels toward target color
    shift = target_color_lab[1:] - mean_ab
    lab[:, :, 1:] += hair_mask[..., None] / 255.0 * shift
    lab = np.clip(lab, 0, 255).astype(np.uint8)
    return cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)

Real-time vs photo mode

Real-time (video): MODNet for segmentation + texture-based recoloring. Limitation: complex shape changes are not realistic in real time.

Photo mode: full pipeline with ViTMatte + diffusion generation. Time: 3–15 seconds depending on method.

What's included in the work

Component Description Timeline, weeks
Segmentation module alpha matte, support for 3+ models (Hair-SAM, MODNet, ViTMatte) 2–3
Coloring module real-time, all color types, ombre, balayage 3–5
Shape change module texture transfer + diffusion inpainting 6–10
Face preservation IP-Adapter / InstantID 2–3
Integration and tests API, Web, mobile devices 2–4

Estimated turnaround for turnkey: 5–18 weeks depending on complexity. Cost is calculated individually. Time savings compared to developing from scratch — 2–3 months, allowing faster product launch. Get a consultation: contact us to evaluate your project.

Method Speed Quality Shape change
Texture transfer 0.1–0.3 s Medium Limited
Diffusion inpainting 3–15 s High Any

We guarantee matting accuracy, identity preservation, and fast integration. Experience: 15+ projects in beauty-tech. For real-time hair try-on we adapt the model to your requirements. Alpha matte hair — our expertise. Identity preservation — mandatory stage. Request a timeline estimate for your project — it takes no more than a day.