MATLAB Answers

Insufficient number of outputs

2 views (last 30 days)
elie lattouf
elie lattouf on 18 Nov 2020
Edited: elie lattouf on 19 Nov 2020
I'm using this code to capture the cap of a bottle. It's working if the cap is on the bottle, but it's not working when i remove the cap because by doing imcrop on the cap there is not a white area in the figure and therefore no level detection. What should I do to fix my error? Below is my code:
a=imread('emptybottle.png');
b=rgb2gray(a);
f1 = imcrop(b,[400 0 200 130]);
imhist(f1);
f2=im2bw(f1,0.78);
f3=~f2;
imshow(f3);
f4=bwareaopen(f3,250);
mask=strel('diamond',5);
f5=imopen(f4,mask);
imshow(f5);
g=bwareaopen(f5,8000); % remove the cap
imshow(g);
f6=regionprops(g) % show the properties of the white area
level = f6.Area % saves the number of pixels inside the white area
level

  4 Comments

Show 1 older comment
elie lattouf
elie lattouf on 19 Nov 2020
what do you mean ? I don't understand.
Walter Roberson
Walter Roberson on 19 Nov 2020
if numel(f6) ~= 1
error("expected exactly 1 region, got %d", numel(f6))
end
elie lattouf
elie lattouf on 19 Nov 2020
how to fix it in the full code ?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 19 Nov 2020
a=imread('emptybottle.png');
b=rgb2gray(a);
f1 = imcrop(b,[400 0 200 130]);
imhist(f1);
f2=im2bw(f1,0.78);
f3=~f2;
imshow(f3);
f4=bwareaopen(f3,250);
mask=strel('diamond',5);
f5=imopen(f4,mask);
imshow(f5);
g=bwareaopen(f5,8000); % remove the cap
imshow(g);
f6=regionprops(g) % show the properties of the white area
if numel(f6) ~= 1
error("expected exactly 1 region, got %d", numel(f6));
end
level = f6.Area % saves the number of pixels inside the white area
level

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!