2008年9月29日 星期一

FOPEN: Open file

FOPEN 這個指令和 C 語言的用法並沒有甚麼不同, 傳回值則是一個整數,
FID = FOPEN(FILENAME)
opens the file FILENAME for read access.
FILENAME can be a MATLABPATH relative partial pathname. If the file is opened for reading and it is not found in the current working directory, FOPEN searches down MATLAB's search path.

FID is a scalar MATLAB integer, called a file identifier. You use the fid as the first argument to other file input/output routines. If FOPEN cannot open the file, it returns -1.
當 MATLAB 無法依照指令找到要開啟的檔案時, 就會傳回 -1, 寫程式時應該特別針對傳回值為 -1 時, 做特別處理。例如:
fid=fopen(filename);
if fid==-1
 error('File does not exist or is not in your MATLAB path.');
end;
就如同 C 語言一樣, FOPEN 指令也可以根據不同的需求, 設定不同的開啟模式。
FID = FOPEN(FILENAME,PERMISSION)
opens the file FILENAME in the mode specified by PERMISSION.
PERMISSION can be:
 'r'  read
 'w'  write (create if necessary)
 'a'  append (create if necessary)
 'r+'  read and write (do not create)
 'w+'  truncate or create for read and write
 'a+'  read and append (create if necessary)
 'W'  write without automatic flushing
 'A'  append without automatic flushing

REPMAT: Replicate and tile an array

REPMAT 指令可以拆解成 replicate matrix, 顧名思義就是複製矩陣的意思,
B = repmat(A,M,N) creates a large matrix B consisting of an M-by-N tiling of copies of A.
將 matrix A 複製 M * N 個, 並以 M 行 N 列的方式排列。舉例來說:
A=[1, 5];
B=repmat(A, 3, 2);
B = [1, 5, 1, 5; 1, 5, 1, 5; 1, 5, 1, 5]

注意: 上述式子中的分號代表換行的意思, 是 MATLAB 矩陣的輸入語法。

2008年9月28日 星期日

IMFINFO:Information about graphics file

透過 IMFINFO 這個指令, 我們可以獲得關於影像檔案的相關資訊。
inf = imfinfo('twins.tif');
觀察 workspace 中的變化,
 
我們發現 IMFINFO 傳回值都放到一個特定型態的結構之中。連按 inf 兩下, 就可以打開 Array Editor 視窗, 觀察 inf 的實際內容。

 
 

SIZE: Size of array

SIZE 這個指令傳回陣列的大小。我們用以下的例子來說明:
x=imread('twins.tif');
s=size(x);
 
 從圖片中我們可以看到 s 的值為 [256, 256, 3], 剛好就是 x 陣列的大小。
 

PIXVAL: Display information about image pixels

PIXVAL('ON') 可以將像素色彩值顯示在圖表視窗(figure window)之中。
figure;
imshow(w);
pixval('on');


我們可以看到 figure 1 底下多了一行反白顯示, 當我們把滑鼠移到圖片上時, 就可以看到圖片上有一個 " + " 的游標, 然後反白顯示列會出現游標的相對應位置與該像素的色彩值。

IMSHOW: Display image

IMSHOW 指令就是將影像顯示在 figure window 之中。不過, 由於影像的格式與類型實在是太多, 太複雜了, 因此, 如果你在 command window 之中, 用 help 查詢 IMSHOW 指令, 你會看到非常多種下不同參數的 IMSHOW 指令。若從另一種角度來看, 這不正也顯示出 IMSHOW 指令的功能強大嗎?
imshow(w)

FIGURE: Create figure window

FIGURE, by itself, creates a new figure window, and returns its handle.
FIGURE(H) makes H the current figure, forces it to become visible, and raises it above all other figures on the screen. If Figure H does not exist, and H is an integer, a new figure is created with handle H.
FIGURE 就是建立一個放置圖片或圖表的視窗。一般來說, 如果你要顯示一張圖片, 直接打
imshow(w)
MATLAB 會自動產生一個 figure 1 視窗, 然後直接將 w 這張圖片放上去,



如果我們繼續鍵入
imshow(x)
MATLAB 會將圖片 x 放到 figure 1 視窗, 取代原先的圖片 w ,



若想要將圖片 x 放到 figure 2 之中, 就必須先用 FIGURE 指令產生一個新的 figure window 然後再執行 imshow(x) 指令。
imshow(w);
figure, imshow(x);
 

IMREAD: Read image from graphics file

把 IMREAD 這個指令拆解, 就是 image read, 就是從影像檔案中讀入影像資料到 workspace 中, 在 MATLAB 整合環境的 Command Window 中鍵入 help imread, 你會看到關於 IMREAD 指令的完整說明。底下就是出現在說明文字中的第一段:
A = IMREAD(FILENAME,FMT) reads a grayscale or color image from the file specified by the string FILENAME, where the string FMT specifies the format of the file.
我們直接用例子說明。請下載範例影像 wombats.tiftwins.tif 後, 在 Command Window 鍵入:
w=imread('wombats.tif');
你會發現在 workspace 視窗中, 已經多了一列資料, 告訴你 w 這個矩陣變數的大小就是 256 * 256, 每個元素的資料型態是 uint8, 換句話說就是長度為 8 位元的 unsigned integer, 沒有正負號的整數。因此, 可表示的數值範圍為 0 ~ 255 的正整數。

 
由於 wombats.tif 是一張灰階影像 ( grayscale image ), 因此, w 是一個二維矩陣。接著, 我們鍵入
x=imread('twins.tif');
觀察 workspace 就會發現多了一個三維陣列 x。