RGB = imread('peppers.png');
figure;
imshow(RGB)
h1 = impoly(gca,[34,298;114,140;195,135;259,200;392,205;467,283;483,104],'Closed',false);
foresub = getPosition(h1);
foregroundInd = sub2ind(size(RGB),foresub(:,2),foresub(:,1));
figure;
imshow(RGB)
h2 = impoly(gca,[130,52;170,32],'Closed',false);
backsub = getPosition(h2);
backgroundInd = sub2ind(size(RGB),backsub(:,2),backsub(:,1));
L = superpixels(RGB,500);
BW = lazysnapping(RGB,L,foregroundInd,backgroundInd);
maskedImage = RGB;
maskedImage(repmat(~BW,[1 1 3])) = 0;
figure;
imshow(maskedImage)
Best Answer