- https://www.mathworks.com/help/images/ref/bwboundaries.html (openExample('images/DisplayObjectBoundariesInRedAndHoleBoundariesInGreenExample'))
- https://www.mathworks.com/help/images/ref/imclose.html
How to find the width of this object at its center?
1 view (last 30 days)
Show older comments
As you can see my object is a bit irregular in shape with bigger width at top and bottom, i would like to find out the width of the object at its center (or better smallest width in the central location).
I have used regionprops and bounding box, but it only output the max width.
Thanks in advanced..![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/329105/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/329105/image.jpeg)
0 Comments
Answers (1)
Prabhan Purwar
on 13 Jul 2020
Edited: Prabhan Purwar
on 13 Jul 2020
Hi,
Following code may help
Change the threshold value and Morphological parameters according to your need.
clc
close all
clear
img = imread('image.jpeg');
grayImage = rgb2gray(img);
figure
imshow(grayImage);
[pixelCount, grayLevels] = imhist(grayImage);
figure
bar(pixelCount);
% threshold (binarize) the image.
binaryImage = grayImage > 20;
% Get rid of small blobs:
binaryImage = bwareaopen(binaryImage, 500);
% Morphologically close image
binaryImage = imclose(binaryImage, ones(15));
figure
imshow(binaryImage);
hold on
[boundaries, L, N, A] = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
% Find out which are interior boundaries
enclosed_boundaries = find(A(:,1));
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
if ismember(enclosed_boundaries, k)
% It's not an enclosed boundary - it's an outer one. Plot in red.
plot(thisBoundary(:,2), thisBoundary(:,1), 'r', 'LineWidth', 3);
else
% It's an enclosed boundary. Plot in green.
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 3);
end
end
hold off
Kindly have a look at the following links for further references:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!