【于燕飞韩丽丽】边缘检测代码

与世无争的帅哥 提交于 2020-02-02 00:10:19

im=imread('C:\Documents and Settings\Administrator\桌面\2.jpg');

Subplot(231);imshow(im);title('原始图像');

ii=rgb2gray(im); %ii=im2double(ii);

Subplot(232);imshow(ii);title('灰度图像');

hsv=rgb2hsv(im);

h=hsv(:,:,1); s=hsv(:,:,2); v=hsv(:,:,3);

%figure;imshow(h);title('h');

%figure;imshow(s);%title('s');

Subplot(233);imshow(v);title('强度v');

 

v=imnoise(v,'salt & pepper',0.1);

Subplot(234),imshow(v);title('椒盐噪声');

v=~im2bw(v,0.75); %转为二值图像并取反

Subplot(235),imshow(v);title('二值取反');

 

%图像增强

[h,w]=size(v);

n=3;  

f=double(v);

a=ones(n,n);

y=f;

 for i=1:h-n+1

     for j=1:w-n+1

         a=f(i:i+(n-1),j:j+(n-1));

         s=sum(sum(a));

         y(i+(n-1)/2,j+(n-1)/2)=s/(n*n);

     end

 end

Subplot(236),imshow(v);title('二值取反并增强');

 

figure;

v = edge(v,'canny',0.9);

Subplot(231),imshow(v);title('canny');

v = edge(v,'sobel');

Subplot(232),imshow(v);title('sobel');

v = edge(v,'prewitt');

Subplot(233),imshow(v);title('prewitt');

v = edge(v,'log');

Subplot(234),imshow(v);title('log');

v = edge(v,'roberts');

Subplot(235),imshow(v);title('roberts');

v=bwfill(v,'holes');%填充内部空洞

Subplot(236),imshow(v);title('holes');

 

 


 

close all;clear;clc;
im=imread('C:\Documents and Settings\Administrator\桌面\2.jpg');
subplot(121);imshow(im);title('原始图像');
ii=rgb2gray(im);subplot(122);imshow(ii);title('灰度图像');

hsv=rgb2hsv(im);
h=hsv(:,:,1);
s=hsv(:,:,2);
v=hsv(:,:,3);
figure;
subplot(221);
imshow(h);title('色调h');
subplot(222);imshow(s);title('饱和度s');
subplot(223);imshow(v);title('强度v');

figure;
subplot(121);imshow(s);title('饱和度s');
subplot(122);imhist(s);title('直方图');

%开操作
se=strel('square',3);%使用5*5的结构元素对图像进行运算
open1=imopen(s,se);
close1=imclose(open1,se);

figure;
imshow(close1);title('开-闭1');

%孔洞填充
bw0=im2bw(close1);
figure;
subplot(221);imshow(bw0);title('bw0');
bw1=im2bw(bw0);
subplot(222);imshow(bw1);title('bw1');
bw2=im2bw(bw1);
subplot(223);imshow(bw2);title('bw2');
bw3=im2bw(bw2);
subplot(224);imshow(bw3);title('bw3');
bw4=im2double(bw3);

edi1 = edge(bw3, 'roberts');
edi2 = edge(bw3, 'sobel');
edi3 = edge(bw3, 'prewitt');
edi4 = edge(bw3, 'canny');%自动阀值选择法对图像进行canny算子检测,轮廓提取
figure;
subplot(221);imshow(edi1);title('roberts算子检测后的图像');
subplot(222);imshow(edi2);title('sobel算子检测后的图像');
subplot(223);imshow(edi3);title('prewitt算子检测后的图像');
subplot(224);imshow(edi4);title('canny算子检测后的图像');

 

%边缘检测
SFST=edge(bw3,'sobel');
figure,imshow(SFST),title('Sobel滤波器(水平和垂直)');
figure;
s45=[-2 -1 0;-1 0 1; 0 1,2];
SFST45=imfilter(bw3,s45,'replicate');
subplot(221);imshow(SFST45),title('Sobel滤波器(45度)');
sm45=[0 1 2;-1 0 1;-2 -1 0];
SFSTM45=imfilter(bw3,sm45,'replicate');
subplot(222);imshow(SFSTM45),title('Sobel滤波器(-45度)');

ed01=or(SFST,SFST45);
subplot(223);imshow(ed01);title('SFST,SFST45融合');
ed02=or(ed01,SFSTM45);
subplot(224);imshow(ed02);title('ed01,SFSTM45');

s0=[-1 -2 -1;0 0 0;1 2 1];
s1=[-2 -1 0;-1 0 1;0 1 2];
s2=[-1 0 1;-2 0 2;-1 0 1];
s3=[0 1 2;-1 0 1;-2 -1 0];
s4=[0 1 2;-1 0 1;-2 -1 0];
s5=[0 1 2;-1 0 1;-2 -1 0];
s6=[1 0 -1;2 0 -2;1 0 -1];
s7=[0 -1 -2;1 0 -1;2 1 0];

figure;
a0=conv2(bw4,s0);subplot(221);imshow(a0);title('a0');
a1=conv2(bw4,s1);subplot(222);imshow(a1);title('a1');
a2=conv2(bw4,s2);subplot(223);imshow(a2);title('a2');
a3=conv2(bw4,s3);subplot(224);imshow(a3);title('a3');
figure;
a4=conv2(bw4,s4);subplot(221);imshow(a4);title('a4');
a5=conv2(bw4,s5);subplot(222);imshow(a5);title('a5');
a6=conv2(bw4,s6);subplot(223);imshow(a6);title('a6');
a7=conv2(bw4,s7);subplot(224);imshow(a7);title('a7');

%边缘融合
figure;
a01=or(a0,a1);subplot(221);imshow(a01);title('a0与a1融合');
a23=or(a2,a3);subplot(222);imshow(a23);title('a2与a3融合');
a45=or(a4,a5);subplot(223);imshow(a45);title('a4与a5融合');
a67=or(a6,a7);subplot(224);imshow(a67);title('a6与a7融合');
figure;
b0=or(a01,a23);subplot(221);imshow(b0);title('a01与a23融合');
b1=or(a45,a67);subplot(222);imshow(b1);title('a45与a67融合');
b01=or(b0,b1);subplot(223);imshow(b01);title('b0与b1融合');

[row col] = size(bw3);
count = 0;
arr = zeros(row*col,1);
visit= zeros(size(bw3));

% 调用计算连通域函数进行数PM2.5颗粒和计算PM2.5颗粒的面积
for i = 1:row
for j = 1:col
if bw3(i,j)&&visit(i,j) == 0%寻找到白点
count = count +1;%计数加一 ,给白色像素赋值为颗粒的标号
p = [i; j]; %记住该点
[c ,domain] = finddomain(bw3, p);%调用函数返回domain连通域内有c个像素(面积)
arr(count) = c;%将各个颗粒的面积存入arr数组中
for m = 1:c
visit(domain(1,m), domain(2, m)) = 1;%标记已入队的白色像素的8连通域内的像素点已访问
end
end
end
end
disp('显微图像中颗粒的总数为:');
disp(count);
%disp('显微图像中颗粒的面积(像素)为:');
%disp(arr(1:count));
pi=3.1415926;
l=0;
arry=zeros(500,1);
arrys=zeros(500,1);
for k=1:count
r=sqrt(arr(k)/pi);
r=2*r;
r=r/3.015;
if r<=2.5
l=l+1;
arry(l)=r;
arrys(l)=arr(k);
end
end
disp('大气PM2.5颗粒数目为:');
disp(l);
%disp('大气PM2.5颗粒粒径为:');
%disp(arry(1:l));
%disp('大气PM2.5颗粒大小为:');
%disp(arrys(1:l));

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!