資源簡介
基于MATLAB實(shí)現(xiàn)的水果品相檢測系統(tǒng)

代碼片段和文件信息
%%?文件導(dǎo)入
clc;
clear;
cd(‘C:\Users\Administrator\Desktop\鄭志豪畢設(shè)全‘);
%%?圖像分割(粗)
%讀取
global?I
global?v
[filenamepathname]=uigetfile(‘*.*‘‘Select?Input?file‘);?
cd(pathname);
file?=?[pathname?filename];?
fid?=?fopen(file‘rt‘);?
if?fid?==?-1?
(‘Error?opening?the?file‘)?
end
assignin(‘base‘‘filename‘filename);%空間共享
I=imread(filename);
figure
imshow(I);
%連通域
g=I(::1);
thresh=10;
bwImg=im2bw(g1-thresh/255);
filtImg=medfilt2(bwImg[5?5]);%濾波
bw=filtImg;
se1=strel(‘disk‘2);?
bw1=imerode(bwse1);%腐蝕
Bw=~bw1;%取反
se1=strel(‘disk‘2);
Bw1=imerode(Bwse1);%腐蝕?
Bw1=~Bw1;
[L?num]=bwlabel(Bw14);%聯(lián)通數(shù)目
figure
imshow(L)
status=regionprops(L‘a(chǎn)ll‘);
for?i=1:num
centroid{i}=status(i).Centroid;%尋找質(zhì)心
end
figure
imshow(L);%聯(lián)通圖像123
%計(jì)算面積
for?i=1:num
area(i)=max(status(i).Area‘);%最大面積
end
%area=max(area);
BoundingBox=regionprops(L‘BoundingBox‘);
for?i=1:num
boundingbox{i}=status(i).BoundingBox;%x?y?wid?leg
end
for?i=1:num
centroids=centroids(i);%質(zhì)心
end
%圖像單個輸出
S=size(L);
A=L;
for?a=1:num
????A=L;
for?i=1:S(1)
????for?j=1:S(2)
????????if?A(ij)==a
????????????A(ij)=1;
????????else
????????????A(ij)=0;
????????end
????end
end
figure
imshow(A)%循環(huán)輸出
end
%%?找孔中心(精)
hold?on
h?=?rgb2gray(I);%二值化
h?=?medfilt2(h[88]);?
c=edge(h‘canny‘);?
rz=c;
figure
imshow(rz)
rz=imfill(rz‘holes‘);%腐蝕
[BL]=bwboundaries(rz‘noholes‘);?%內(nèi)外輪廓
for?p=1:length(B)?
????boundary?=?B{p};
????plot(boundary(:2)?boundary(:1)?‘w‘?‘LineWidth‘?2)%描邊
end
%找中心
stats?=?regionprops(L‘Area‘‘Centroid‘);
min?=?0.85;js=0;length(B)
for?p?=?1:length(B)
boundary?=?B{p};
delta_s?=?diff(boundary).^2;
per?=?sum(sqrt(sum(delta_s2)));?area?=?stats(p).Area;
metric?=?4*pi*area/per^2;?metric_s?=?sprintf(‘%2.2f‘metric);?
if(metric>min?&?per>50)
js=js+1;
centroid?=?stats(p).Centroid;
plot(centroid(1)centroid(2)‘ko‘);%畫出質(zhì)心
end?
text(boundary(12)-35boundary(11)+13metric_s‘Color‘‘y‘...
‘FontSize‘14‘FontWeight‘‘bold‘);
end
%?if(js>=1)
%?set(handles.ax2‘xtick‘[]);
%?set(handles.ax2‘Ytick‘[]);
%?set(handles.ax2‘box‘‘on‘);
%?axes(handles.ax2);
%?imshow(‘18.jpg‘);
%?else
%?set(handles.ax2‘xtick‘[]);
%?set(handles.ax2‘Ytick‘[]);
%?set(handles.ax2‘box‘‘on‘);
%?axes(handles.ax2);
%?imshow(‘19.jpg‘);
%?end
%%?間距
%模擬間距
centroid=[51?117;136?56;231?99]
%
d=num;
for?b=1:num
????if?d>b
????????final(x)=sqrt((centroid(b1)-centroid(b+11)).^2+(centroid(b2)-centroid(b+12)).^2)
????else
????????return?
????end
end
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2624??2018-04-11?15:59??interfere.m
-----------?---------??----------?-----??----
?????????????????2624????????????????????1
評論
共有 條評論