【问题标题】:Python/Pyside: Create Image HistogramPython/Pyside:创建图像直方图
【发布时间】: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 计算图像直方图?

【问题讨论】:

标签: python histogram pyside


【解决方案1】:

您可能会尝试的另一种方法是将图像数据放入一个 numpy 数组(希望可能合理有效)并使用numpy.histogram。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2022-01-21
    • 2017-02-25
    • 2011-02-06
    • 1970-01-01
    相关资源
    最近更新 更多