Relabelling with bwlabel()

1 view (last 30 days)
no zoop
no zoop on 28 Oct 2019
Edited: no zoop on 30 Oct 2019
Hi, I want my code to label the images in the collage by row instead of by column. I have come pretty close to doing this. The images I have attached are the original image and the labeled image. In the labeled image you can see in the first row the first image is labeled as 2 and if you follow that the image labeled 1 is the 10th position. I am not sure how to fix it. Below is my code...any insight?
My code is formatted to do this for multiple images at a time, I want to automate this process.
% Read in folder of binary images
image_folder_binary = ' folder '; % Enter name of folder from which you want to upload pictures with full path
filenames_bin = dir(fullfile(image_folder_binary, '*.tif')); % read all images with a sppecified extention, its tif in our case
binary_images = numel(filenames_bin); % count total number of photos present in that folder
%%
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
L = bwlabel(our_images_binary,8)
L = bwlabel(L')'
blobMeasurements {n} = regionprops(L, our_images_binary, 'all');
numberOfBlobs(n) = size(blobMeasurements{n}, 1);
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
% Plot the borders of all the zoop on the original grayscale image using the coordinates returned by bwboundaries.
boundaries = bwboundaries(our_images_binary);
numberOfBoundaries(n) = size(boundaries, 1);
end
%%
for n = 1 : binary_images
% Loop over all blobs printing their measurements to the command window.
for k = 1 : numberOfBlobs(n) % Loop through all blobs.
% Find the mean of each blob. (R2008a has a better way where you can pass the original image
% directly into regionprops. The way below works for all versions including earlier versions.)
thisBlobsPixels = blobMeasurements{n}(k).PixelIdxList; % Get list of pixels in current blob.
meanGL = mean(our_images_binary(thisBlobsPixels)); % Find mean intensity (in original image!)
meanGL2008a = blobMeasurements{n}(k).MeanIntensity; % Mean again, but only for version >= R2008a
blobArea = blobMeasurements{n}(k).Area; % Get area.
blobPerimeter = blobMeasurements{n}(k).Perimeter; % Get perimeter.
blobCentroid = blobMeasurements{n}(k).Centroid; % Get centroid one at a time
blobECD{n}(k) = sqrt(4 * blobArea / pi); % Compute ECD - Equivalent Circular Diameter.
fprintf(1,'image #%03d blob #%03d %17.1f %11.1f %8.1f %8.1f %8.1f % 8.1f\n', n, k, meanGL, blobArea, blobPerimeter, blobCentroid, blobECD{n}(k));
end
end
%%
for n = 1 : binary_images
for k = 1 : numberOfBlobs(n)
boxes = cat(1, blobMeasurements{n}.BoundingBox);
left_edge = boxes(:,1);
[sorted, sort_order] = sort(left_edge);
s2 = blobMeasurements{n}(sort_order);
end
end
%%
for n = 1 : binary_images
I = im2uint8(our_images_binary);
I(~our_images_binary) = 200;
end
%% option 1, which is like 99% good
extrema = cat(1, blobMeasurements{n}.Extrema);
left_most_top = extrema(1:8:end, :);
[sorted, sort_order] = sortrows(fliplr(left_most_top));
s2 = blobMeasurements{n}(sort_order);
imshow(I, 'InitialMag', 'fit')
hold on
for k = 1:numel(s2)
centroid = s2(k).Centroid;
text(centroid(1), centroid(2), sprintf('%d', k));
end
hold off

Answers (1)

Image Analyst
Image Analyst on 28 Oct 2019
You need to call sort(). Instead of
L = bwlabel(our_images_binary,8)
L = bwlabel(L')'
blobMeasurements {n} = regionprops(L, our_images_binary, 'all');
numberOfBlobs(n) = size(blobMeasurements{n}, 1);
have this
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;
  3 Comments
Image Analyst
Image Analyst on 28 Oct 2019
Yes, just replace the lines of code with the new ones I gave.
You could use ismember() to loop through the blobs to get each blob one at a time and them show it with imshow() and you should see them marching down the screen from top to bottom.
no zoop
no zoop on 30 Oct 2019
Edited: no zoop on 30 Oct 2019
Unfortunatley it still doesn't order them correctly. The end game is if I can order them along a row than I can crop them along the wrong (by taking measurements along the row). Below is my code in what I think you are trying to explain to me.
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
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;
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
% Plot the borders of all the zoop on the original grayscale image using the coordinates returned by bwboundaries.
boundaries = bwboundaries(our_images_binary);
numberOfBoundaries(n) = size(boundaries, 1);
end
%%
for n = 1 : binary_images
% Loop over all blobs printing their measurements to the command window.
for k = 1 : numberOfBlobs(n) % Loop through all blobs.
% Find the mean of each blob. (R2008a has a better way where you can pass the original image
% directly into regionprops. The way below works for all versions including earlier versions.)
thisBlobsPixels = blobMeasurements{n}(k).PixelIdxList; % Get list of pixels in current blob.
meanGL = mean(our_images_binary(thisBlobsPixels)); % Find mean intensity (in original image!)
meanGL2008a = blobMeasurements{n}(k).MeanIntensity; % Mean again, but only for version >= R2008a
blobArea = blobMeasurements{n}(k).Area; % Get area.
blobPerimeter = blobMeasurements{n}(k).Perimeter; % Get perimeter.
blobCentroid = blobMeasurements{n}(k).Centroid; % Get centroid one at a time
blobECD{n}(k) = sqrt(4 * blobArea / pi); % Compute ECD - Equivalent Circular Diameter.
fprintf(1,'image #%03d blob #%03d %17.1f %11.1f %8.1f %8.1f %8.1f % 8.1f\n', n, k, meanGL, blobArea, blobPerimeter, blobCentroid, blobECD{n}(k));
end
end
%%
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 : original_images
for k = 1 : numberOfBlobs(n) % Loop through all blobs.
% Find the bounding box of each blob.
thisBlobsBoundingBox = s2(k).BoundingBox; % Get list of pixels in current blob.
% Extract out this zoop into it's own image.
subImage = imcrop(OG_images{j}, thisBlobsBoundingBox);
% Saving the cropped image
folder = 'folder';
thisBaseFileName = sprintf('Image%03d_%03d.tif', n, k);
thisFullFileName = fullfile(folder, thisBaseFileName);
imwrite(subImage, thisFullFileName,'tif');
end
end
end

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!