【发布时间】:2011-10-30 17:18:40
【问题描述】:
在 PySide 中创建图像(QImage)直方图最有效的方法是什么?
我的测试图片是1,9MB,3648x2736px,jpeg照片
我尝试了两种方法:
1.
import time
start = time.time()
for y in range(h):
line = img.scanLine(y) # img - instance of QImage
for x in range(w):
color = struct.unpack('I', line[x*4:x*4+4])[0]
self.obrhis[0][QtGui.qRed(color)] += 1 # red
self.obrhis[1][QtGui.qGreen(color)] += 1 # green
self.obrhis[2][QtGui.qBlue(color)] += 1 # blue
print 'time: ', time.time() - start
平均时间 = 15 秒
2.
import time
start = time.time()
buffer = QtCore.QBuffer()
buffer.open(QtCore.QIODevice.ReadWrite)
img.save(buffer, "PNG")
import cStringIO
import Image
strio = cStringIO.StringIO()
strio.write(buffer.data())
buffer.close()
strio.seek(0)
pilimg = Image.open(strio)
hist = pilimg.histogram()
self.obrhis[0] = hist[:256]
self.obrhis[1] = hist[256:512]
self.obrhis[2] = hist[512:]
print 'time: ', time.time() - start
平均时间 = 4s
更好,但仍然很慢。 有没有更快的方法从 QImage 计算图像直方图?
【问题讨论】:
-
“最有效”是什么意思?:最容易编码?处理速度最快?使用最少的内存?
-
最快的处理是我的目标
-
通过两种不同的方法计时,您将有一个良好的开端。您可以尝试不同的循环/交互计算方式(例如those listed in this question),但identify the performance bottlenecks with code profiling and such 更难。