Remove small objects from binary image
The basic steps are
Determine the connected components:
CC = bwconncomp(BW, conn);
Compute the area of each component:
S = regionprops(CC, 'Area');
Remove small objects:
L = labelmatrix(CC); BW2 = ismember(L, find([S.Area] >= P));