![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/505938/image.png)
Row-wise labelling with bwlabel() and regionprops
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
yanqi liu
2021-2-1
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/505938/image.png)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!