matlab经典代码大全
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
哈哈哈
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 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'); %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_origin(1),kk-se_origin(2))=max(max(se+image_dilation(k-se_origin(1):k+halfh eight-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)
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'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 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_image(k-se_origin(1),kk-se_origin(2))=max(max(se+image_dilation2(k-se_origin(1):k+hal fheight-1,kk-se_origin(2):kk+halfwidth-1)));
end
end
figure;imshow(opening_image,[]);title('Opening Image'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Closing(Dilation first, then Erosion) %%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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_origin(2):k+halfwidt h-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’);%读入图像
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');