Why does this order of the Gaussian filter in scipy give the x and y derivative?

Multi tool use
Why does this order of the Gaussian filter in scipy give the x and y derivative?
I'm using a Gaussian filter with Scipy and I saw this code online which I'm curious about.
imx = zeros(im.shape)
filters.gaussian_filter(im, (sigma,sigma), (0,1), imx)
imy = zeros(im.shape)
filters.gaussian_filter(im, (sigma,sigma), (1,0), imy)
For the first Gaussian filter call, the order is (0,1) and according to this link, that should give the the first order derivative of a Gaussian in y-direction. However, on running the code, I can see that the Gaussian is along the X direction.
The same thing applies to imy. Why does the code work that way?
For reference, running:
filters.gaussian_filter(im, (sigma, sigma), (0, 1), output= imx)
on this array:
[[0 3 2]
[1 4 1]
[3 4 2]]
Returns:
[[0.00071801 0.00148952 0.00077151]
[0.0006947 0.00144284 0.00074815]
[0.00067141 0.00139622 0.00072482]]
Which is a Gaussian in the x direction, even though the order (0, 1) suggests that it should be in the y direction. I think I might be missing something.
y
x
imx[y][x]
I'm sorry, I don't think I understand you. This where I saw the code. I understand that the dimension of the array is imx[columns(y)][rows(x)]. Does the same apply for the 'order' parameter of the Gaussian filter class? Does (0, 1) mean (columns, rows) in that sense?
– Timi Adeniran
Jul 1 at 22:28
I reviewed it, let me ask you this, what direction, x or y, would you expect the gaussian of
[[1,1,1],[2,2,2],[3,3,3]]
to be nonzero? (Just from the matrix, without running the code)– jedwards
Jul 1 at 22:29
[[1,1,1],[2,2,2],[3,3,3]]
I expect it to be nonzero in the y direction.
– Timi Adeniran
Jul 1 at 22:34
Does the graphical image display maybe transpose the image? Maybe they think of the row index as being x?
– Cris Luengo
Jul 2 at 1:36
1 Answer
1
SciPy works with arrays, not with images. Arrays don't have x-coordinates and y-coordinates, they have indexes. Thus, the behavior of SciPy is explained in terms of indexes:
order : int or sequence of ints, optional
The order of the filter along each axis is given as a sequence of integers, or as a single number. An order of 0 corresponds to convolution with a Gaussian kernel. A positive order corresponds to convolution with that derivative of a Gaussian.
So, [0, 1]
is the derivative in the direction of the change of the second index, and [0, 0, 0, 1, 0]
is the derivative in the direction of the change of the fourth index.
[0, 1]
[0, 0, 0, 1, 0]
When a 2D array is represented graphically, it is customary to interpret the first index as "row number" and the second index as "column number". This makes the first index correspond to the vertical downward direction and the second index to the horizontal direction (left to right, unless we are in an RTL environment).
So, (0, 1)
is horizontal and (1, 0)
is vertical. The author of the page may have made a terminological mistake. Also, there is no law against calling the horizontal axis y
and the vertical axis x
, so they may have done that.
(0, 1)
(1, 0)
y
x
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you sure you're "naming" the axes the same way as the author of that post? The first dimension of the arrays is
y
and the secondx
(e.g.imx[y][x]
).– jedwards
Jul 1 at 22:15