1 / 30

Matlab 介绍

Matlab 介绍. 陈雷 2008-04-05. Matlab 基础. Matlab 图像操作基础. Matlab 图像变换. 简介. Matlab 基础. 常用运算符 +, -, *, ^, /( 右除 ), ( 左除 ) 一维数组创建 直接创建 x = [1,2,3,4,5,6,7] X = 初值 : 终值, x = 10:20 X = 初值 : 增量 : 终值 , x = 0:0.1:1 X = linspace( 初值,终值, n), x = linspace(0,pi,11)

mahala
Download Presentation

Matlab 介绍

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Matlab介绍 陈雷 2008-04-05

  2. Matlab基础 Matlab图像操作基础 Matlab图像变换 简介

  3. Matlab基础 • 常用运算符 • +, -, *, ^, /(右除), \(左除) • 一维数组创建 • 直接创建 x = [1,2,3,4,5,6,7] • X = 初值:终值,x = 10:20 • X = 初值:增量:终值, x = 0:0.1:1 • X = linspace(初值,终值,n), x = linspace(0,pi,11) • X = logspace(初值,终值,n), x = logspace(0,1,11) • 一维数组的访问和赋值 • 用括号来表示下标:x(n)表示访问x的第n个下标 • X([1 2 3]) 或者 x([1,2,3]) • X(1:3) • X(3:end) • 修改 • X(3) = 0, x([1 4]) = [1 1]

  4. Matlab基础 • 二维数组 • A = [1,2,3;3,4,5] • A(2,1) : 访问A的第二行,第一列 • 特殊数组 • 全1数组 • ones(n): 创建一个nXn的1数组 • ones(r,c): 创建一个rXc的1数组 • 全0数组 • zeros(n) • zeros(r,c) • 单位矩阵 • eye(4,4)

  5. Matlab基础 • 数组运算(数组运算是元素与元素之间的运算,与矩阵运算不同) • +  加 • -  减 • .*  乘积 • ./  左除 • .^  乘幂 • 矩阵运算 • +  加 • -  减 • *  乘积 • /  左除 • ^  乘幂 • ‘  转置

  6. Matlab6 语言结构与编程 • Matlab程序文件 • .m文件 • 命令文件,一组相关命令编辑在同一个ASCII码命令文件中。 • 运行时,只需要输入文件名字(没有.m后缀),Matlab就会自动按顺序执行文件中的命令 • 第一句可执行语句是以function引导的定义语句 • 不需要用end 作为M文件的结束标志

  7. Matlab6 语言结构与编程 %logotu.m load logo surf(L,R),colormap(M) n=size(L,1); axis off axis([1 n 1 n -.2 .35]) view(-37.5,60) --保存为logotu.m >>logotu

  8. Matlab6 语言结构与编程 %fibfun.m function f=fibfun(n) if n>2 f=fibfun(n-1)+fibfun(n-2); else f=1; end >>fibfun(3) Ans = 2

  9. Matlab6 语言结构与编程 • 数据类型 • double • char • sparse • unint8 • cell • struct

  10. Matlab6 语言结构与编程 • 程序结构 • 顺序结构 • 循环结构 • for.end循环 for x = array {command} end >>for i = 1:10; y(i) = I; end; >>y y = 1 2 3 4 5 6 7 8 9 10

  11. Matlab6 语言结构与编程 • while.end循环 while expression {command} end >> a(1) = 1; a(2) = 1; i = 2; >> while a(i) <= 10000 a(i + 1) = a(i - 1) + a(i); i = i + 1; end; >> i, a(i), i = 21 ans = 10946

  12. Matlab6 语言结构与编程 • 选择结构 if expression {command} end function f = pdbsline(x) if x < 0 f = 0; elseif x < 1 f = x; elseif x < 2 f = 2 - x; else f = 0 end

  13. Matlab6 语言结构与编程 • 分支结构 switch switch_expr case case_expr statement, …, statement case {case_expr1, case_expr2, case_expr3,…}, statement, …, statement … otherwise, statement,…,statement end

  14. Matlab6 语言结构与编程 function f = casefun(method) switch lower(method) case {'linear','bilinear'}, disp('method is linear') case 'cubic', disp('method is cubic') case 'nearest', disp('method is nearest') otherwise, disp('Unknwn method.') end >> casefun('linear') method is linear >> casefun('cubic') method is cubic >> casefun('cub') Unknwn method.

  15. Matlab图像操作基础 • Matlab图像类型 • 真彩色图像 • 又称RGB图像 • 一个尺寸为m X n 的彩色图像,在matlab中存储为m X n X 3的多维数组 • 索引图像 • 灰度图像 • 二值图像

  16. Matlab图像操作基础 • 转换图像类型 • im2bw函数,通过设置亮度阈值将真彩色图转换成二值图 BW = im2bw(RGB, level), level为归一化阈值 load trees BW = im2bw(X, map, 0.4) imshow(X, map) figure, imshow(BW)

  17. Matlab图像操作基础 • mat2gray函数,用于将一个数据矩阵转换成一幅灰度图 • I = mat2gray(A) I = imread('rice.tif') %产生sobel算子,并且用sobel算子对图像I进行滤波 J = filter2(fspecial('sobel'),I); %将数据矩阵转换成灰度图像 K = mat2gray(J); imshow(I) figure,imshow(K)

  18. Matlab图像操作基础 • rgb2gray函数 • 用于将一幅真彩图像转换成灰度图像 • I = rgb2gray(RGB) • rgb2ind函数 • 用于将真彩图转换成索引图像 • I = rgb2ind(X, map)

  19. Matlab图像操作基础 • 读写图像文件 • imread函数 • 用于读取各种图像文件 • A = imread(filename, format) • imwrite函数 • 用于输出图像 • imwrite(A, filename,format) • Imfinfo函数 • 用于读取图像文件的有关信息 • info = imfinfo(filename,format)

  20. Matlab图像操作基础 • 图像文件的显示 • Image函数 • 最原始的图像显示函数 • Image(X); colormap(map); load clown image(X) colormap(map) axis equal axis('cff')

  21. Matlab图像操作基础 • imshow函数 • 最常用的显示各种图像的函数 • imshow(I, [low, high]) – 用于显示灰度图像,[low, hight]为图像数据的值域,如果不清楚值域用[] • Imshow(RGB) – 用于显示真彩图像 I = imread('rice.tif') %用模板[1 2;-1 -2]对图像滤波 J = filter2([1 2;-1,-2],I) imshow(I) %由于滤波后图像灰度范围与绿波前不同,所以用[]来作为参数 figure, Imshow(J,[])

  22. Matlab图像操作基础 • subimage函数 • 用于在一个图像窗口内使用多个调色板,使得各种图像能在同一窗口中显示 • subimage(X,map), subimage(RGB)… load trees X2 = X map2 = map load clown subplot(1,2,1),subimage(X, map); subplot(1,2,2),subimage(X2,map2)

  23. Matlab图像操作基础 • 图像块操作 • 图像块操作是将图像的数据矩阵划分为同样大小的矩行区域操作,他是图像分析和图像压缩的基础 • bestblk函数用于选择图像的尺寸 • Size = bestblk( [m n], k) • 返回对尺寸为mXn的图像块的划分size,k为图像块划分长度和宽度的最大值 • [mb, nb] = bestblk([m n], k) • Mb, nb分别为图象块划分的行数和列数

  24. Matlab图像操作基础 • blkproc函数 • 通用的图象块操作函数 • B = blkproc(A, [m n], function), B = blkproc(A, [m n], function, P1, P2,…) P1,P2为function中除x以外的其他参数 %计算图像快局部标准差 I = imread('tire_orig.bmp'); f = inline('uint8(round(std2(x) * ones(size(x))))'); I2 = blkproc(I, [8 8],f); imshow(I) figure, imshow(I2, [])

  25. Matlab图像变换 • 傅立叶变换 • Matlab中提供fft, fft2,fftn三个函数,分别用于计算一维,二维和n维离散傅立叶变换 • fft2(I), fft2(I, m, n) • 通过对图象I剪切或者补全,按用户指定的点数计算fft, 返回矩阵的大小为mXn • fftn(I), fftn(I, size) • fftshift(I) • 将变换后的图象频谱从举证的圆点移到矩阵的中心 • ifft2(I), ifft2(I, m, n) • ifftn(I), ifftn(I,size)

  26. Matlab图像变换 • 快速傅立叶变换例子 %计算图像的二维傅立叶变换 %并显示起幅值的结果 load imdemos saturn2 imshow(saturn2) B = fftshift(fft2(saturn2)) %很多MATLAB图像显示函数无法显示复数图像 %为了观察图像傅立叶变换后的结果 %应该对变换后的结果求模,调用abs() figure, imshow(log(abs(B)), []); colormap(jet(64)); colorbar

  27. Matlab图像变换 • 离散余弦变换 • dct2(A), dct2(A, m, n): 实现二维离散余弦变换 • Idct2(A), idct2(A, m, n):逆变换 load imdemos saturn2 imshow(saturn2) B = dct2(saturn2) figure, imshow(log(abs(B)), []); colormap(jet(64)); colorbar

  28. Matlab图像变换 • dctmtx函数 • 返回dct变换矩阵 • D = dctmtx(n), 返回nXn的DCT变换矩阵 • 如果矩阵A的大小为nXn, D*A是A矩阵每一列的变换值,D’*A为A每一列的逆的变换值,如果矩阵A为nXn的方阵,则A的DCT变换可以利用D*A*D’计算。

  29. Matlab图像变换 • 离散余弦变换和图象压缩 I0 = imread('lena2.jpg'); I0 = im2double(I0); I = RGB(:,:,1); %产生二维DCT变换矩阵 T = dctmtx(8); %计算二维DCT B = blkproc(I, [8 8], 'P1*x*P2',T,T'); %二值掩膜,用来压缩DCT的系数 mask = [ 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; %只保留DCT变换的10个系数 B2 = blkproc(B, [8 8], 'P1.*x',mask); %逆DCT变换,用来重构图像 I2 = blkproc(B2, [8 8], 'P1*x*P2',T',T); imshow(I); figure, imshow(I2);

  30. Thank You !

More Related