QImage:有没有惰性复制裁剪方法

发布于 2024-11-13 22:29:55 字数 638 浏览 2 评论 0原文

我试图使用 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() 上,因为它只在那里执行复制。如果它只是一个惰性的写时复制操作,那么它会更快。

有什么办法可以做得更快吗?

I was trying to use QImage for loading image and checking the equality with a template sub-image that moves all over the image. The code is as:

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'

The problem is that its taking too much time to do this. Similar operation using Python Imaging Library was too fast. The two major operations are copy() and operator==() . I think that major time is being spent in the copy() as it performs the copy there only. If it had been just a lazy copy-on-write operation then it would have been faster.

Is there any way to do it faster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2024-11-20 22:29:55

更快的方法是手动比较像素——您所做的副本是浪费的。假设您想查找backgroundMask 作为“image”的子图像。您从左上角开始。现在你发现图像的像素(0, 0)与backgroundMask的(0, 0)不匹配。如果您手动比较像素,您只需继续到图像的 (0, 1) 并将其与 (0, 0) 进行比较,依此类推。但就你而言,你已经浪费了很多时间来复制宽度 x 高度像素。

start = time.time()
for i in xrange(image.height() - backgroundMask.height() + 1):
    for j in xrange(image.width() - backgroundMask.width() + 1):
        success = True
        for y in xrange(backgroundMask.height()):
            for x in xrange(backgroundMask.width()):
                if image.pixel(j + x, i + y) != backgroundMask.pixel(x, y):
                    success = False
                    break
            if not success:
                break

        if success:
            print 'match'
        else:
            print 'no match'

诚然,Python 中的每像素访问速度很慢,并且相等运算符是用 C 编写的。但它仍然比您发布的快得多。对于我尝试的图像,你的代码花了 27 秒,我的代码花了 0.8 秒。

然而,如果实现了此功能,最好的解决方案可能是将 QImage 转换为 PIL 图像。 QImages 和 PIL 图像之间的转换非常简单并且有详细的文档记录。

The faster way to do it is to compare the pixels by hand – the copies you're doing are wasteful. Suppose you want to find backgroundMask as a subimage of 'image'. You start at the top left corner. Now you find that pixel (0, 0) of image doesn't match (0, 0) of backgroundMask. If you're comparing pixels by hand, you just continue over to (0, 1) of image and compare that with (0, 0), and so on. But in your case, you've wasted ages copying width x height pixels already.

start = time.time()
for i in xrange(image.height() - backgroundMask.height() + 1):
    for j in xrange(image.width() - backgroundMask.width() + 1):
        success = True
        for y in xrange(backgroundMask.height()):
            for x in xrange(backgroundMask.width()):
                if image.pixel(j + x, i + y) != backgroundMask.pixel(x, y):
                    success = False
                    break
            if not success:
                break

        if success:
            print 'match'
        else:
            print 'no match'

Admittedly per-pixel access is slow in Python and the equality operator is written in C. But it's still significantly faster than what you posted. For the image I tried on, your code took 27 seconds and mine took 0.8.

However, the best solution is probably to convert the QImage to a PIL Image if this functionality is implemented there. The conversion between QImages and PIL images is simple and well-documented.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文