使用PIL矢量化图像的重塑和裁剪 [英] Vectorizing the reshaping and cropping of images using PIL

查看:74
本文介绍了使用PIL矢量化图像的重塑和裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要裁剪然后重新塑形的图像.为了帮助我,我编写了两个帮助函数:

I have a number of images that I want to crop, then reshape. To help me with this I have written two helper functions:

def crop_images(images_data):
    cropped_images = []
    for image_data in images_data:
        image = Image.fromarray(image_data)
        cropped_image = np.asarray(image.crop((25,40,275,120)))
        cropped_images.append(cropped_image)
    return(np.array(cropped_images))

def resize_images(images_data):
    resized_images = []
    width, height = images_data.shape[2], images_data.shape[1]
    resized_width, resized_height = int(width/2), int(height/2)
    for image_data in images_data:
        image = Image.fromarray(image_data)
        image = image.resize((resized_width, resized_height), Image.ANTIALIAS)
        resized_images.append(np.asarray(image))
    return(np.array(resized_images))

然后,我将两个函数链接在一起,以处理我的图像,例如: resize_images(crop_images(images_data))

Then I would just chain the two functions together to process my images like: resize_images(crop_images(images_data))

但是我想知道是否有一种矢量化这些操作的方法,因为我知道numpy最好是矢量化操作,因为它更快.

But I was wondering whether there is a way to vectorize these operation as I know that numpy should ideally be vectorized operations, as it is faster.

推荐答案

这是更高级别的迭代-在图像数组上-通常不涉及向量化"的讨论.

This is a higher level of iteration - over image arrays - where the usual talk about 'vectorizing' is not as applicable.

图像数组的大小通常为(400,400,3)或更大.如果不需要,您不想迭代这400个方面中的一个.因此,对图像阵列进行向量化"操作非常有意义.

Image arrays tend to have size like (400,400,3) or bigger. You don't want to iterate of one of those 400 sides if you don't have to. So 'vectorizing' operations on image arrays makes a lot of sense.

但是,如果处理这些图像中的100张,则循环遍历并不是很糟糕. 向量化"的唯一方法是将它们组合成一个更大的数组(N,400、400、3),然后找到可在4d或较大片段上使用的表达式.如果N为1000或更大,就很容易走这条路,但是对于像这样的大阵列,内存管理问题会开始影响速度的提高.

But if processing 100 of these images, a loop over images isn't so bad. The only way to 'vectorize' is to assemble them into a larger array (N, 400, 400, 3) and find expressions that work on 4d, or slices of that big one. It's tempting to go that route if N is 1000 or more, but for a big array like that memory management issues start chewing into any speed gains.

对于迭代,我认为追加到列表并插入到预分配的数组中都是有用的.我还没有明显的证据表明,在所有情况下,一个都比另一个要快.

For iteration, I think appending to list and inserting into a preallocated array are both useful. I haven't seen clear evidence that one is faster than the other in all cases.

alist = []
for arr in source:
    <process arr>
    alist.append(arr)
bigarr = np.array(alist)

bigarr = np.zeros((N,..)
for i in range(N):
    arr = source[i,...]
    <process arr>
    bigarr[i,...] = arr

尝试对批处理操作进行矢量化处理时,代码的清晰度也会受到影响.

Code clarity can also suffer when trying to 'vectorize' batch operations.

这篇关于使用PIL矢量化图像的重塑和裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆