This example shows how to remove small objects from an image by using its’ size. (thanks Janifal for contributing the sample image)
- First, read the image into Scilab and label the objects found on the image. If the input image is in grayscale, it will be converted to binary with the threshold of 0.5.
–> S = imread(removeobjects.jpg’);
–> [S2 n] = imlabel(S);
- Check how many objects found.
- Visualize the labelled image with colormap.
–> f = gcf();
–> f.children.children.image_type =’index’;
–> f.color_map = hsvcolormap(128);
- We could also draw the bounding boxes around the detected objects.
–> [A, BB, ctr] = imblobprop(S2);
–> imshow(S);imrects(BB,[0 255 0]);
- Check the distribution of objects’ size with histrogram
- Let’s pick the value of 1000, in which the objects with the size smaller than 1000 will be filtered out.
–> ind = find(A<1000);
–> Snew = S;
–> for cnt = 1:size(ind,2)
> Snew(S2==ind(cnt)) = 0;
- Show the new image