# Finding the width of a blob perpendicular to the major axis

5 views (last 30 days)
Muhammad Syahmeer on 8 Dec 2022
Commented: Image Analyst on 10 Dec 2022
Hi!
I've got a binarised blob here with a major axis plotted inside the blob. I am looking to find a series of width, perpendicular to the major axis plotted in the blob. Basically, I'm looking to find the distance of a line that has a starting coordinate from the left side of boundary, intersects with the coordinate of the major axis and ending with a coordinate from the right side of the boundary. The image below shows what I'm looking for. Apologies if my question isn't clear enough.
Basically I'm looking to measure the distance of the black arrows. Attached is the code I've used to draw the magenta coloured line (major axis) of the blob.
Really appreciate if someone can help me to solve this problem.
Cheers,
How

DGM on 9 Dec 2022
Unless you have some existing means to find the major axis, I'd still use feret properties to rotate the object. The rest of this really isn't much different than IA's answer, though.
% cropped screenshot with annotations removed
% pick the largest blob if desired
% otherwise, the script processes all blobs
% get properties
th = vertcat(S.MinFeretAngle)-180;
% preallocate outputs
blobimgs = cell(numel(S),1);
blobwidth = cell(numel(S),1);
for k = 1:numel(S)
% rotate blob image
thisblob = imrotate(S(k).Image,th(k));
% close-crop blob image
yrange = any(thisblob,2);
thisblob = thisblob(yrange,:);
xrange = any(thisblob,1);
thisblob = thisblob(:,xrange);
% throw results into output arrays
blobimgs{k} = thisblob;
blobwidth{k} = sum(thisblob,2);
end
% view the blob and width profile
f = 1; % select a blob
subplot(2,1,1)
imshow(blobimgs{f})
subplot(2,1,2)
plot(blobwidth{f})
Muhammad Syahmeer on 9 Dec 2022
Edited: Muhammad Syahmeer on 9 Dec 2022
Thanks for that @Image Analyst. Yup on my way to get the latest version installed.
I used the “orientation” option from regionprops like you suggested and subtracted it with 90 degrees to get the image vertical. Though wasn’t sure why 90 is the magic number but it seems to do the trick..
Image Analyst on 10 Dec 2022
@Muhammad Syahmeer With regionprops the Orientation value is in degrees, ranging from -90 degrees to 90 degrees. If you want a different range, you'd have to add the offset. For example to go from -90 to +90 to 0 to 180, you'd have to add 90 to the Orientation value.

Image Analyst on 8 Dec 2022
##### 3 CommentsShow 1 older commentHide 1 older comment
Image Analyst on 9 Dec 2022
Then use regionprops() to get the 'Image' and the 'Orientation'. Then rotate the blob so that it's vertical and then scan down line by line using find() or nnz() to count the white pixels on each line. I think you can do it. Something like
for k = 1 : numel(props)
thisImage = props(k).Image;
thisImage = imrotate(thisImage, props(k).Orientation);
[rows, columns] = size(thisImage);
widths = nan(rows, 1), 1);
for row = 1 : rows
widths(row) = nnz(thisImage(row, :));
end
end
If that's not right, I'm sure you can debug it.
Muhammad Syahmeer on 9 Dec 2022
Thanks so much for the code Image Analyst. That's really helpful!

### Categories

Find more on Image Segmentation and Analysis in Help Center and File Exchange

R2018b

### Community Treasure Hunt

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

Start Hunting!