How to threshold an image when background and foreground color are similar to each other?

6 views (last 30 days)
Hi i would like to mask the background in this image so i have only the face of this baby as the output. But the background colors are very similar to the face skin color so when I'm applying thresholding im also losing the portion of the face or including the region that are unnecesary. How can I tackle this issue? What pre processing techniques should use? Or should take a different approach to the problem? What would you suggest?
Thanks in advance.

Accepted Answer

yanqi liu
yanqi liu on 2 Dec 2021
clc; clear all; close all;
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/819834/image.png');
% method 1
jmg = rgb2ycbcr(img);
s = jmg(:,:,2);
bw = ~imbinarize(s);
bw = bwareafilt(imopen(bw, strel('disk', round(size(bw,2)/5))), 1);
stats = regionprops(bw);
figure;
subplot(2, 2, 1); imshow(img);
subplot(2, 2, 2); imshow(s, []);
subplot(2, 2, 3); imshow(bw, []);
subplot(2, 2, 4); imshow(img, []);
hold on; rectangle('position', stats(1).BoundingBox, 'EdgeColor', 'g', 'LineWidth', 2)
% method 2
faceDetector = vision.CascadeObjectDetector();
faceDetector.ScaleFactor = 1.01;
bbox = step(faceDetector, img);
figure;
imshow(img);
hold on; rectangle('position', bbox(1,:), 'EdgeColor', 'g', 'LineWidth', 2)
  3 Comments
yanqi liu
yanqi liu on 2 Dec 2021
yes,sir
The main idea is to transform the color space and extract the skin color region. The face has obvious continuous skin color characteristics, and there are face edges around. Therefore, although the background is partially repeated, the face is still a relatively large region. Through morphological filtering, the interference of the background can be eliminated and the face candidate region can be obtained. Of course, other color space analysis can also be considered.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!