使用PIL和Numpy的Python图像过滤速度太慢 [英] Python image filtering with PIL and Numpy too slow
本文介绍了使用PIL和Numpy的Python图像过滤速度太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用PIL和Numpy在Python中实现最简单的高斯滤波器.一切正常,但速度很慢:(无法确定如何加快速度.您能帮忙吗?
I'm trying to implement the simpliest Gauss filter in Python with PIL and Numpy. All works fine but very slowly :( Can't figure out how to speed up. Could you help?
import os, sys
import Image, ImageEnhance
import numpy as np
if (len(sys.argv) > 1):
im = Image.open(sys.argv[1])
data = np.array(im.resize((200,200)))
out_data = np.array(data)
chs = len(data[0][0])
kernel = np.array([[1.,2,1],
[2,3,2],
[1,2,1]])
ctr = 1
kernel = kernel/np.sum(kernel)
for x in xrange(data.shape[0]):
for y in xrange(data.shape[1]):
for c in xrange(chs):
acc = 0
for i in xrange(kernel.shape[0]):
for j in xrange(kernel.shape[1]):
m = x + i - ctr
n = y + j - ctr
if (m >= 0 and n >= 0 and m < data.shape[0] and n < data.shape [1]):
acc += data[m][n][c]*kernel[i][j]
out_data[x][y][c] = acc
out = Image.new(im.mode, (data.shape[0], data.shape[1]))
out = Image.fromarray(out_data)
out.show()
else:
print "no file was passed"
推荐答案
看看高斯模糊滤波器是可分离的,这意味着您可以大大降低算法的复杂性(在研究其他答案(即并行化)的建议之上).
Gaussian blur filters are separable, which means you can reduce the complexity of your algorithm quite a bit (on top of looking into the suggestions from other answers, i.e. parallelization).
这篇关于使用PIL和Numpy的Python图像过滤速度太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文