将QImage划分为较小的部分 [英] Dividing QImage to smaller pieces

查看:224
本文介绍了将QImage划分为较小的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图片,我想把这张图片分成n块。所以我想知道的是我怎么能用QImage做到这一点?或者是否有更好的(性能明智的)Qt类用于此任务?

I have an image, and I want to divide this image to n pieces. So what I'm wondering is that how can I do this with QImage? Or is there a better(performance wise) Qt class for this task?

例如,假设我的图像由1920x1080像素组成,我想将其除以100碎片意味着每件都包含192x108像素。请注意,我不想创建缩放到192x108的相同部分。每一部分都是主要图片的一部分。

For example imagine I have an image consist of 1920x1080 pixels, and I want to divide it to 100 pieces which means each piece will consist of 192x108 pixels. Note that I don't want to create same piece scaled to 192x108. Each piece is a distinct piece of the main picture.

我附上了一张图片,让我的问题更加清晰。
这个图像由1920x1080像素组成,每个部分都是由192x108像素组成的网格,我希望将它们作为QImage对象处理,而不是实际将其分为100个。

I have attached an Image to make my question clearer. This image consist of 1920x1080 pixels and each part dived with grids consists of 192x108 pixels, I want to treat each of them as QImage object without actually dividing it to 100 pieces.

有没有办法用QImage做到这一点?性能在这里很重要,因为我会分析每件作品,通常会有超过1000件。

Is there a way to do this with QImage? Performance is important here because I'll analyze each piece and usually there is going to be more than 1000 piece.

感谢您的帮助!

编辑:两种方法都运行良好,但是alexisdm的方法更复杂,更低级,但更快。 jmk的方法更容易使用,但速度较慢。

Both methods work perfectly, however alexisdm's method is bit more complex, lower-level, but faster. jmk's method is easier to use but it's slower.

感谢您的回答。

推荐答案

您可以创建 QImage 不使用相同的步幅(每行字节数)和格式作为源图像复制任何数据并传递原始数据缓冲区,其偏移量对应于所需的起始位置,到 QImage的一个构造函数

You can create a QImage without copying any data by using the same stride (bytes per line) and format as the source image and passing the raw data buffer, with an offset corresponding to the desired starting position, to one of the constructors of QImage:

QImage createSubImage(QImage* image, const QRect & rect) {
    size_t offset = rect.x() * image->depth() / 8
                    + rect.y() * image->bytesPerLine();
    return QImage(image->bits() + offset, rect.width(), rect.height(),
                  image->bytesPerLine(), image->format());
}

只要子,源图像必须存在QImage 存在。

但是如果分析代码试图直接访问图像原始数据而不使用 QImage :: bytesPerLine() QImage :: scanLine() QImage :: pixel()

But it might not work if the analyzing code tries to access the image raw data directly without using QImage::bytesPerLine(), QImage::scanLine() or QImage::pixel().

这篇关于将QImage划分为较小的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆