Insufficient number of outputs
1 次查看(过去 30 天)
显示 更早的评论
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
2020-11-19
if numel(f6) ~= 1
error("expected exactly 1 region, got %d", numel(f6))
end
回答(1 个)
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!