A MATLAB-based face detection pipeline that segments skin regions using color-space thresholds derived from Thakur et al. (2011) and refines candidate regions through morphological operations and blob analysis. The system evaluates seven different color-space combinations across RGB, YCbCr, and HSV and reports IoU-based detection metrics.
This project implements a classical (non-deep-learning) approach to face detection. The core idea is straightforward:
- Classify every pixel as skin or non-skin using threshold rules in multiple color spaces.
- Clean up the resulting binary mask with morphological opening/closing and hole filling.
- Extract connected components (blobs) and filter them by area, aspect ratio, and eccentricity to keep only face-like regions.
- Evaluate detections against YOLO-format ground truth using Intersection over Union (IoU).
Seven color-space rule combinations are tested to determine which produces the best precision, recall, and F1 score.
The sample input image (images/image1.jpg) used for evaluation:
The pixel-level skin classification rules are adapted from:
Thakur, S., Paul, S., Mondal, A., Das, S., & Abraham, A. (2011). Face detection using skin tone segmentation. 2011 World Congress on Information and Communication Technologies, pp. 53–60. IEEE. doi:10.1109/WICT.2011.6141217
Three independent rules are defined:
| Rule | Color Space | Conditions |
|---|---|---|
| A (RGB) | RGB | R > 50, G > 40, B > 20, max–min channel difference > 10, ` |
| B (CbCr) | YCbCr | 60 ≤ Cb ≤ 130 and 130 ≤ Cr ≤ 165 |
| C (HS) | HSV | 0 ≤ H ≤ 50/360 and 0.1 ≤ S ≤ 0.9 |
| # | Combination | Formula |
|---|---|---|
| 1 | RGB only | A |
| 2 | CbCr only | B |
| 3 | HS only | C |
| 4 | RGB + CbCr | A ∩ B |
| 5 | RGB + HS | A ∩ C |
| 6 | CbCr + HS | B ∩ C |
| 7 | RGB + HS + CbCr | A ∩ B ∩ C |
Each binary skin mask passes through:
- Hole filling —
imfill(mask, 'holes') - Opening — removes small noise (disk structuring element, radius 5)
- Closing — bridges narrow gaps
Connected components are accepted as face candidates only if:
- Area ≥ 1 500 px
- Aspect ratio (width / height) ∈ [0.4, 1.1]
- Eccentricity ∈ [0.25, 0.97]
Detections are matched to ground truth bounding boxes via greedy IoU matching (threshold = 0.3). The following metric is reported:
- Mean IoU (mIoU) over matched pairs — measures both detection correctness and bounding-box overlap quality
Note: With a single test image containing only one face, metrics like TP/FP/FN, precision, recall, and F1-score are trivially 0 or 1 and not statistically meaningful. mIoU alone is sufficient: a value of 0 means no face was detected; a positive value reflects both correct detection and localization quality.
morphological-face-detection/
├── src/
│ └── main.m # Main MATLAB script (all-in-one pipeline)
├── images/
│ └── image1.jpg # Input test image
├── output/ # Generated outputs (masks, results, report)
│ ├── 00_original_image.png
│ ├── 01–07_*_a_raw_mask.png
│ ├── 01–07_*_b_after_morphology.png
│ ├── 01–07_*_result.png
│ └── performance_report.txt
├── README.md
└── LICENSE
- MATLAB R2019b or later (Image Processing Toolbox required)
rgb2hsv,rgb2ycbcr,imfill,imopen,imclose,strel,regionprops
-
Clone the repository
git clone https://github.com/kutmur/morphological-face-detection.git cd morphological-face-detection -
Place input images in the
images/directory (a sampleimage1.jpgis already included). -
Run the pipeline from MATLAB:
cd src main
-
Check outputs in the
output/directory:- Individual channel visualizations (R, G, B, H, S, Cb, Cr)
- Raw and morphologically processed skin masks for each combination
- Detection result images (red = detected, green = ground truth)
performance_report.txt— summary performance table
Performance on image1.jpg (IoU threshold = 0.30):
| Model | mIoU |
|---|---|
| RGB only (A) | 0.3751 |
| CbCr only (B) | 0.0000 |
| HS only (C) | 0.3733 |
| RGB + CbCr (A∩B) | 0.3751 |
| RGB + HS (A∩C) | 0.3861 |
| CbCr + HS (B∩C) | 0.3733 |
| RGB + HS + CbCr (A∩B∩C) | 0.3861 |
The RGB + HS and RGB + HS + CbCr combinations achieved the highest mean IoU (0.3861), correctly detecting the face with the best localization quality.
The figure below traces the best-performing combination — RGB + HS (A ∩ C) through each stage of the pipeline, showing how each step progressively narrows down the skin regions to a precise face bounding box.
Green box = ground truth bounding box · Red box = detected face
Stage-by-stage explanation:
- Skin classification — Each pixel is tested against the RGB rule (A) and the HS rule (C). Only pixels satisfying both rules are marked white. This produces a noisy binary mask with gaps and isolated specks.
- Morphological cleanup — Hole filling (
imfill) closes interior gaps. Opening with a disk structuring element (radius 5) removes small noise specks. Closing bridges narrow gaps in the skin region, producing a smooth, solid skin blob. - Blob filtering —
regionpropsextracts every connected component. Blobs that meet all geometric constraints (area, aspect ratio, eccentricity) are accepted as face candidates and drawn as red bounding boxes. - Evaluation — Detected boxes are compared to ground-truth boxes via greedy IoU matching (threshold = 0.3). Green boxes show the ground truth.
The RGB + HS + CbCr (A ∩ B ∩ C) combination produced an equally high IoU (0.3861) and follows the identical pipeline pattern — see 07_*_a_raw_mask.png, 07_*_b_after_morphology.png, 07_*_result.png in the output/ directory.
The test images are sourced from the Face Detection Dataset on Kaggle:
Fares Elmenshawii. Face Detection Dataset. Kaggle. https://www.kaggle.com/datasets/fareselmenshawii/face-detection-dataset
The dataset provides images with YOLO-format bounding box annotations used as ground truth for evaluation.
-
S. Thakur, S. Paul, A. Mondal, S. Das, and A. Abraham, "Face detection using skin tone segmentation," 2011 World Congress on Information and Communication Technologies, pp. 53–60, Dec. 2011. doi:10.1109/WICT.2011.6141217
-
F. Elmenshawii, "Face Detection Dataset," Kaggle. [Online]. Available: https://www.kaggle.com/datasets/fareselmenshawii/face-detection-dataset
This project is licensed under the MIT License — see the LICENSE file for details.




