(完整word版)matlab经典代码大全,推荐文档.doc
哈哈哈MATLAB显示正炫余炫图:plot(x,y1,'* r',x,y2,'o b')定义【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);边缘检测:global im str=get(hObject,'string');axes (handles.axes1);switch str case ' 原图 ' 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; %Structuring ElementI=rgb2gray(im);imshow(I,);title('Original Image');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'); %Image to be used for dilationimage_erosion=padarray(I,se_origin,256,'both'); %Image to be used for erosion% Dilation %for k=se_origin(1)+1:im_height+se_origin(1) for kk=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('Image after Dilation');% Erosion %se=se'for k=se_origin(2)+1:im_height+se_origin(2) for kk=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('Image after Erosion');% Opening(Erosion first, then Dilation) %se=se'image_dilation2=eroded_image; %Image to be used for dilationfor k=se_origin(1)+1:im_height-se_origin(1) for kk=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('Opening Image');% Closing(Dilation first, then Erosion) %se=se'image_erosion2=dilated_image; %Image to be used for erosionfor k=se_origin(2)+1:im_height-se_origin(2) for kk=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('Closing Image');Warning: Image is too big to fit on screen; displaying at 31% scale.> In truesize>Resize1 at 308 In truesize at 44 In imshow at 161图像的直方图归一化: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),180 190);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',0 1 0;1 0 0;0 0 1);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_modecorr通过使用相关来完成,该值为默认。conv通过使用卷积来完成boundary_optionsX输入图像的边界通过用值X(无引号)来填充扩展其默认值为0replicate图像大小通过复制外边界的值来扩展symmetric图像大小通过镜像反射其边界来扩展circular图像大小通过将图像看成是一个二维周期函数的一个周期来扩展size_optionsfull输出图像的大小与被扩展图像的大小相同same输出图像的大小与输入图像的大小相同。这可通过将滤波掩模的中心点的偏移限制到原图像中包含的点来实现,该值为默认值。中直滤波:h=medfilt2(I1,m,n);