1 / 49

Matlab with DIP 教學

Matlab with DIP 教學. 師大資工所助教 羅安鈞 s0914742@mail.ncyu.edu.tw. What is matlab. MATLAB is a high-performance language for technical computing. MATLAB is an interactive system. File format : M-file i.e. *.m . Matlab 介面. 介面說明. Workspace : 顯示目前工作環境內定義的變數 Command Window :

kairos
Download Presentation

Matlab with DIP 教學

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 with DIP 教學 師大資工所助教 羅安鈞 s0914742@mail.ncyu.edu.tw

  2. What is matlab • MATLAB is a high-performance language for technical computing. • MATLAB is an interactive system. • File format : M-file i.e. *.m

  3. Matlab介面

  4. 介面說明 • Workspace: 顯示目前工作環境內定義的變數 • Command Window: 就如同一個文字操作介面,有 >> 提示號,在提示號之後輸入指令 • Command History 紀錄曾經下過的指令

  5. 介面說明(cont.) • Current Directory Window 預設路徑C:\MATLAB\Work 當你的圖片檔案或M檔案至於此路徑時, 使用時只需呼叫檔名,不必給予檔案路徑。

  6. 指令操作 • 在command window輸入指令 • 按下 [Enter] 就執行計算並且顯示答案 • Matlab不在乎空格 ex: 2*3-4 等同於 2 * 3 - 4 • 內建Function不分大小寫 ex: imread() 等同於 IMREAD()

  7. 指令操作 • 自訂變數有分大小寫 ex: 指令 x=0不等於X=0 • 分號(;)接於指令之後: 功用:不顯示結果(有存起來) ex: x=[0 1 2]//向量ex: x=[0 1 2]; 顯示不顯示 x= 0 1 2

  8. 影像處理常用數學函式 • 計算平方根 (square root),指令是 sqrt( ) • 絕對值 (absolute value),指令是abs( ) • ( ) 裡面可以有其他的運算 ex : sqrt(1+2*3) sqrt(sqrt(16))

  9. 其他常用指令 • 冒號指令:可以從「頭」到「尾」產生一序列的數。 ex: 1:5 製造了序列 1 2 3 4 5 • 折線圖plot(): 用法:plot(x,y),其中 x 和 y 是維度相同的序列或向量。 ex: x=[0 1 2]; y=[0 1 0]; plot(x,y) 畫出從 (0,0) 到 (1,1) 到 (2,0) 的兩條折線

  10. 其他常用指令(cont.) • 軸線axis( ) : 用法: axis(v1 v2 v3 v4) 意思是在橫軸 v1 與 v2 之間,縱軸 v3 與 v4 之間,呈現圖形

  11. For loop • 用法:for 變數 = 向量或序列, 計算, end ex: for i = 1:100, ex: for i=1:2:100 , y(i) =2; s = s+i; End end ex: for x = [0 1 5 3], disp(x); //印出 x end

  12. If Else • 用法:if (True or False), 計算, end ex: if (isempty(t)) t = 0; End • 用法: if (True_or_False),計算, else,計算, end

  13. The MATLAB Image Processing Toolbox • The Image Processing Toolbox is a collection of MATLAB functions (called M-functionsor M-files) that extend the capability of the MATLAB environment for the solution of digital image processing problems.

  14. The MATLAB Image Processing Toolbox(cont.) • Including: -Spatial image transformations -Morphological operations -Neighborhood and block operations -Linear filtering and filter design -Transforms - Image analysis and enhancement -Image registration -Deblurring -Region of interest operations

  15. How do I know M-function? • Find it in Matlab Help. -by category. -by alphabetical order. • Find it on the textbook.

  16. Matlab 內建影像 • C:\MATLAB7\toolbox\images\imdemos • 皆為Matlab Help中範例的原始影像。 • 使用時只需直接在指令中輸入檔名,即可使用。 • 適用於觀察影像處理結果

  17. Different Image Types • Indexed images • Intensity (grayscale) images • Binary images • RGB (true-color) images

  18. Reading an image • imread() • 功用:將影像載入並存成array格式備用 • 用法:[I,map] = imread(filename); I = imread(filename); ex: I = imread('pout.tif'); I為指向影像的變數 不指定變數,則為ans

  19. Displaying an image • imshow() • 功用:開啟一個視窗顯示影像 用法: imshow(I) imshow(I,map) • Figure, imshow() 功用:開啟一個新視窗顯示影像 用法: figure,imshow(I)

  20. Displaying an image(cont.) • imshow(I, [low, high]) • imshow(I, [ ]) 功用:displays I as a grayscale intensity image, specifying the data range for I. The minimum value in I is displayed as black, and the maximum value is displayed as white.

  21. Displaying an image(cont.) • Spatial domain

  22. Displaying an image(cont.) • pixval : 功能:cursor on image to show pixel values 用法: imshow(I),pixval

  23. Displaying an image(cont.) • colorbar • 功能:To display an image with a colorbar that indicates the range of intensity values. • 用法: imshow(I), colorbar ex: I = imread('pout.tif'); imshow(I) , colorbar

  24. Writing an image • imwrite() • 功能:將影像寫入成檔案 • 用法: imwrite(I,filename,format) ex: imwrite(I,'pout.jpg‘,’JPEG’);

  25. Image information • Image size: size() ex: I= imread('saturn.png'); size(I) [M,N] = size(I) M=影像I的高 N=影像I的寬

  26. Image information • whos 功用:display information about an image . • ex: whos I • Imfinfo( filename ) 功用: display information about image file . • ex: info = imfinfo('saturn.png')

  27. Digital Image processing • 影像二元化 • g = im2bw(I, T); • 功用:Convert intensity image I to binary image g using threshold T, where T must be in range [0, 1]. ex: I= imread('pout.tif'); g = im2bw(I, 0.4); imshow(g) ,colorbar

  28. Digital Image processing(cont.) • 彩色轉灰階 • Rgb2gray() • 功用:將RBG彩色影像轉換成gray-level影像。ex: I= imread ('saturn.png'); g = rgb2gray(I); imshow(g), colorbar

  29. Digital Image processing(cont.) • 反相 • imcomplement( ) • 功用:The negative of an image. ex: J = imcomplement(g); imshow(J),

  30. Digital Image processing(cont.) • 變更影像大小 • imresize(I,scale,method); • 功用:To change the size of an image. • interpolation Method: -'nearest‘ :Nearest-neighbor interpolation -'bilinear‘ :Bilinear (the default) -'bicubic‘ :Bicubic interpolation

  31. Digital Image processing(cont.) • ex: I = imread('circuit.tif'); J = imresize(I,1.25); imshow(I) figure, imshow(J) • ex: I = imread('circuit.tif'); J = imresize(I,[100 150], 'bilinear'); imshow(I) figure, imshow(J)

  32. Digital Image processing(cont.) • 旋轉影像 • imrotate(I, angle); • 功用:To rotate an image. • ex: I = imread('pout.tif'); J = imrotate(I,35); imshow(J)

  33. More Example • Using affine transformation 1.Rotation 45 degree I=imread('spokes1.bmp'); T = maketform('affine',[cosd(45) -sind(45) 0; sind(45) cosd(45) 0; 0 0 1]); tformfwd([10 20],T); I2 = imtransform(I,T); imshow(I2)

  34. More Example(cont.) 2.Translation I=imread('spokes1.bmp'); move = translate(strel(1), [25 30]); H = imdilate(I,move); imshow(H)

  35. More Example(cont.) 3.shear I=imread('spokes1.bmp'); T = maketform('affine',[1 0 0; 1 2 0; 0 0 1]); tformfwd([10 20],T); I2 = imtransform(I,T); imshow(I2)

  36. More Example(cont.) • Edge detectors 1.Sobel edge detector Code://Both horizontal and vertical I=imread('bridge.jpg'); BW=rgb2gray(I); BW2=edge(BW,'sobel',0.02,'both'); figure,imshow(BW2)

  37. More Example(cont.) 2.the Canny method low threshold: 0.04,high threshold: 0.10 , sigma is 1 Code: I=imread('bridge.jpg'); BW=rgb2gray(I); BW4 = edge(BW,'canny',[0.04 0.10],1); figure,imshow(BW4)

  38. More Example(cont.) • Canny 以 C語言來實作會困難很多!! Canny .txt

  39. More Example(cont.) • Filter(mean filter) Code: I = imread('coins.png'); h = ones(5,5) / 25; I2 = imfilter(I,h); imshow(I), title('Original Image'); figure, imshow(I2), title('Filtered Image')

  40. More Example(cont.) • Filter(unsharp masking filter ) Code: I = imread('cameraman.tif'); h = fspecial('unsharp'); I2 = imfilter(I,h); imshow(I), title('Original Image') figure, imshow(I2), title('Filtered Image')

  41. M-function 不夠用怎麼辦? • 自己寫。 • 在網路上尋找高手的package。 • 當找到新的M-files時,需幫它設定路徑,才可以像一般內建function直接使用。

  42. Set path

  43. Set path (cont.)

  44. 使用外掛程式 範例:使用Median Filter消除影像雜訊 Code: I=imread('510a.jpg'); imshow(I) F=SPFILT(I,'median',3,3); figure,imshow(F) F2=SPFILT(F,'median',3,3); figure,imshow(F2) F3=SPFILT(F2,'median',3,3); figure,imshow(F3)

  45. MATLAB Compiler • MATLAB Compiler 能讓您將MATLAB 程式轉換為可單獨執行的應用程式和軟體元件,並分享給其他使用者。 • MATLAB Compiler 可免除您經由手動編寫方式將 MATLAB 程式碼轉譯為 C 或 C++ 的程序。 • 要再安裝MATLAB Component Run-time (MCR)元件等。

  46. MATLAB Compiler (cont.) • C to Matlab • 設定了合適的編譯器,matlab 會自動幫我們編譯這個程式。 • Example: hello world • 指令: -編譯:mex filename ex: mex hello.c -執行: filename ex: hello

  47. MATLAB Compiler (cont.) hello.c #include <stdio.h> #include "mex.h" void mexFunction(int nlhs,mxArray *plhs[], int nrhs, const mxArray *prhs[]){ printf("Hello, world."); } • mex.h 定義了所有 Matlab 和 C 溝通所用到的 subroutine。 • mexFunction 是程式的進入點,等價於 ANSI C 的 main。

  48. 參考書籍 • Digital Image Processing Using MATLAB by Rafael C. Gonzalez , Richard E. WoodsSteven L. Eddins

  49. 參考網站 • http://libai.math.ncu.edu.tw/bcc16/B/matlab/index.shtml

More Related