您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 招聘面试 > matlab经典代码大全
哈哈哈MATLAB显示正炫余炫图:plot(x,y1,'*r',x,y2,'ob')定义【0,2π】;t=0:pi/10:2*pi;定义函数文件:function[返回变量列表]=函数名(输入变量列表)顺序结构:选择结构1)if-else-end语句其格式为:if逻辑表达式程序模块1;else程序模块2;End图片读取:%选择图片路径[filename,pathname]=...uigetfile({'*.jpg';'*.bmp';'*.gif'},'选择图片');%合成路径+文件名str=[pathname,filename];%为什么pathname和filename要前面出现的位置相反才能运行呢???%读取图片im=imread(str);%使用图片axes(handles.axes1);%显示图片imshow(im);边缘检测:globalimstr=get(hObject,'string');axes(handles.axes1);switchstrcase'原图'imshow(im);case'sobel'BW=edge(rgb2gray(im),'sobel');imshow(BW);case'prewitt'BW=edge(rgb2gray(im),'prewitt');imshow(BW);case'canny'BW=edge(rgb2gray(im),'canny');imshow(BW);Canny算子边缘定位精确性和抗噪声能力效果较好,是一个折中方案end;开闭运算:se=[1,1,1;1,1,1;1,1,1;1,1,1];%StructuringElementI=rgb2gray(im);imshow(I,[]);title('OriginalImage');I=double(I);[im_height,im_width]=size(I);[se_height,se_width]=size(se);halfheight=floor(se_height/2);halfwidth=floor(se_width/2);[se_origin]=floor((size(se)+1)/2);image_dilation=padarray(I,se_origin,0,'both');%Imagetobeusedfordilationimage_erosion=padarray(I,se_origin,256,'both');%Imagetobeusedforerosion%%%%%%%%%%%%%%%%%%%%%Dilation%%%%%%%%%%%%%%%%%%%%%fork=se_origin(1)+1:im_height+se_origin(1)forkk=se_origin(2)+1:im_width+se_origin(2)dilated_image(k-se_origin(1),kk-se_origin(2))=max(max(se+image_dilation(k-se_origin(1):k+halfheight-1,kk-se_origin(2):kk+halfwidth-1)));endendfigure;imshow(dilated_image,[]);title('ImageafterDilation');%%%%%%%%%%%%%%%%%%%%Erosion%%%%%%%%%%%%%%%%%%%%se=se';fork=se_origin(2)+1:im_height+se_origin(2)forkk=se_origin(1)+1:im_width+se_origin(1)eroded_image(k-se_origin(2),kk-se_origin(1))=min(min(image_erosion(k-se_origin(2):k+halfwidth-1,kk-se_origin(1):kk+halfheight-1)-se));endendfigure;imshow(eroded_image,[]);title('ImageafterErosion');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Opening(Erosionfirst,thenDilation)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%se=se';image_dilation2=eroded_image;%Imagetobeusedfordilationfork=se_origin(1)+1:im_height-se_origin(1)forkk=se_origin(2)+1:im_width-se_origin(2)opening_image(k-se_origin(1),kk-se_origin(2))=max(max(se+image_dilation2(k-se_origin(1):k+halfheight-1,kk-se_origin(2):kk+halfwidth-1)));endendfigure;imshow(opening_image,[]);title('OpeningImage');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Closing(Dilationfirst,thenErosion)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%se=se';image_erosion2=dilated_image;%Imagetobeusedforerosionfork=se_origin(2)+1:im_height-se_origin(2)forkk=se_origin(1)+1:im_width-se_origin(1)closing_image(k-se_origin(2),kk-se_origin(1))=min(min(image_erosion2(k-se_origin(2):k+halfwidth-1,kk-se_origin(1):kk+halfheight-1)-se));endendfigure;imshow(closing_image,[]);title('ClosingImage');Warning:Imageistoobigtofitonscreen;displayingat31%scale.IntruesizeResize1at308Intruesizeat44Inimshowat161图像的直方图归一化:I=imread(‘red.bmp’);%读入图像figure;%打开新窗口[M,N]=size(I);%计算图像大小[counts,x]=imhist(I,32);%计算有32个小区间的灰度直方图counts=counts/M/N;%计算归一化灰度直方图各区间的值stem(x,counts);%绘制归一化直方图图像平移:I=imread('shuichi.jpg');se=translate(strel(1),[180190]);B=imdilate(I,se);figure;subplot(1,2,1),subimage(I);title('原图像');subplot(1,2,2),subimage(B);title('平移后图像');图像的转置;A=imread('nir.bmp');tform=maketform('affine',[010;100;001]);B=imtransform(A,tform,'nearest');figure;imshow(A);figure;imshow(B);imwrite(B,'nir转置后图像.bmp');图像滤波:B=imfilter(A,H,option1,option2,...)或写作g=imfilter(f,w,filtering_mode,boundary_options,size_options)其中,f为输入图像,w为滤波掩模,g为滤波后图像。filtering_mode用于指定在滤波过程中是使用“相关”还是“卷积”。boundary_options用于处理边界充零问题,边界的大小由滤波器的大小确定。具体参数选项见下表:选项描述filtering_mode‘corr’通过使用相关来完成,该值为默认。‘conv’通过使用卷积来完成boundary_options‘X’输入图像的边界通过用值X(无引号)来填充扩展其默认值为0‘replicate’图像大小通过复制外边界的值来扩展‘symmetric’图像大小通过镜像反射其边界来扩展‘circular’图像大小通过将图像看成是一个二维周期函数的一个周期来扩展size_options‘full’输出图像的大小与被扩展图像的大小相同‘same’输出图像的大小与输入图像的大小相同。这可通过将滤波掩模的中心点的偏移限制到原图像中包含的点来实现,该值为默认值。中直滤波:h=medfilt2(I1,[m,n]);
本文标题:matlab经典代码大全
链接地址:https://www.777doc.com/doc-2887760 .html