问题描述
似乎无法想出这个。非常新的numpy。
Can't seem to figure this one out. Very new to numpy.
我有一个numpy数组(200,1,1000,1000)
对应于(图像数量,通道,x_of_image,y_of_image)。所以我有200个图像,1个通道,每个1000x1000像素。
I have a numpy array of shape (200,1,1000,1000)
which corresponds to (number of images, channel, x_of_image, y_of_image). So I have 200 images with 1 channel that are 1000x1000 pixels each.
我想拍摄200张图片(1,1000,1000) )
,对图像部分(1000,1000)
执行操作,并将其连接/连接到一个全新的数组。
I want to take each of the 200 images (1,1000,1000)
, do a operation on the image portion (1000,1000)
, and append/concatenate it to a brand new array.
new_array = np.array([])
for image in original_array:
new_array = np.concatenate(new_array,original_array[0].operation())
新数组最终会成为确切的结果与原始(200,1,1000,1000)
相同的形状,因为执行了操作,只有不同的图像。
New array would end up being the exact same shape as the original (200,1,1000,1000)
just with different images because of the operation performed.
奖金:
我如何才能对阵列的某些百分比进行操作,比如50%?
这将输出(100,1,1000,1000)
推荐答案
避免在循环中调用 np.concatenate
。它分配一个新数组并复制所有内容。这很慢,如果丢弃的副本堆积而没有被垃圾收集,你可能会遇到内存问题。
Avoid calling np.concatenate
in a loop. It allocates a new array and copies everything. This is slow and you may run into memory problems if the discarded copies pile up without being garbage collected.
如何做到这一点主要取决于你在图片。大多数numpy操作都适用于多维数组。
How this should be done depends mostly on the operations you perform on the images. Most numpy operations are designed to work very well with multi-dimensional arrays.
-
尝试使用numpy数组函数表示操作。例如,将图像标准化为0..1的范围可以这样做:
Try to express the operation with numpy array functions. For example, normalizing the images to a range of 0..1 could be done like this:
new_array = original_array - original_array.min(axis=(-1, -2), keepdims=True)
new_array /= new_array.max(axis=(-1, -2), keepdims=True)
如果图像操作过于复杂而无法分解为numpy函数,请先分配新数组并对其进行修改。 / p>
If the image operations are too complex to be broken down into numpy functions, allocate the new array first and modify it in place.
new_array = np.empty_like(original_array)
for i in range(new_array.shape[0]):
new_array[i] = complicated_operation(original_array[i])
或复制原始数组并工作只在副本上:
Or copy the original array and work only on the copy:
new_array = original_array.copy()
for image in new_array:
image[:] = complicated_operation(image)
出于某种原因,你不想预先分配,然后将图像存储在临时的数组列表中,并最终将它们连接起来:
For some reason you do not want to pre-allocate, then store the images in a temporary list of arrays and concatenate them in the end:
new_images = []
for image in original_array:
new_images.append(image.operation())
new_array = np.stack(new_images)
如果你真的想连续连接数组,请注意数组-to-be-concatenated作为一个序列传递给函数,如下所示:
If you really want to successively concatenate arrays, note that the arrays-to-be-concatenated are passed to the function as one sequence, like this:
new_array = np.array([])
for image in original_array:
new_array = np.concatenate([new_array, image.operation()])
奖励:查找。这是非常基本的numpy / Python,绝对应该在你的工具箱中。
Bonus: look up slicing. This is very basic numpy/Python and should definitely be in your toolbox.
original_array[::2, :, :, :] # take every second image
这篇关于从Numpy数组中的元素创建新的Numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!