matlab图像分割二值化开运算闭运算水瓶满溢判断

In a juice filling factory, the engineers are required to make a software to find out if the bottle is filled or not as shown in fig.1 . The bottle that are not filled up should be found out and marked, and the bottle will be picked up by the robot and placed on the filling line again to finish filling. Please work as an engineer to help the factory solve this problem.

The procedure could be

  1. binary the original image.
  2. do something to make the binary image better.
  3. find the connected components, count them, and calculate the area, find out the connected components that are not satisfied with quality requirement.
  4. mark the unqualified connected components , calculate the rate of disqualification.
  5. show all the image from the whole procedure, compare the original image and the final processed image, show all the analysis data.

The final image could be like shown in fig. 2 .

matlab图像分割二值化开运算闭运算水瓶满溢判断_第1张图片
matlab图像分割二值化开运算闭运算水瓶满溢判断_第2张图片

% this file is used to  finish assignment
im = imread('sujon1.jpg');
imshow(im);
im = rgb2gray(im);
[m,n]=size(im);

for y=1:n
    S(y)=sum(im(1:m,y));
end
y=1:n;
figure
subplot(211),plot(y,S(y));
title('分割图');
x = find(S<2000);
choose_x = [0,0,0];
choose_end = 1;
start = x(1);
for i=2:length(x)
    if x(i) - x(i-1) > 2 || i == length(x)
        choose_x(choose_end) = int16((x(i-1)+start)/2);
        choose_end = choose_end + 1;
        start = x(i);
    end
end
I_gather = cell(5,1);
for i=1:length(choose_x)
    figure(2);

    if i==1
        I = im(:,1:choose_x(i));
        I_gather{i} = I;
    else
        I = im(:,choose_x(i-1):choose_x(i));
        I_gather{i} = I;
    end
    
    subplot(2,5,i);
    imshow(I)
end
I = im(:,choose_x(4):n);
I_gather{5} = I;
subplot(2,5,5);imshow(I)


for i=1:5
    x = double(I_gather{i});
    x=x / 255;
    thresh=graythresh(x);
    B=im2bw(x,thresh+0.3);
    se = strel('disk',7);
    B = imopen(B,se);
    B = imclose(B,se);
    se = strel('disk',10);
    B = imopen(B,se);
    subplot(2,5,5+i);
    imshow(B);  
end

你可能感兴趣的:(matlab,图像分割,二值化,腐蚀膨胀)