【问题标题】:A faster way to separate the green channel of an image一种更快的分离图像绿色通道的方法
【发布时间】:2017-03-05 03:54:10
【问题描述】:

我正在使用以下代码,使用 PIL 和 numpy 将图像的绿色通道分离为单独的 .jpeg。

from PIL import Image
import numpy as np

im = Image.open('image.jpg')
im = np.array(im)
im[:,:,0] *=0
im[:,:,2] *=0
im = Image.fromarray(im,'RGB')
im.save('greened.jpg')

代码运行良好,但在循环处理一系列图像时需要大约一秒钟的时间。

还有其他更快的方法吗?

【问题讨论】:

  • 分析您的代码。什么时间最长?我猜,im.save。如果是这样,就没有机会让您的代码更快。

标签: python performance numpy image-processing python-imaging-library


【解决方案1】:

您可以一次性分配零,甚至不需要乘以 0 -

im[:,:,[0,2]] = 0

【讨论】:

  • 哦,你让程序快了半毫秒。
【解决方案2】:

如果你可以使用 opencv,你可以通过避免构造 numpy 数组和转换回图像来加速它。

我试过了:

这平均花费了0.011458 秒 (10)。

@tm_it
def with_PIL():
    im = imag.open('C:/Users/srlatch/Desktop/shi.jpg')
    im = np.array(im)
    im[:,:,0] *=0
    im[:,:,2] *=0
    im = imag.fromarray(im,'RGB')
    im.save('greened.jpg')

这平均花费了0.000474981 秒(10):

@tm_it
def with_open_cv():
    image=cv2.imread('C:/Users/srlatch/Desktop/shi.jpg')
    image[:,:,[0,2]] = 0
    cv2.imwrite('result.jpg',image)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 2022-09-01
    • 2019-05-07
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    相关资源
    最近更新 更多