MATLAB Answers

Row-wise labelling with bwlabel() and regionprops

4 views (last 30 days)
no zoop
no zoop on 25 Jan 2021
Commented: no zoop on 1 Feb 2021
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?

Answers (1)

yanqi liu
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
  1 Comment
no zoop
no zoop on 1 Feb 2021
Pretty awesome peice of code to label the binary images. However, when cropping it ends up cropping the entire row (12 rows croped) instead instead of each individual blob in a row in the entire image (~194 blobs). Do you have any suggestions? Below is the code I use to crop blobs.
message = sprintf('Would you like to crop out and save each individual images?');
reply = questdlg(message, 'Extract Individual Images?', 'Yes', 'No', 'Yes');
% Note: reply will = '' for Upper right X, 'Yes' for Yes, and 'No' for No.
if strcmpi(reply, 'Yes')
for n = 1 : non_binary_images
for k = 1 : number(n) % Loop through all blobs.
% Find the bounding box of each blob.
thisBlobsBoundingBox = blobMeasurements{n}(k).BoundingBox; % Get list of pixels in current blob.
% Extract out this blob into it's own image.
subImage = imcrop(OG_images{n}, thisBlobsBoundingBox);
% Saving the cropped image
folder = 'folder';
thisBaseFileName = sprintf('name.tif', n, k);
thisFullFileName = fullfile(folder, thisBaseFileName);
imwrite(subImage, thisFullFileName,'tif');
end
end
end

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!