目標追蹤(Object tracking)

是一類常見的影象處理問題,例如

細胞追蹤以及動物行為的追蹤

等等。

之前的文章介紹了怎樣利用ImageJ進行自動的細胞追蹤,以及動物軌跡追蹤:

但這些外掛都是根據某一類資料進行編寫的,缺乏靈活性和通用性。

而在遇到實際的追蹤問題時,根據具體情景不同,追蹤演算法實現的複雜度和難度也不同。

MATLAB科研影象處理——細胞追蹤

不同成像條件、不同運動狀況的細胞,需要不同的追蹤演算法

細胞追蹤可以分為以下幾個步驟:

分割(Segmentation)

關聯(Linking)

過濾(Filtering)

MATLAB科研影象處理——細胞追蹤

推薦教程:https://www。youtube。com/watch?v=IIt1LHIHYc4

這篇文章會介紹最基本的一類細胞追蹤問題,演示怎樣利用MATLAB(R2019a),對2D細胞進行追蹤:

MATLAB科研影象處理——細胞追蹤

資料來源:

http://

celltrackingchallenge。net

/

資料處理前分析

:這是一類比較簡單的細胞運動影象,優點在於:

1。細胞和背景之間對比度高,易於分割;

2。細胞之間沒有粘連。

難點在於細胞會出現和消失,所以在追蹤時需要區分新細胞的出現,以及細胞的消失。

二、細胞追蹤

例項程式碼

1.細胞分割

% This script shows how to track cells using

% Written by Ethan Zhao, Sept。 2021

% Tutorial: https://zhuanlan。zhihu。com/p/368919577

clear

close

all

grayThd

=

4

sizeThd

=

500

ImStack

=

imstackread

‘trackingDemo。tif’

);

ImStackBW

=

imgaussfilt

ImStack

3

>

grayThd

% binarization

% image filtering

for

ii

=

1

size

ImStackBW

3

ImStackBW

(:,:,

ii

=

imfill

ImStackBW

(:,:,

ii

),

‘holes’

);

% fill holes

ImStackBW

(:,:,

ii

=

bwareaopen

ImStackBW

(:,:,

ii

),

sizeThd

);

% remove small particles

ImStackBW

(:,:,

ii

=

imclearborder

ImStackBW

(:,:,

ii

));

% remove cells on border

end

% get center coordinates of cells

cellPos

=

cell

1

size

ImStackBW

3

));

% initialize cell array

for

ii

=

1

size

ImStackBW

3

tmpPos

=

regionprops

ImStackBW

(:,:,

ii

),

‘Centroid’

);

% get centroid

cellPos

{

ii

}

=

cat

1

tmpPos

Centroid

);

% concatenate all cells of single frame

end

save

‘cellPos-AllFrames。mat’

‘cellPos’

);

細胞追蹤的

第一步是分割出細胞,並找出細胞的中心座標,分割效果:

MATLAB科研影象處理——細胞追蹤

關鍵程式碼分析:

ImStack

=

imstackread

‘trackingDemo。tif’

);

ImStackBW

=

imgaussfilt

ImStack

3

>

grayThd

% binarization

這裡用到了之前提到的自定義函式:

這裡使用的資料比較容易分割,所以

利用高斯模糊imgaussfilt去噪

之後,可以用一個固定閾值進行分割。

cellPos

=

cell

1

size

ImStackBW

3

));

% initialize cell array

for

ii

=

1

size

ImStackBW

3

tmpPos

=

regionprops

ImStackBW

(:,:,

ii

),

‘Centroid’

);

% get centroid

cellPos

{

ii

}

=

cat

1

tmpPos

Centroid

);

% concatenate all cells of single frame

end

預先給CellPos變數分配一個空array,可以提高運算速度

這裡利用

cell array

,因為不同frame可能找到的細胞座標不同。

2.細胞關聯

clear

close

all

load

‘cellPos-AllFrames。mat’

);

distThd

=

30

% unit:pixel

minTraceLength

=

5

% cell has to exist at least 5 frames

ii

=

1

traceNum

=

1

cellTrace

=

[];

while

ii

<

length

cellPos

% traverse all frames

tmpTrace

=

[];

tmpFrame

=

cellPos

{

ii

};

% cells of current frame

if

isempty

tmpFrame

% if all cells in this frame were linked, go to next frame

disp

([

‘frame-’

num2str

ii

),

‘ finished。’

]);

ii

=

ii

+

1

continue

end

% set the first center point as seed

tmpSeed

=

tmpFrame

1

,:),

ii

];

% save as [x y t]

tmpTrace

1

,:)

=

tmpSeed

% set current seed as trace start

cellPos

{

ii

}(

1

,:)

=

[];

% once seed is linked, remove it

% search every frame after that seed

for

jj

=

ii

+

1

length

cellPos

nextFrame

=

cellPos

{

jj

};

minDist

cellIdx

=

min

pdist2

tmpSeed

1

2

),

nextFrame

));

% search the closest cell

if

minDist

<

distThd

% the next cell is close enough, the seed can be linked

tmpSeed

=

nextFrame

cellIdx

,:),

jj

];

% set this cell as new seed

tmpTrace

=

cat

1

tmpTrace

tmpSeed

);

% add this cell to the trace

cellPos

{

jj

}(

cellIdx

,:)

=

[];

% remove the linked seed

else

% if the trace is finished, stop searching

break

end

end

if

size

tmpTrace

1

>

=

minTraceLength

% trace filtering

cellTrace

{

traceNum

}

=

tmpTrace

traceNum

=

traceNum

+

1

end

end

save

‘cellTrace。mat’

‘cellTrace’

);

這裡細胞關聯用到的是最簡單的

歐氏距離

,透過尋找下一幀離細胞最近的那個細胞,進行Linking。

從一幀開始,每一個細胞作為一個種子(seed),往後面的幀進行搜尋。

如果搜尋不到符合條件的下一個細胞,則該細胞的軌跡(trace)結束。

MATLAB科研影象處理——細胞追蹤

示意圖

if

size

tmpTrace

1

>

=

minTraceLength

% trace filtering

cellTrace

{

traceNum

}

=

tmpTrace

traceNum

=

traceNum

+

1

end

這裡也包含了軌跡過濾(trace filtering),如果這個trace出現時間過短(少於5幀),那麼這一條trace很可能並不正確。

Trace filtering有很多特徵可以進行考慮,例如細胞的速度、運動方向等等。

如果對於MATLAB影象處理有什麼問題可以私信我,或者給我發郵件:zhaoyc9@163。com

更多影象處理的教程可以關注我的專欄:

希望對大家有幫助~