Labeling image region properties
Show older comments
I have a binary image, and I'd like to create a corresponding mask that contains a value in each pixel for the area of each region. I can do this with bwlabel and regionprops, as shown below, but the loop is extremely slow for a very large binary image. It's just a matter of indexing, so surely there's a way to vectorize my loop?
This code creates the region_area mask that I'm aiming for--Notice the periods have low values, while the bigger letters like m have much larger areas. I'm just seeking ideas for how to speed this up.
% Load a binary image:
BW = imread('text.png');
% Label regions:
[L,Ln] = bwlabel(BW);
% Get the area of each labeled region:
stats = regionprops(L,'Area');
% Create a mask:
region_area = zeros(size(L));
for k = 1:Ln
region_area(L==k) = stats(k).Area;
end
% Display the results:
imagesc(region_area)
axis image
cb = colorbar;
ylabel(cb,'Region area (# of pixels)')
Accepted Answer
More Answers (0)
Categories
Find more on Images 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!