Python/Pyside: 创建图像直方图

4

如何在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

平均时间 = 4秒

更好但仍然缓慢。 有没有更快的方法从QImage计算图像直方图?


"最有效"是什么意思?编写最容易?处理速度最快?使用最少的内存? - ewall
最快的处理速度是我的目标。 - DooBLER
你已经很好地开始了,通过计时两种不同的方法。你可以尝试不同的循环/迭代计算方式(例如此问题中列出的那些),但是使用代码分析等方法来识别性能瓶颈会更加困难。 - ewall
1个回答

0

你可以尝试另一种方法,将图像数据转换为numpy数组(希望能够相对高效地完成),然后使用numpy.histogram


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接