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