基于MATLAB谷物计数系统【含界面GUI】.zip
立即下载
资源介绍:
基于MATLAB谷物计数系统【含界面GUI】.zip
clc
img=imread('1.jpg');
figure(1)
imshow(img);
title('原图');
gray_img=rgb2gray(img);%灰度
figure(2)
imshow(gray_img);
title('灰度图');
mainfc;
gray_img=imnoise(gray_img,'gaussian',0.04);
figure(3)
imshow(gray_img);
title('加入高斯噪声');
f=gray_img;
f2=double(f);
[row,col]=size(f);
f3=zeros([row,col]);
for x=2:(row-1);
for y=2:(col-1);
f3(x,y)=median([f2(x-1,y-1),f2(x,y-1),f2(x+1,y-1),f2(x-1,y),f2(x,y),f2(x+1,y),f2(x-1,y+1),f2(x,y+1),f2(x+1,y+1)]);
end
end
figure(4)
gray_img=f3;
imshow(gray_img/255);
title('去噪');
bw_img=gray_img<200;
figure(5),imshow(bw_img);%阈值
bw_img=~bw_img;
figure(6),imshow(bw_img);%反色
img2=bw_img;
se=strel('disk',5); %生成圆形结构元素
img2=imdilate(img2,se); %用生成的结构元素对图像进行膨胀
figure(7)
imshow(img2);
title('膨胀');
se=strel('disk',1);
img2=imclose(img2,se);
figure(8);imshow(img2);title('闭运算')
img2=imfill(img2,'holes');
img2=img2;
figure(9);imshow(img2);
title('空洞填充')
%%%定位计数
%计算连通域
img3=img2;
%%
[row,col] = size(img3);
count = 0;
ricearr = zeros(row*col,1);
visited = zeros(size(img3));%
% 调用计算连通域函数进行数米粒和计算米粒的面积
for i = 1:row
for j = 1:col
if img3(i,j)&&visited(i,j) == 0
count = count +1;
pt = [i; j];
[c ,domain] =finddomain(img3, pt);
ricearr(count) = c;
ricearr(1:count);
for m = 1:c
visited(domain(1,m), domain(2, m)) = 1;
end
end
end
end
disp('个数为');
disp(count);