Is the function regionprops useful for counting small regions ?
6 views (last 30 days)
Show older comments
Luiz Felipe Fernandes Stipp
on 22 Jul 2020
Commented: Image Analyst
on 24 Jul 2020
Hello, I'm making a project with image processing. My new goal is to clear all the small pixels (noise).
I've found regionprops as a good tool that helped me throw the first part of my project.
During the second part of the project I've used the function:
BW = im2bw (image); % Saving as BW image
no_hole = imfill(BW,'holes'); % Filling holes
new = no_hole-BW; % See what was filled
region=regionprops(new,'Area'); % Count all the "holes filled"
object=sort([region.Area]); % Create a vector with size of "holes filled"
number=length(object) % Response to see the number of areas
My problem is that regionprops doesn't actually counts all my objects separately, he counts them all as if they were all together.
"number" is always storing 1. "Region" is saved as 1x1struct.
By the way here's the image: imshow (new)

In the counterpart I've tried to use bwlabel function to count the REAL number of objects, and he returned to me 100 different objects.
[L,num]=bwlabel(new);
Is the function regionprops usefull for counting small regions like 30 pixels inside a 1300x1600 picture ?
I'm trying to clear the small pixels with find, for and if functions, but I still can't find a way to clear my noisy pixels.
Thanks for your time,
Luiz Stipp
0 Comments
Accepted Answer
Image Analyst
on 22 Jul 2020
You did something wrong before but I don't know what because all the code is not there. One thing I notices is that your image caption says "holes filled" yet the holes are not filled.
My guess is that your object was actually the inverse of that so your foreground is actually all the black stuff and there is just one of those. You'd need to invert the image to get the multiple white blobs.
Of course regionprops can be used to count regions of any size.
If you want to remove small regions, say smaller than 30 pixels you can use bwareaopen():
bw = bwareaopen(bw);
If you want to extract only blobs that are larger than 30 pixels, you can use bwareafilt() and specify an allowable size range of 31 to infinity:
bw = bwareafilt(bw, [31, inf]);
Essentially those two lines of code do the exact same thing, or (more accurately stated) they end up with the same thing when used like I used them above.
With regionprops(), it first finds connected components (regions) by scanning from top to bottom, then left to right - in other words, column-by-column as it scans across the image. Whenever it hits a white pixel, it does a regiongrowing to find the remainder of the blob, no matter how big it is or what columns it lies in. Everywhere it finds a pixels that is connected to that point, it labels it with an ID number, like 1. Then once all pixels for that blob have been labeled, it goes back to where it first found that blob and starts looking for other blobs. If it hits a pixel that has already been found and labeled, then it skips over that pixel. In that way, it will only label undiscovered blobs and not re-label a blob it has already labeled previously. Then regionprops look at each blob that has a particular label and makes its measurements, then it does the same thing for the next blob until all blobs have been analyzed. Does that explain it?
2 Comments
Image Analyst
on 24 Jul 2020
regionprops() works with binary (logical) images as well as labeled images. Perhaps because new was a built-in function???
>> which -all new
C:\Program Files\MATLAB\R2020a\toolbox\shared\spcuilib\unifiedscopes\@uiscopes\new.m % uiscopes method
Or else your new was not a logical image. If you use whos, it should show it as logical
whos new
More Answers (1)
See Also
Categories
Find more on Graph and Network Algorithms 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!