MATLAB: Removing objects which have area greater and lesser than some threshold areas and extracting only the objects which have the area in between

Image Processing Toolboxremoving small and larger objects at a time?

Hi, I am doing a project on nighttime vehicle detection. I have extracted bright objects from the input traffic image.I have to remove smaller objects(noise) and larger objects(reflections) when compared to vehicle headlight objects. I have removed small objects using "sterl" function. How can i remove larger objects? Please help me in extracting based on the area of these objects. I have to extract only the in between area objects.
Thank you sir.

Best Answer

Given some image, for example (just to make a random image):
I = conv2(randn(500),ones(10)) > 10;
imshow(I);
To keep only objects between, say, 30 pixels and 50 pixels in area, you can use the BWAREAOPEN command, like this:
LB = 30;
UB = 50;
Iout = xor(bwareaopen(I,LB), bwareaopen(I,UB));
figure, imshow(Iout);