how to find diameter of multiple shapes in one image? i get the error message index exceeds area bounds

4 views (last 30 days)
Madhav Natarajan
Madhav Natarajan on 24 Nov 2021
Commented: Image Analyst on 24 Nov 2021
obj = imread('venn.png');
%%Segmenting image
%% Divide image "obj" into its respective RGB intenstites
red = obj(:,:,1);
green = obj(:,:,2);
blue = obj(:,:,3);
subplot(2,2,1); imshow(obj); title('Original Image');
subplot(2,2,2); imshow(red); title('Red Plane');
subplot(2,2,3); imshow(green); title('Green Plane');
subplot(2,2,4); imshow(blue); title('Blue Plane');
%Threshold the blue plane
level = 0.37;
bw2 = imbinarize(blue,level);
subplot(2,2,1);imshow(bw2); title('Blue plane threshold')
%%Remove Noise
%%Fill any holes
fill = imfill(bw2, 'holes');
subplot(2,2,2); imshow(fill); title('Holes Filled');
%%Remove any blobs on the border of the image
clear = imclearborder(fill);
subplot(2,2,3); imshow(clear); title('Remove blobs on border');
%%Remove blobs that are smaller than 7 pixels across
se = strel('disk',7);
open = imopen(fill,se);
subplot(2,2,4); imshow(clear); title('Remove small blobs')
%% Measure Object Diameter
diameter = regionprops(open,'MajorAxisLength');
%%Show result
d = imdistline; %%Include a line to physically measure the ball
so this in this code, i am able to find pixel diameters of the images which have one circle or one square in them but when i try to use images with two shapes in them, such as a venn diagram or just three random shapes in the image, i get this "Index in position 3 exceeds array bounds (must not exceed 1)"
please help asap

Answers (1)

Image Analyst
Image Analyst on 24 Nov 2021
Edited: Image Analyst on 24 Nov 2021
You're opening a gray scale image so when you do
green = obj(:,:,2);
it throws an error because there is no second color channel. Before that do this
if ndims(obj) == 1
message = 'Error because this image is gray scale not color.';
Also you could also compute diameters like this:
props = regionprops(open,'EquivDiameter');
allDiameters = 2 * [props.EquivDiameter]
This is the diameter of a circle with the same area as your blob, so it essentially fits a circle to it. MajorAxisLength fits an ellipse to it.
Image Analyst
Image Analyst on 24 Nov 2021
Looks like your mask is all black meaning your threshold was not chosen correctly. You should put in a check to see that the max of the mask image is 1, not zero
if max(open(:)) == 0
message = 'Error because no mask was found.';
Not sure why you're doing an opening. That's used when you want to clip off little tendrils from your blobs. But if your blobs are really thin it can make them, or parts of them, disappear.
I also wouldn't use "open" as the name of your variable since it's a built-in function. Call it openedImage instead of open.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!