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

7 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

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!

Translated by