필터 지우기
필터 지우기

Row-wise labelling with bwlabel() and regionprops

조회 수: 1 (최근 30일)
no zoop
no zoop 2021년 1월 25일
댓글: no zoop 2021년 2월 1일
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?

답변 (1개)

yanqi liu
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
  댓글 수: 1
no zoop
no zoop 2021년 2월 1일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by