MATLAB: How to Fill a segmented image

boundariescontourdigital image processingfillImage Processing Toolboximage segmentation

Im trying to segment a photo and the program only segmentates a part of it, what do i have to add to segment the whole photo. Im trying to fill the segmented image but it fills a part, not the complete photo, this is the code that im using:
clear all clc
IO=imread('FotoJuan.jpg'); IOG=rgb2gray(IO);
[~, threshold] = edge(IOG, 'sobel'); fudgeFactor = .5; BWs = edge(IOG,'sobel', threshold * fudgeFactor);
se90 = strel('line', 2, 90); se0 = strel('line', 3, 0); BWsdil = imdilate(BWs, [se90 se0]);
BWdfill = imfill(BWsdil, 'holes');
% BWnobord = imclearborder(BWdfill, 4); % % seD = strel('line', 1, 90); % BWfinal = imerode(BWnobord,seD); % BWfinal = imerode(BWfinal,seD);
etiqueta = bwlabel(BWdfill, 4); numObjetos = max(max(etiqueta)); numObjetos
subplot(2,2,1); imshow(IO); title('Imagen Original');
subplot(2,2,2); imshow(IOG); title('Imagen en escala de grises');
subplot(2,2,3); imshow(BWdfill); title('Dilated Gradient Mask');
Im going to attach the original photo and the one who shows how is segmented, Hope you can help me 🙂

Best Answer

Tell it to read in your image and trace out some part of the blue background. The demo will give you the blue background as a mask. Your person mask is simply the inverse of the blue background mask. You might call imfill() to get rid of any little holes in the mask.