您當前的位置:首頁 > 攝影

使用plt.contour 繪製配準後圖像的形變場(Deformation Field)

作者:由 毛毛Timmy 發表于 攝影時間:2020-06-09

2019-3-17 更新

第一次寫部落格,寫得不夠好,還存在一些問題,而且僅提供了一個函式,沒有測試程式碼,在此做一下糾正與補充。我之前寫的繪製變形場是在PyTorch框架下的一個函式,對於其他框架的不夠友好,這次我修改成使用numpy的函式,方便大家使用。

下面直接奉上修改後的程式碼:

import

matplotlib。pyplot

as

plt

import

numpy

as

np

def

grid2contour

grid

):

‘’‘

grid——image_grid used to show deform field

type: numpy ndarray, shape: (h, w, 2), value range:(-1, 1)

’‘’

assert

grid

ndim

==

3

x

=

np

arange

-

1

1

2

/

grid

shape

1

])

y

=

np

arange

-

1

1

2

/

grid

shape

0

])

X

Y

=

np

meshgrid

x

y

Z1

=

grid

[:,:,

0

+

2

#remove the dashed line

Z1

=

Z1

[::

-

1

#vertical flip

Z2

=

grid

[:,:,

1

+

2

plt

figure

()

plt

contour

X

Y

Z1

15

colors

=

‘k’

plt

contour

X

Y

Z2

15

colors

=

‘k’

plt

xticks

(()),

plt

yticks

(())

#remove x, y ticks

plt

title

‘deform field’

測試

在影象配準中,將規則的空間網格,加上預測出的變形場,得到取樣網格,對浮動影象(moving image)進行取樣,即得預測的配準影象。這一過程牽涉到了變形場的視覺化,具體流程示意圖如下:

使用plt.contour 繪製配準後圖像的形變場(Deformation Field)

1,規則網格視覺化測試程式碼:

img_shape

=

40

80

x

=

np

arange

-

1

1

2

/

img_shape

1

])

y

=

np

arange

-

1

1

2

/

img_shape

0

])

X

Y

=

np

meshgrid

x

y

regular_grid

=

np

stack

((

X

Y

),

axis

=

2

grid2contour

regular_grid

輸出影象:

使用plt.contour 繪製配準後圖像的形變場(Deformation Field)

2,取樣網格視覺化測試程式碼:

rand_field

=

np

random

rand

*

img_shape

2

rand_field_norm

=

rand_field

copy

()

rand_field_norm

[:,:,

0

=

rand_field_norm

[:,:,

0

*

2

/

img_shape

1

rand_field_norm

[:,:,

1

=

rand_field_norm

[:,:,

1

*

2

/

img_shape

0

sampling_grid

=

regular_grid

+

rand_field_norm

grid2contour

sampling_grid

輸出影象:

使用plt.contour 繪製配準後圖像的形變場(Deformation Field)

值得注意的三點:

1,使用規則網格(regular grid),對浮動影象進行取樣得到的影象與浮動影象相同,而使用包含形變資訊的取樣網格(sampling grid),得到的是變形後的影象。如上兩圖示意。

2,grid2contour函式的輸入grid,實際上是歸一化的變形場(deformation field或者稱為dense displacement vector field),歸一化之前,其中的值,指的是對應座標畫素的位移。

3,之所以網格要進行

歸一化

,將取樣網格的值縮放到[-1, 1]之間,這是因為我沿用的PyTorch中grid_sample函式的規則,呼叫該函式時,輸入的是deformation field,對應於上述程式碼中的rand_field_norm,它要求grid的值大部分的值在[-1, 1]之間,當x=-1,y=-1時,對應於影象的左上畫素,而x=1,y=1,對應於影象的右下畫素。原文如下:

grid should have most values in the range of [-1, 1]。 This is because the pixel locations are normalized by the inputspatial dimensions。 For example, values x = -1, y = -1 is the left-top pixel of input, and values x = 1, y =1 is the right-bottom pixel of input。

參考文獻

1。 圖片來自 Siyuan Shan, Wen Yan, Xiaoqing Guo, et al。 Unsupervised End-to-end Learning for Deformable Medical Image Registration。 arXiv:1711。08608v2 [cs。CV], 2018。 arXiv: 1711。08608v2。

2,pytorch grid_sample():

https://

pytorch。org/docs/stable

/nn。html#grid-sample

版權說明

原文作者為本人,首發於我的CSDN部落格,如有興趣瞭解更多配準相關的內容,請移步本人的部落格。

以下內容,你可能感興趣:

我的GitHub倉庫(

Timmy-Fang/

Deformable-Image-Registration-Projects)

我的bilibili影片

(配準講解影片已上傳,關注我的B站賬號:

方便及時獲取更新內容,謝謝~)

標簽: Grid  field  rand  shape  np