python曲线镜像_Python:沿垂直轴镜像图像的最有效方法

I have a lot of images that I will need to flip (on the fly) and so am looking for the fastest way possible to do this using Python.

What is the most efficient way to do this?

I have image files on disk and have tried to ways, shown in my own answer below, but these start with Numpy arrays and so may not be optimal. Are there better ways?

解决方案

You can simply use slicing to flip the second last axis to get equivalent flipped view into the input array of images, as such won't be creating any new data in memory and hence an efficient one, like so -

images[...,::-1,:]

If you still need to make a copy, use .copy there, which would still be more efficient than np.fliplr and noticeable with small/decent sized arrays.

Runtime test -

It seems NumPy is the winner, so I will test it out against that one.

In [64]: images = np.random.randint(0,255,(3,200,400,3))

In [65]: out1 = np.array([np.fliplr(images[i]) for i in range(3)])

In [66]: out2 = images[...,::-1,:]

In [67]: np.allclose(out1, out2)

Out[67]: True

In [68]: %timeit np.array([np.fliplr(images[i]) for i in range(3)])

1000 loops, best of 3: 1.38 ms per loop

In [69]: %timeit images[...,::-1,:]

1000000 loops, best of 3: 259 ns per loop # virtually free

If you need copy -

In [76]: images = np.random.randint(0,255,(3,10,10,3))

In [77]: %timeit np.array([np.fliplr(images[i]) for i in range(3)])

100000 loops, best of 3: 5.76 µs per loop

In [78]: %timeit images[...,::-1,:].copy()

100000 loops, best of 3: 2.23 µs per loop

In [79]: images = np.random.randint(0,255,(3,100,100,3))

In [80]: %timeit np.array([np.fliplr(images[i]) for i in range(3)])

10000 loops, best of 3: 159 µs per loop

In [81]: %timeit images[...,::-1,:].copy()

10000 loops, best of 3: 152 µs per loop

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值