Row-wise labelling with bwlabel() and regionprops
2 views (last 30 days)
Show older comments
Hi, I am trying to label a collage row-wise with bwlabel() and regionprops and then crop indivudial images from that collage.
Currently I have tried using either extrema or centroids. Extrema has worked out significantly better for me.
For extrema my code is as follows:
for n = 1:binary_images
binary_Images=fullfile(image_folder_binary, filenames_bin(n).name) ; % its will specify images names with full path and extension
our_images_binary = imread(binary_Images); % read images
% Do a "hole fill" to get rid of any background pixels or "holes" inside the blobs.
our_images_binary = imfill(our_images_binary, 'holes');
L = bwlabel(our_images_binary,8)
props = regionprops(L, our_images_binary, 'all');
extrema = vertcat(props.Extrema);
left_most_top = extrema(1:8:end, :);
[sortedEX, sort_Order] = sortrows(fliplr(left_most_top));
% Re-sort props
props = props(sort_Order);
numberOfBlobs(n) = length(props);
blobMeasurements{n} = props;
end
and for centroid:
for n = 1:binary_images
binary_Images=fullfile(image_folder_binary, filenames_bin(n).name) ; % its will specify images names with full path and extension
our_images_binary = imread(binary_Images); % read images
% Do a "hole fill" to get rid of any background pixels or "holes" inside the blobs.
our_images_binary = imfill(our_images_binary, 'holes');
L = bwlabel(our_images_binary,8)
props = regionprops(L, our_images_binary, 'all');
xyCentroids = vertcat(props.Centroid);
y = xyCentroids(:, 2); % Get all the y values (row centroids of blobs)
[sortedY, sortOrder] = sort(y, 'ascend'); % Do the sort.
% Re-sort props in order of increasing y.
props = props(sortOrder);
numberOfBlobs(n) = length(props);
blobMeasurements{n} = props;
end
I've also attached the binary image and the labelled image using the extrema, any suggestion on how to label this image row-wise?
0 Comments
Answers (1)
yanqi liu
on 1 Feb 2021
clc; clear all; close all;
im = imread('binary image.png');
bw = im2bw(im);
sz = size(im);
bwt = imclose(bw, strel('line', round(sz(2)*0.2), 0));
[L, num] = bwlabel(bwt);
stats = regionprops(L);
rects = cat(1, stats.BoundingBox);
[~, ind] = sort(rects(:, 2));
figure; imshow(im, []);
for i = 1 : num
bwi = bwt;
bwi(L~=ind(i)) = 0;
bwi = logical(bwi.*bw);
statsi = regionprops(bwi);
ceni = cat(1, statsi.Centroid);
ceni = sortrows(ceni, 1);
hold on;
for j = 1 : size(ceni,1)
text(ceni(j,1), ceni(j,2), sprintf('%d-%d', i, j),'Color','r')
end
end
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!