Insufficient number of outputs

1 次查看(过去 30 天)
elie lattouf
elie lattouf 2020-11-18
编辑: elie lattouf 2020-11-19
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 个评论
Walter Roberson
Walter Roberson 2020-11-19
if numel(f6) ~= 1
error("expected exactly 1 region, got %d", numel(f6))
end
elie lattouf
elie lattouf 2020-11-19
how to fix it in the full code ?

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2020-11-19
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

Community Treasure Hunt

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

Start Hunting!

Translated by