How to reduce blob using aspect ratio?
Show older comments

.
I have a binary image with 4 blobs. 3 of them has an aspect ratio of more than 1. and 1 has aspect ratio of 1. Now I want to reduce that blobs which aspect ratio more than 1 in binary image. How could i do this. Can some one please provide a code??
A picture is provided for understanding.
2 Comments
Walter Roberson
on 18 Jul 2017
When you say "reduce" do you mean "set to black" ?
Dominic
on 18 Jul 2017
Accepted Answer
More Answers (2)
Walter Roberson
on 18 Jul 2017
1 vote
If you used regionprops() to get the BoundingBox in order to calculate the aspect ratio, then you can also ask for the pixel ID list. For each region that does not pass your aspect ratio test, assign 0 to the pixels given by those indices.
9 Comments
Dominic
on 18 Jul 2017
Walter Roberson
on 18 Jul 2017
Before discarding the roi entries with the aspect ratio you do not want, use those roi to zero parts of the array. Just remember that X corresponds to columns and Y corresponds to rows.
There could potentially be a problem if the roi overlap though.
Dominic
on 18 Jul 2017
Walter Roberson
on 18 Jul 2017
Edited: Walter Roberson
on 18 Jul 2017
unwanted_roi = roi( aspectRatio ~= 1 ,:);
for K = 1 : size(unwanted_roi,1)
this_roi = unwanted_roi(K,:);
binary_image(this_roi(2) : this_roi(2) + this_roi(4) - 1, this_roi(1) : this_roi(1) - 1) = 0;
end
Dominic
on 18 Jul 2017
Walter Roberson
on 18 Jul 2017
Could you attach the original image? The one that does not have the red rectangles on it?
Dominic
on 18 Jul 2017
Walter Roberson
on 18 Jul 2017
Change the line to
binary_image(this_roi(2) : this_roi(2) + this_roi(4) - 1, this_roi(1) : this_roi(1) + this_roi(3) - 1) = 0;
Dominic
on 19 Jul 2017
Image Analyst
on 18 Jul 2017
1 vote
Use regionprops() to ask for the bounding box. Then compute aspect ratio: width over height, and height over width and take the max (or min), whichever you're using. Then use find() to find out which blobs to keep, then use ismember() to extract only those blobs. See my Image Segmentation Tutorial for a detailed demo. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862&sort=downloads_desc
2 Comments
Dominic
on 18 Jul 2017
Image Analyst
on 18 Jul 2017
If I have more time later I'll help more, in the meantime, do this:
props = regionprops(labeledImage, 'BoundingBox');
bb = [props.BoundingBox];
allWidths = bb(3:4:end);
allHeights = bb(4:4:end);
aspectRatio = [allWidths./allHeights ; allHeights ./ allWidths]
aspectRatios = max(aspectRatio, [], 1)
compactIndexes = find(aspectRatios > 5); % or whatever.
binaryImage = ismember(labeledImage, compactIndexes);
Categories
Find more on Region and Image Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

