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);
真的是我2
- 粉丝: 839
- 资源: 90
最新资源
- 全球干旱数据集【自校准帕尔默干旱程度指数scPDSI】-190101-202312-0.5x0.5
- 基于Python实现的VAE(变分自编码器)训练算法源代码+使用说明
- 全球干旱数据集【标准化降水蒸发指数SPEI-12】-190101-202312-0.5x0.5
- C语言小游戏-五子棋-详细代码可运行
- 全球干旱数据集【标准化降水蒸发指数SPEI-03】-190101-202312-0.5x0.5
- spring boot aop记录修改前后的值demo
- 全球干旱数据集【标准化降水蒸发指数SPEI-01】-190101-202312-0.5x0.5
- ActiveReports
- vgbvdsbnjkbfnb
- effsefefeffsfwfse
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈