MATLAB: Remove pixel and height that doesnt meet certain criteria

blobs

hi sir, in my color detection, I used ismember to set object between pixel 100 and 500. Its worked. however, can I know from the bounding box of object detection, how to remove bounding of blobs that doesnt meet height and width criteria ,say width and height of 100×100 pixels?

Best Answer

Sure, you just get all the bounding boxes
allBB = [blobMeasurements.BoundingBox];
Then extract every 4th one for the widths and heights.
allWidths = allBB(3:4:end);
allHeights = allBB(4:4:end);
Then use ismember just like you did to filter based on area. You'd do something like this:
% Get a list of the blobs that meet our criteria and we need to keep.
allowableWidthIndexes = (allWidths > 150) & (allWidths < 220);
allowableHeightIndexes = allHeights < 2000; % Take the small objects.
keeperIndexes = find(allowableWidthIndexes & allowableHeightIndexes );
% Extract only those blobs that meet our criteria, and
% eliminate those blobs that don't meet our criteria.
% Note how we use ismember() to do this.
keeperBlobsImage = ismember(labeledImage, keeperIndexes);
You can then relabel keeperBlobsImage and call regionprops on keeperBlobsImage if you want to measure only the keeper blobs.