MATLAB: How to detect only object without background

MATLAB

Best Answer

Find out where all color channels are darker than some threshold, then and the masks and take the largest blob
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Threshold to create mask for each channel.
threshold = 20; % Whatever...
mask = (redChannel < threshold) & (greenChannel < threshold) & (blueChannel < threshold);
backgroundMask = bwareafilt(mask, 1); % Take largest blob
footMask = ~backgroundMask; % The inverse of the background mask.