本文介绍了多维数组和梯度的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手。我试图将图像读入多维numpy数组并计算梯度的大小,这应该产生单个通道图像。然而,计算幅度后的尺寸是700 x 900.任何人都可以帮我格式化数据,所以我可以收到单个频道图像吗?

I am new to python. I am trying to read an image into a multidimensional numpy array and compute the magnitude of the gradient, which should produce a single channel image. The dimensions after I compute the magnitude, however, is 700 x 900. Can anyone help me format the data, so I can receive a single channel image?

a = imread('20091016_tumor_26_18245948_1chop.png')
ndimage.gaussian_gradient_magnitude(a, 0.4, a)


推荐答案

我认为人们对你的术语感到困惑。单通道图像 2D阵列......,多通道图像将是3D阵列(存储每个通道的额外维度)。例如,单个通道图像将是灰度,但多通道将是彩色的。有关详细信息,请参阅

I think people are being confused by your terminology. A single channel image is a 2D array..., a multi-channel image would be a 3D array (extra dimension to store each channel). For example, a single channel image would be grayscale, but multi-channel would be color. For more information, see Channel (digital image)

如果你想要一个标量输出,你必须更具体地说明如何将图像缩小为标量。一个简单的例子是数组的 norm ,如下所示,但这个选择应该取决于你的用例。

If you want a scalar output, you would have to be more specific on how you want to reduce an image to a scalar. One simple example would be the norm of the array, as below, but this choice should depend on your use case.

a = imread('20091016_tumor_26_18245948_1chop.png')  # a 2d input image
g = ndimage.gaussian_gradient_magnitude(a, 0.4)  # a 2d image showing magnitude of the gradient in a

s = np.linalg.norm(g)  # the scalar norm of the gradient

在你的评论中你建议你试过

In your comment you suggested that you'd tried

grad = numpy.gradient(a)
a = numpy.sqrt(grad.dot(grad)) 

这里的问题,假设你想要一个标量最后,默认情况下,numpy中的许多(如果不是大多数)函数对数组的每个元素进行操作。因此,在上面的代码中, gradient 给出每个像素的渐变

The problem here, assuming you want a scalar in the end, is that many (if not most) functions within numpy, by default, operate on each element of the array. So, in the above code, gradient gives the gradient at each pixel

a.shape
#(H, W)
grad.shape
#(2, H, W)

由于渐变本身是一个向量,它实际上添加到维度中,因此2D图像变为3d数组。

Since the gradient itself is a vector,it actually adds to the dimensionality, so a 2d image becomes a 3d array.

sqrt dot (在本例中)返回与输入大小相同的数组。如果你在两个数组上使用 dot ,那么它的矩阵版本是 aT * b 这里, a b 都是相同的形状。 1d或2d阵列的内积或点积的输出具有与右手侧相同的宽度和与左手侧相同的高度,因此两个正方形矩阵给出方阵。

sqrt and dot (in this case) each return an array of the same size as the input. If you use dot on two arrays, it is doing the matrix version of a.T * b Here, a and b are both the same shape. The output of the inner or dot product of a 1d or 2d array has the same width as the right hand side and same height as the left hand side, hence two square matrices give a square matrix.

以下是一些例子:

a = Image.open('kinglet_bw.jpg')
plt.imshow(a)
for i, g in enumerate(np.gradient(a,3,3)):
    plt.subplot(121+i)
    plt.imshow(g)
g = ndimage.gaussian_gradient_magnitude(a, 3)
plt.imshow(g)

这篇关于多维数组和梯度的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 03:04