matlab水果识别

clear;clc;close all;
%%%%%%%%%%%%%%%%% 读取原图,并处理成二值图像 %%%%%%%%%%%%%%%%%%%%%%%
I=imread(‘tao.png’);

I2=rgb2gray(I);
BW=im2bw(I2,0.9);
figure(1),subplot(1,3,1),imshow(I),title(‘原始图像’);
subplot(1,3,2),imshow(I2),title(‘灰度图像’);
subplot(1,3,3),imshow(BW),title(‘二值图像’);

%%%%%%%%%%%%%%%% 进行边缘检测 得到不连续的图形边界 %%%%%%%%%%%%%%%%%%%%%%
%得到各个图形的连续边界
SE=strel(‘rectangle’,[40 30]); % 结构定义
J2=imopen(BW,SE); % 开运算
figure(2),imshow(J2),title(‘对二值图像进行开运算’);

SE=strel(‘square’,5); % 采用方形结构元素进行腐蚀
J=imerode(~J2,SE);
BW2=(~J2)-J; % 检测边缘
figure(3),imshow(BW2),title(‘边缘检测’);

%填充了已有的检测的连续形状边界
B = imfill(BW2,‘holes’);%图像填充
B = bwmorph(B,‘remove’);%移除内部像素
figure(4),imshow(B),title(‘提取出的边界图像’);

%将不同的图形进行分别标记,num表示连接的图形对象的个数
[Label,num] = bwlabel(B,8);

%%%%%%%%%%%%%%%%%%%计算各个图形单元边界像素点数%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1 : num
Premeter(i) = 0;
end

[row,col] = size(Label); 
for i = 1 : row 
    for j = 1 : col 
        if(Label(i,j) > 0) 
            Premeter(Label(i,j)) = Premeter(Label(i,j)) + 1;%计算标记后的各块图形边界中像素的个数的总数 
        end 
    end 
end 

%%%%%%%%%%%%%%%%%%% 计算各个图形单元的面积%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FilledLabel = imfill(Label,‘holes’); %填充标记过的边界线中间围成的图形区域
figure,imshow(FilledLabel),title(‘标记过并被填充的结果’);
for i = 1 : num
Area(i) = 0;
end

[row,col] = size(FilledLabel);
for i = 1 : row
for j = 1 : col
if(FilledLabel(i,j) > 0)
Area(FilledLabel(i,j)) = Area(FilledLabel(i,j)) + 1; %通过统计像素点个数的方式来求各形状的面积
end
end
end

%%%%%%%%%%%%%%%%%% 计算各个图形单元的圆度 %%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1 : num
Ecllipseratio(i) = 4piArea(i)/Premeter(i)^2;
end

%%%%%%%%%%%%%%%%%%%% 计算各个图像的色度 %%%%%%%%%%%%%%%%%%%%%

HSV = rgb2hsv(I); %转换为HSV,为后面的色度元素的提取做准备

[row,col] = size(FilledLabel); %统计填充后的图形中各块图形所含像素的个数的多少
MeanHue = zeros(1,num);
for i = 1 : num
Hue = zeros(Area(i),1);
nPoint = 0;
for j = 1 : row
for k = 1 : col
if(FilledLabel(j,k) == i)
nPoint = nPoint + 1;
Hue(nPoint,1) = HSV(j,k,1);
end
end
end

    Hue(:,i) = sort(Hue(:,1)); 
    for j = floor(nPoint*0.1) : floor(nPoint*0.9) 
        MeanHue(i) = MeanHue(i) + Hue(j,1); 
    end 
    MeanHue(i) = MeanHue(i) / (0.8*nPoint);  %计算出平均的色度值 
end 

%%%%%%%%%%%%%%%%%%%%%%%% 各种水果识别 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%识别梨,在二维特征空间对各个图像进行类别区分
pear=0;result1=0;
for i=1:num
if(MeanHue(i)<0.125) %判断各个图形中平均色度值小于0.125的为梨
pear=i;
result1=1;
end
end
%对分出来的梨构建相应的图像掩膜,并用对原图的亮度图像进行掩膜操作
pearHSV=HSV;
for j = 1 : row
for k = 1 : col
if(FilledLabel(j,k) ~=pear)
pearHSV(j,k,3)=0;
end
end
end
%变换生成最终的结果图像
pearmatrix = hsv2rgb(pearHSV);%转换为RGB彩图,彩图中已经滤去了其余水果,只剩下梨
if(result1==1)
figure,imshow(pearmatrix);title(‘梨’);
end

%%%%%%%%%识别桃,在二维特征空间对各个图像进行类别区分
pitch=0;result2=0;
for i=1:num
if(MeanHue(i)>0.5) %判断各个图形中平均色度值大于0.5的为桃
pitch=i;
result2=1;
end
end
%对分别出来的桃构建相应的图像掩膜,并用对原图的亮度图像进行掩膜操作
pitchHSV=HSV;
[row,col] = size(FilledLabel); %统计填充后的图形中各块图形所含像素的个数的多少
for j = 1 : row
for k = 1 : col
if(FilledLabel(j,k) ~=pitch)
pitchHSV(j,k,3)=0;
end
end
end
%变换生成最终的结果图像
pitchmatrix = hsv2rgb(pitchHSV); %转换为RGB彩图,彩图中已经滤去了其余水果,只剩下桃
if(result2==1)
figure,imshow(pitchmatrix);title(‘桃子’);
end

%%%%%%%识别苹果,在二维特征空间对各个图像进行类别区分
apple=0;result3=0;
for i=1:num
if((Ecllipseratio(i)<1.25)&&(Ecllipseratio(i)>1.0)) %判断各个图形中圆度居于1.0与1.25之间的的为苹果
apple=i;
result3=1;
end
end
%对分出来的苹果构建相应的图像掩膜,并用对原图的亮度图像进行掩膜操作
appleHSV=HSV;
for j = 1 : row
for k = 1 : col
if(FilledLabel(j,k) ~=apple)
appleHSV(j,k,3)=0;
end
end
end
%变换生成最终的结果图像
applematrix = hsv2rgb(appleHSV); %转换为RGB彩图,彩图中已经滤去了其余水果,只剩下苹果
if(result3==1)
figure,imshow(applematrix);title(‘苹果’);
end

%%%%%%当没有梨,桃子,苹果,出现时
if(result10&&result20&&result3==0)
figure,imshow(‘无.png’);
end

你可能感兴趣的:(matlab)