⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 小波边缘检测matlab程序.m

📁 一个基于小波变换的边缘检测程序。采用matlab编写的M文件
💻 M
字号:
%load woman; 
X=imread('Miss.bmp'); 
clear coef1; 
clear coef2; 
clear coef3; 
clear form; 
x=X; 
x=double(x);%200 line,300 column;  
line=250; 
column=250; 
J=6; 
Tu=7; 
smooth_area_num=20; 
edge_area_num=40; 
for i=1:line 
  coef1(i,:)=cwt(x(i,:),1,'bior4.4'); 
  coef2(i,:)=cwt(x(i,:),2,'bior4.4'); 
  coef3(i,:)=cwt(x(i,:),J,'bior4.4'); 
end 
%正值1,负值0的矩阵temp表示(突变点左位置) 
max=0; 
max_loc=0; 
max_num=15; 
for i=1:line 
    j=1; 
    while j0 & coef3(i,j+1)<0) | (coef3(i,j)<0 & coef3(i,j+1)>0 )%找到左位置j 
            for k=j+1:column-1 
                
                if coef3(i,k)*coef3(i,j)>=0%找到右位置k 
                    for m=j:k 
                        if abs(coef3(i,m))>max 
                            max=abs(coef3(i,m)); 
                            max_loc=m; 
                        end 
                    end 
                    if max>max_num 
                       form(i,max_loc)=1; 
                    end 
                    j=k; 
                    max=0; 
                    break; 
                end 
            end%for k 
            if k==column-1 
                j=column-1; 
            end 
        else 
            j=j+1; 
        end%if 
    end%while 
end 
%for i=1:line 
%    for j=1:column 
%        if abs(coef1(i,j))>=2*abs(coef2(i,j)) %是否加绝对值?!!!!!!!!!!!!!!!!!!!!!!!!!!! 
%           form(i,j)=0;%第一种方法去噪声 
%        end 
%    end 
%end 
%***************************************************** 
%对列作小波变换 
x=x'; 
for i=1:line 
  coef1(i,:)=cwt(x(i,:),1,'bior4.4'); 
  coef2(i,:)=cwt(x(i,:),2,'bior4.4'); 
  coef3(i,:)=cwt(x(i,:),J,'bior4.4'); 
end 
%正值1,负值0的矩阵temp表示(突变点左位置) 
max=0; 
max_loc=0; 
for i=1:line 
    j=1; 
    while j0 & coef3(i,j+1)<0) | (coef3(i,j)<0 & coef3(i,j+1)>0 )%找到左位置j 
            for k=j+1:column-1 
                
                if coef3(i,k)*coef3(i,j)>=0%找到右位置k 
                    for m=j:k 
                        if abs(coef3(i,m))>max 
                            max=abs(coef3(i,m)); 
                            max_loc=m; 
                        end 
                    end 
                    if max>max_num 
                       form(max_loc,i)=1; 
                    end 
                    j=k; 
                    max=0; 
                    break; 
                end 
            end%for k 
            if k==column-1 
                j=column-1; 
            end 
        else 
            j=j+1; 
        end%if 
    end%while 
end 
%包括噪声在内的所有极值点① 
%for i=1:line 
%    for j=1:column 
%        if abs(coef1(i,j))>=2*abs(coef2(i,j)) %是否加绝对值?!!!!!!!!!!!!!!!!!!!!!!!!!!! 
%           form(i,j)=0;%第一种方法去噪声 
%        end 
%    end 
%end 
%此处添加用第二种方法去噪!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!② 结束 
for i=2:line-2 
    for j=2:column-2 
        %链长为1 
        if form(i,j-1)==0 & form(i,j+1)==0 & form(i-1,j-1)==0 & form(i-1,j)==0 & form(i-1,j+1)==0 & form(i+1,j-1)==0 & form(i+1,j)==0 & form(i+1,j+1)==0 
           form(i,j)=0;% 
        end 
         
        %**************************************** 
        %链长为2 
        %0000 
        %0110 
        %0000 
        if form(i,j)==1 & form(i,j+1)==1 & sum(sum(form(i-1:i+1,j-1:j+2)))==2 
            form(i,j)=0; 
            form(i,j+1)=0; 
        end 
        %000 
        %010 
        %010 
        %000 
        if form(i,j)==1 & form(i+1,j)==1 & sum(sum(form(i-1:i+2,j-1:j+1)))==2 
            form(i,j)=0; 
            form(i+1,j)=0; 
        end 
        %0000 
        %0100 
        %0010 
        %0000 
        if form(i,j)==1 & form(i+1,j+1)==1 & sum(sum(form(i:i+1,j:j+1)))==2 
            if sum(sum(form(i-1:i+2,j-1:j+2)))==3 & ( form(i-1,j+2)==1 | form(i+2,j-1)==1) 
                form(i,j)=0; 
                form(i+1,j+1)=0; 
            end 
            if sum(sum(form(i-1:i+2,j-1:j+2)))==4 &  form(i-1,j+2)==1 & form(i+2,j-1)==1 
                form(i,j)=0; 
                form(i+1,j+1)=0; 
            end 
        end 
        %0000 
        %0010 
        %0100 
        %0000 
        if form(i,j+1)==1 & form(i+1,j)==1 & sum(sum(form(i:i+1,j:j+1)))==2 
            if sum(sum(form(i-1:i+2,j-1:j+2)))==3 & ( form(i-1,j-1)==1 | form(i+2,j+2)==1) 
                form(i,j+1)=0; 
                form(i+1,j)=0; 
            end 
            if sum(sum(form(i-1:i+2,j-1:j+2)))==4 &  form(i-1,j-1)==1 & form(i+2,j+2)==1 
                form(i,j+1)=0; 
                form(i+1,j)=0; 
            end 
        end 
    end 
end 
%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!! 
max=0; 
 
 
             
     
             
for i=3:line-3 
    for j=3:column-3 
        s=sum(sum(form(i-2:i+2,j-2:j+2))); 
            for m=i-2:i+2 
                for n=j-2:j+2 
                    if s>=7 & abs(coef3(m,n))<=s*3 
                        form(m,n)=0; 
                    end 
                end 
            end 
        end 
    end 
end 
             
             
%显示form即为边缘检测图像


%load bust; 
%加入含噪 
[X1,map]=imread('miss.bmp'); 
%init=2055615866; 
%randn('seed',init); 
%X1=X+20*randn(size(X)); 
subplot(221);image(X1); 
title('原始图像');axis square; 
%用小波db4对图像X进行一层小波包分解 
T=wpdec2(X1,1,'db4'); 
%重构图像近似部分 
A=wprcoef(T,[1,0]); 
subplot(222);image(A); 
title('图像的近似部分'); 
axis square; 
%边缘检测 
%原图像的边缘检测 
BW1=edge(A,'sobel'); 
subplot(223);imshow(BW1); 
title('原图像的边缘'); 
axis square; 
%图像近似部分的边缘检测 
BW2=edge(X1,'sobel'); 
subplot(224);imshow(BW2); 
title('图像近似部分的边缘'); 
axis square;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -