How to Add a Mustache to Faces Using PHP and OpenCV – A Fun Image‑Processing Tutorial

This tutorial walks through using PHP with OpenCV to detect faces, noses, and mouths, filter out false detections, and programmatically overlay a mustache image onto the identified region, complete with step‑by‑step algorithms, debugging tips, and visual results.

21CTO
21CTO
21CTO
How to Add a Mustache to Faces Using PHP and OpenCV – A Fun Image‑Processing Tutorial

Inspired by the popularity of Microsoft’s age‑recognition software, the author decided to explore the capabilities of PHP combined with OpenCV to create a playful “mustache‑sticker” application.

The first step is face detection using the haarcascade_frontalface_alt.xml classifier. The detection returns coordinates such as

Array ( [0] => Array ( [x] => 115 [y] => 190 [w] => 278 [h] => 278 ) )

, which are then visualized as lines.

Subsequent detections target the nose ( haarcascade_mcs_nose.xml) and mouth ( haarcascade_mcs_mouth.xml) regions. The author notes multiple detections (e.g., three noses on one face, five on another) and uses a series of exclusion rules to isolate the true nose and mouth:

Discard detections outside the upper‑left corner of the face.

Remove detections below the face boundary.

Exclude detections beyond the right edge of the face.

Eliminate detections whose vertical line crosses the nose’s central line.

Eliminate detections whose horizontal line crosses the nose’s central line.

Discard mouths above the nose.

Remove detections whose bottom line lies below the face.

After filtering, the accurate face, nose, and mouth coordinates are obtained, as illustrated by the accompanying images.

To place the mustache, the algorithm computes the middle line between the nose and mouth, determines the mustache’s width as 1.8 times the mouth’s width (based on empirical testing of 100 mustaches), scales the mustache image accordingly, and calculates the final coordinates:

X = nose_x – mustache_width / 2; Y = (nose_center_y + mouth_center_y) / 2 – scaled_mustache_height / 2;

The mustache image is then overlaid at these coordinates, completing the effect.

Additional screenshots show the debugging information being removed and the final “stylized” interface with various mustache styles (100 different mustaches were prepared for demonstration).

The article concludes by inviting readers to try the program, experiment with different faces, and share the results.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Computer VisionPHPOpenCVMustache Overlay
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.