您好,欢迎访问三七文档
当前位置:首页 > 医学/心理学 > 医学现状与发展 > 图像处理的matlab程序
贡献几个图像处理的matlab程序,每一个都是单独的程序。前言:学习了图像处理这门课程,掌握了一些基本的图像处理方法及编程实现方法。提供给大家参考,欢迎加QQ探讨问题。以下程序均为笔者根据处理的原理原创作品,经运行检测可行。若效果不能令方家满意,恳请赐教:cfvsfc@163.com直方图拉伸matlab实现:map1=imread('p.jpg');[row,col,dep]=size(map1);%行,列,深度值map=zeros(row,col);map=uint8(map);pixsum=row*col;%像素总数%灰度化fori=1:rowforj=1:colgray=0.11*map1(i,j,1)+0.59*map1(i,j,2)+0.3*map1(i,j,3);map(i,j)=gray;endend%灰度化完成,输出灰度图figure(1)imshow(map)title('原始灰度图像')%统计各灰度值像素个数,存储在p中,计算概率,存储在pi中p=zeros(1,256);pr=zeros(1,256);sumpr=zeros(1,256);%累积概率密度fork=1:256;p(k)=length(find(map==k));pr(k)=p(k)/pixsum;endsumpr(1)=pr(1);fori=1:255sumpr(i+1)=sumpr(i)+pr(i);end%统计完成,概率计算完成,累积概率密度计算完成。%绘制灰度直方图figure(2)bar(0:255,pr,'g')axis([02550max(pr)])xlabel('灰度r')ylabel('概率pi')title('灰度直方图')%灰度直方图绘制完成%直方图拉伸。(g-0)/(f-min)=(255-0)/(max-min)A=max(max(map));B=min(min(map));fori=1:rowforj=1:col%fig(i,j)=255/(A-B)*(map(i,j)-B);fig(i,j)=255*sumpr(map(i,j)+1);%直方图均衡化endendfig=uint8(fig);fork=1:256;pfig(k)=length(find(fig==k));pfigi(k)=pfig(k)/pixsum;endfigure(3)imshow(fig)title('直方图拉伸后灰度图像')figure(4)bar(0:255,pfigi,'g')axis([02550max(pfigi)])xlabel('灰度r')ylabel('概率pi')title('直方图拉伸化后灰度直方图')%计算拉伸前后方差mean_map=sum(sum(map))/(row*col);mean_fig=sum(sum(fig))/(row*col);var_map=sum(sum((map-mean_map).^2))/(row*col);var_fig=sum(sum((fig-mean_fig).^2))/(row*col);fprintf('拉伸前方差为:%d',var_map)fprintf('\n拉伸后方差为:%d\n',var_fig)Sobel算子matlab实现:map1=imread('BMW.jpg');[m,n,l]=size(map1);%行,列,深度值I=zeros(m,n);%I=uint8(I);%灰度化fori=1:mforj=1:nI(i,j)=0.11*map1(i,j,1)+0.59*map1(i,j,2)+0.3*map1(i,j,3);endend%灰度化完成,输出灰度图figure(1)imshow(I,[])title('原始灰度图像')B=zeros(m,n);A=I;fori=2:m-1forj=2:n-1sx=-1*A(i-1,j-1)-2*A(i,j-1)-1*A(i+1,j-1)...+1*A(i-1,j+1)+2*A(i,j+1)+1*A(i+1,j+1);sy=-1*A(i-1,j-1)-2*A(i-1,j)-1*A(i-1,j+1)...+1*A(i+1,j-1)+2*A(i+1,j)+1*A(i+1,j+1);B(i,j)=max(sx,sy)/4;endendfigure(2)imshow(B,[])title('Sobel算法处理后')Scharr算子matlab实现:map1=imread('BMW.jpg');[m,n,l]=size(map1);%行,列,深度值I=zeros(m,n);%I=uint8(I);%灰度化fori=1:mforj=1:nI(i,j)=0.11*map1(i,j,1)+0.59*map1(i,j,2)+0.3*map1(i,j,3);endend%灰度化完成,输出灰度图figure(1)imshow(I,[])title('原始灰度图像')B=zeros(m,n);A=I;fori=2:m-1forj=2:n-1sx=-3*A(i-1,j-1)-10*A(i,j-1)-3*A(i+1,j-1)...+3*A(i-1,j+1)+10*A(i,j+1)+3*A(i+1,j+1);sy=-3*A(i-1,j-1)-10*A(i-1,j)-3*A(i-1,j+1)...+3*A(i+1,j-1)+10*A(i+1,j)+3*A(i+1,j+1);B(i,j)=max(sx,sy)/15;endendfigure(2)imshow(B,[])title('Scharr算法处理后')Robert算子matlab实现:map1=imread('BMW.jpg');[m,n,l]=size(map1);%行,列,深度值I=zeros(m,n);%I=uint8(I);%灰度化fori=1:mforj=1:nI(i,j)=0.11*map1(i,j,1)+0.59*map1(i,j,2)+0.3*map1(i,j,3);endend%灰度化完成,输出灰度图figure(1)imshow(I,[])title('原始灰度图像')B=zeros(m,n);A=I;fori=1:m-1;forj=1:n-1;B(i,j)=max(abs(A(i+1,j+1)-A(i,j)),abs(A(i+1,j)-A(i,j+1)));endendfigure(4)imshow(B,[])title('Robert算法处理后')骨架提取和交叉点检测的matlab实现,用的是中轴法,细化法检测出来的是边缘,程序附后:map1=imread('p.jpg');[row,col,dep]=size(map1);%行,列,深度值map=zeros(row,col);pixsum=row*col;%像素总数%灰度化fori=1:rowforj=1:colmap(i,j)=0.11*map1(i,j,1)+0.59*map1(i,j,2)+0.3*map1(i,j,3);endend%灰度化完成,输出灰度图figure(1)imshow(map,[])title('原始灰度图像')Y=zeros(row,col);%存储输出图像%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5根据通道二值化fori=1:rowforj=1:colifmap1(i,j,2)map1(i,j,1)&&map1(i,j,2)map1(i,j,3)Y(i,j)=1;elseY(i,j)=0;endendendfigure(2)imshow(Y,[])title('二值化后图像')%以下为骨架提取程序temp=ones(row,col);N=0;%统计邻域内非零像素个数。T=0;%统计0-1变换次数fork=1:80%大循环次数fori=2:row-1forj=2:col-1a=[Y(i,j),Y(i,j-1),Y(i+1,j-1),Y(i+1,j),Y(i+1,j+1),Y(i,j+1),Y(i-1,j+1),Y(i-1,j),Y(i-1,j-1)];%邻域元素排序。中间为1,左边为2,逆时针分别为2---9ifa(1)==1form=1:8ifa(m)==1N=N+1;endendforn=1:7ifa(n)==0&&a(n+1)==1T=T+1;endendifa(8)==0&&a(1)==1T=T+1;endendif(N=2)&&(N=6)&&(T==1)&&(a(2)*a(4)*a(6)==0)&&(a(4)*a(6)*a(8)==0)temp(i,j)=0;%标记要删除的点,标志为1endN=0;T=0;endend%删除点fori=1:rowforj=1:coliftemp(i,j)==0Y(i,j)=0;endendend%第一步完成fori=2:row-1forj=2:col-1a=[Y(i,j),Y(i,j-1),Y(i+1,j-1),Y(i+1,j),Y(i+1,j+1),Y(i,j+1),Y(i-1,j+1),Y(i-1,j),Y(i-1,j-1)];%邻域元素排序。中间为1,左边为2,逆时针分别为2---9ifa(1)==1form=2:9ifa(m)==1N=N+1;endendforn=1:7ifa(n)==0&&a(n+1)==1T=T+1;endendifa(8)==0T=T+1;endendif(N=2)&&(N=6)&&(T==1)&&(a(2)*a(4)*a(8)==0)&&(a(2)*a(6)*a(8)==0)temp(i,j)==0;%标记要删除的点,标志为1endN=0;T=0;endend%删除点fori=1:rowforj=1:coliftemp(i,j)==0Y(i,j)=0;endendend%第二步完成end%完成,输出figure(3)imshow(Y,[])title('提取的骨架')%以下为交叉点检测及标志,交叉点标注在灰度图中temp1=zeros(row,col);fori=2:row-1forj=2:col-1c=sum(sum(Y(i-1:i+1,j-1:j+1)));ifc=4temp1(i,j)=1;endendendfori=1:rowforj=1:coliftemp1(i,j)==1map(i,j)=255;endendend%标注完成,显示灰度图figure(4)imshow(map,[])title('标注的交叉点')细化法(这个程序没有实现骨架提取,但仍提出供大家参考):map1=imread('p.jpg');[row,col,dep]=size(map1);%行,列,深度值map=zeros(row,col);pixsum=row*col;%像素总数%灰度化fori=1:rowforj=1:colmap(i,j)=0.11*map1(i,j,1)+0.59*map1(i,j,2)+0.3*map1(i,j,3);endend%灰度化完成,输出灰度图figure(1)imshow(map,[])title('原始灰度图像')Y=zeros(row,col);%存储输出图像%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5根据通道二值化fori=1:rowforj=1:colifmap1(i,j,2)map1(i,j,1)&&map1(i,j,2)map1(i,j,3)Y(i,j)=1;elseY(i,j)=0;endendendfigure(2)imshow(Y,[])title('二值化后图像')%骨架提取,以前的思路是进行若干次腐蚀,直到腐蚀为空集的前一次,但效果不好。%现在改成细化法。skel=zeros(row,col);%细化索引表tab=[0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,...1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,...0,
本文标题:图像处理的matlab程序
链接地址:https://www.777doc.com/doc-5660699 .html