Try   HackMD

Lecture - 動畫影像

Recall: Matlab-04-作圖

References Matlab commands

  • plot
  • hold on; hold off
  • axis

動畫

下列這個是函數數列

fn(x)=sin(nx),0x2π,nN
給定一個正整數
n
, 就會是一個函數. 通常我們想知道這數列會收斂到哪個函數去.

在學習進階的如何判斷收斂之前, 也許我們想先用眼睛看看這函數數列究竟長什麼樣子. 一種做法是把他們畫出來. 以下我們畫出

n=1,,10 這十個函數:

x = linspace(0, 2*pi); figure; hold on for ii=1:10 plot(x, sin(ii*x)); end

很明顯當十個函數畫一起時, 實在什麼都看不出來. 因此我們試試將剛剛的 hold on 指令拿掉, 一個一個畫:

x = linspace(0, 2*pi); figure for ii=1:10 plot(x, sin(ii*x)); end

會發現, 跟我們預期的有點不一樣(似乎只畫最後一張?).

如果要它馬上畫出來, 需要另外下一個drawnow (馬上畫) 的指令

x = linspace(0, 2*pi); figure for ii=1:10 plot(x, sin(ii*x)); drawnow end

這樣它就會真的找我們回圈順序一張一張畫出來了 (不過還是很快, 看不太清楚).

要每張畫之間時間間隔久一點的話, 可以手動的加入 pause (暫停), 例如以下我們要求圖與圖之間間隔

0.1 秒:

x = linspace(0, 2*pi); figure for ii=1:10 plot(x, sin(ii*x)); drawnow; pause(0.1) end

Exercise

試試畫

n=1,,100 看會發生什麼事

會看到所謂的 aliasing 現象!

References

影像讀取

References

matlab 中最基本影像是個

n×m 矩陣, 矩陣的每一個元素則是個正整數. 每個正整數會對應到一種顏色, 因此會有一個顏色對照表, 是個
p×3
的矩陣.

如以下 matlab 內建小丑影像:

load clown.mat % 載入小丑影像資料,含變數 X 和 map image(X); % 顯示影像 colormap(map) % 取用色盤矩陣

其中 X 是個

200×320 矩陣, 而這矩陣的元素值在
1
81
這範圍之內, 因此有個 map 是個
81×3
的顏色矩陣.


另一種存照片的方式則是直接存成

n×m×3 的矩陣, 例如我們下載以下圖片:
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
.
接著載入並畫出:

X = imread('Matlab_Logo.png'); % 載入影像 image(X); % 顯示影像

Exercise

載入一張照片後, 將影像由左至右逐條顯示出來, 範例如下:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Recall: [X,Y,BUTTON] = ginput(N)

[X,Y,BUTTON] = ginput(1);

Assignment

載入一張照片後

  1. 讓使用者以游標點選照片某位置, 將選取的位置附近某範圍皆以一特定顏色取代.
  2. 讓使用者以鍵盤上下左右鍵控制方向, 模擬板擦將移動到的位置附近某範圍皆以一特定顏色取代