4
什么是创造PySide图像(QImage的)直方图的最有效方法是什么?的Python/Pyside:创建图像直方图
我的测试图像是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的图像直方图?
你所说的“最有效”的意思是?:最简单的代码?最快的处理?使用最少的内存? – ewall
最快处理是我的目标 – DooBLER
通过对两种不同的方法进行计时,您已经处于一个良好的开端。你可以尝试循环/的作用中计算(如[那些在这个问题上市(http://stackoverflow.com/questions/2870466/python-histogram-one-liner))的方式不同,但它很难[确定性能瓶颈与代码分析等](http://stackoverflow.com/questions/3956112/how-to-determine-bottlenecks-in-code-besides-visual-inspection)。 – ewall