资源简介
基于MATLAB实现的水果品相检测系统
代码片段和文件信息
%% 文件导入
clc;
clear;
cd(‘C:\Users\Administrator\Desktop\郑志豪毕设全‘);
%% 图像分割(粗)
%读取
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);%联通数目
figure
imshow(L)
status=regionprops(L‘all‘);
for i=1:num
centroid{i}=status(i).Centroid;%寻找质心
end
figure
imshow(L);%联通图像123
%计算面积
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);%质心
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)%循环输出
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‘); %内外轮廓
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‘);%画出质心
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
- 上一篇:RNN算法打包matlab
- 下一篇:QPSK调制与解调的simuli
nk仿真
评论
共有 条评论