Reputation: 46
I am trying to understand this code
I can't get my head around what this line is doing. The flow variable is an array of flow vectors with one for each pixel in the image (so a 2d array).
fx, fy = flow[:, :, 0], flow[:, :, 1]
Any help would be appreciated
Upvotes: 1
Views: 1250
Reputation: 477883
Let us first simplify the expression. Your code:
fx, fy = flow[:, :, 0], flow[:, :, 1]
is equivalent to:
fx = flow[:, :, 0]
fy = flow[:, :, 1]
So now it boils down on what flow[:, :, 0]
. It means that flow
is a numpy array with at least three dimensions (let us define N
as the number of dimensions). Then flow[:,:,0]
is an N-1
-dimensional array, where we pick 0
always as the third dimension.
In the context of image processing, an image is usually a 3d-array (given it has color) with dimensions w
× h
× 3
(three color channels). So here it means that flow[:,:,0]
will generate a w
× h
view where for each pixel, we select the red channel (given the red channel is the first channel).
So if flow
is a 5 × 4 × 3 matrix, like:
>>> flow
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]],
[[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23]],
[[24, 25, 26],
[27, 28, 29],
[30, 31, 32],
[33, 34, 35]],
[[36, 37, 38],
[39, 40, 41],
[42, 43, 44],
[45, 46, 47]],
[[48, 49, 50],
[51, 52, 53],
[54, 55, 56],
[57, 58, 59]]])
Then we will obtain for each 3-tuple the first element, making it:
>>> flow[:,:,0]
array([[ 0, 3, 6, 9],
[12, 15, 18, 21],
[24, 27, 30, 33],
[36, 39, 42, 45],
[48, 51, 54, 57]])
and by querying flow[:,:,1]
, we obtain:
>>> flow[:,:,1]
array([[ 1, 4, 7, 10],
[13, 16, 19, 22],
[25, 28, 31, 34],
[37, 40, 43, 46],
[49, 52, 55, 58]])
mind that these are views: if you alter flow
, it will have impact on fx
and fy
as well, even if you did these assignments before.
Upvotes: 6