Relabelling with bwlabel()
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Image Analyst
2019-10-28
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 个评论
Image Analyst
2019-10-28
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!