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

조회 수: 13 (최근 30일)
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.

채택된 답변

yanqi liu
yanqi liu 2021년 12월 2일
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
yanqi liu
yanqi liu 2021년 12월 2일
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.
Oguzhan Guvercin
Oguzhan Guvercin 2021년 12월 3일
Thank you sincerely sir. I appreciate for your reply and time.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by