1、 哈哈哈 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'},'选择图片'); %合成路径+文件名
2、 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'
3、 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
4、]; %Structuring Element I=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'
5、); %Image to be used for dilation image_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_origi
6、n(1),kk-se_origin(2))=max(max(se+image_dilation(k-se_origin(1):k+halfheight-1,kk-se_origin(2):kk+halfwidth-1))); end end figure;imshow(dilated_image,[]);title('Image after Dilation'); %%%%%%%%%%%%%%%%% %%% Erosion %%% %%%%%%%%%%%%%%%%% se=se'; for k=se_origin(2)+1:im_height+se_origin(2
7、) 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)); end end figure;imshow(eroded_image,[]);title('Image after Erosion'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%
8、 %%% Opening(Erosion first, then Dilation) %%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% se=se'; image_dilation2=eroded_image; %Image to be used for dilation for k=se_origin(1)+1:im_height-se_origin(1) for kk=se_origin(2)+1:im_width-se_origin(2) opening_
9、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))); end end figure;imshow(opening_image,[]);title('Opening Image'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Closing(Dilation first, then Erosion) %%%
10、 se=se'; image_erosion2=dilated_image; %Image to be used for erosion for 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_origi
11、n(2):k+halfwidth-1,kk-se_origin(1):kk+halfheight-1)-se)); end end figure;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
12、’);%读入图像 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(
13、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_mod
14、e, 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]);






