我试图使用QImage加载图像,并检查与在整个图像中移动的模板子图像相等。该代码是:QImage:是否有任何懒惰的复制裁剪方法
for i in range(image.height() - backgroundMask.height() + 1):
for j in range(image.width() - backgroundMask.width() + 1):
subsection = image.copy(j, i, j + backgroundMask.width() - 1, i + backgroundMask.height() - 1)
if subsection == backgroundMask:
print 'equality action here'
else:
print 'non-equality action here'
问题是,它需要太多的时间来做到这一点。使用Python Imaging Library的类似操作太快了。两个主要操作是copy()和operator ==()。我认为主要时间是在copy()中花费的,因为它只在那里执行拷贝。如果它只是一个懒惰的写入时复制操作,那么它会更快。
有没有办法更快地做到这一点?
这是唠叨我的问题。 Python循环很慢并且没有优化。 – Xolve 2011-06-05 12:15:41
但是这个算法比你使用的算法好得多,尽管你的一部分被推入了C++。否则使用我建议的PIL方法。 – PAG 2011-06-05 12:29:43