* This example program shows how to use optical_flow_mg to compute the optical flow in an image sequence and how to segment the optical flow.
*這個示例程序顯示了如何使用._flow_mg來計算圖像序列中的光流,以及如何分割光流。
dev_update_off ()
dev_close_window ()
* Initialize the image sequence.
初始化圖像序列
read_image (Image1, 'xing/xing000')
dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle)
* Set the display parameters for the vector field.
設置現實的矢量場參數
dev_set_paint (['vector_field',6,1,2])
定義灰度值輸出模型,
dev_set_draw ('margin')
for I := 1 to 587 by 1
? ? * Read the current image of the image sequence.
? ? ?讀連續的圖像序列
? ? read_image (Image2, 'xing/xing'+I$'03')
? ? * Compute the optical flow.
? ? ? 計算光流
? ? optical_flow_mg (Image1, Image2, VectorField, 'fdrig', 0.8, 1, 8, 5, 'default_parameters', 'accurate')
*計算兩幅圖片的光流場。
參數:1&2是輸入的兩個單色圖片序列的連續圖片,
??????????? 3為輸出的光流結果
? ? ? ? ? ?4是求取光流三種算法的選取其中的一種
? ? ? ? ? ?5高斯平滑初始偏差初始值
? ? ? ? ? ?6積分濾波偏差初始值
? ? ? ? ? ?7平滑項在數據項中的權重
? ? ? ? ? ?8梯度恒定性相對于灰度值恒定性的權重
? ? ? ? ? ?9多柵算法的名字
? ? ? ? ? 10多柵算法的變量?
? ? * Segment the optical flow vector field.
? ? 分割矢量場
? ? threshold (VectorField, Region, 1, 10000)
? ? * Display the current image of the sequence.? Note that this means that the optical flow vectors will also be displayed at the "end" of the movementof the objects in the image.
? ? 顯示序列的當前圖像。注意,這意味著光流矢量也將顯示在圖像中物體運動的“末端”。
? ? dev_display (Image2)
? ? * Display the optical flow.
? ? dev_set_color ('yellow')
? ? dev_set_line_width (1)
? ? dev_display (VectorField)
? ? * Display the segmented optical flow.
? ? dev_set_color ('green')
? ? dev_set_line_width (3)
? ? dev_display (Region)
? ? * Copy the current image to the previous image of the sequence.
? ? *將當前圖像復制到序列的前一個圖像。
? ? copy_obj (Image2, Image1, 1, 1)
endfor
背景介紹
光流的概念是Gibson在1950年首先提出來的。它是空間運動物體在觀察成像平面上的像素運動的瞬時速度,是利用圖像序列中像素在時間域上的變化以及相鄰幀之間的相關性來找到上一幀跟當前幀之間存在的對應關系,從而計算出相鄰幀之間物體的運動信息的一種方法。一般而言,光流是由于場景中前景目標本身的移動、相機的運動,或者兩者的共同運動所產生的。
當人的眼睛觀察運動物體時,物體的景象在人眼的視網膜上形成一系列連續變化的圖像,這一系列連續變化的信息不斷“流過”視網膜(即圖像平面),好像一種光的“流”,故稱之為光流(optical flow)。光流表達了圖像的變化,由于它包含了目標運動的信息,因此可被觀察者用來確定目標的運動情況。
研究光流場的目的就是為了從圖片序列中近似得到不能直接得到的運動場。運動場,其實就是物體在三維真實世界中的運動;光流場,是運動場在二維圖像平面上(人的眼睛或者攝像頭)的投影。
那通俗的講就是通過一個圖片序列,把每張圖像中每個像素的運動速度和運動方向找出來就是光流場。那怎么找呢?咱們直觀理解肯定是:第t幀的時候A點的位置是(x1, y1),那么我們在第t+1幀的時候再找到A點,假如它的位置是(x2,y2),那么我們就可以確定A點的運動了:(ux, vy) = (x2, y2) - (x1,y1)。
? ? ? ? 那怎么知道第t+1幀的時候A點的位置呢? 這就存在很多的光流計算方法了。
1981年,Horn和Schunck創造性地將二維速度場與灰度相聯系,引入光流約束方程,得到光流計算的基本算法。人們基于不同的理論基礎提出各種光流計算方法,算法性能各有不同。Barron等人對多種光流計算技術進行了總結,按照理論基礎與數學方法的區別把它們分成四種:基于梯度的方法、基于匹配的方法、基于能量的方法、基于相位的方法。近年來神經動力學方法也頗受學者重視。