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

1 次查看(过去 30 天)
obj = imread('venn.png');
imshow(obj)
%%Segmenting image
%% Divide image "obj" into its respective RGB intenstites
red = obj(:,:,1);
green = obj(:,:,2);
blue = obj(:,:,3);
figure(1)
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
figure(2)
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
figure(3)
imshow(obj)
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

回答(1 个)

Image Analyst
Image Analyst 2021-11-24
编辑:Image Analyst 2021-11-24
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.';
uiwait(warndlg(message));
return;
end
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.
  2 个评论
Madhav Natarajan
Madhav Natarajan 2021-11-24
编辑:Madhav Natarajan 2021-11-24
thankyou for replying! the diameter optimisation works great!
but now i tried using a colour image and still the problem persists. i have attached a screenshot for your reference...
please note this happens only when there is more than one shape in the image, the original code works fine where there is one shape , colour or BW, which makes me puzzled. bottom left corner reference image
Image Analyst
Image Analyst 2021-11-24
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.';
uiwait(warndlg(message));
return;
end
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Orange 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by