【问题标题】:Copying data correctly between Qt Qimage and Boost Multi Array在 Qt Qimage 和 Boost Multi Array 之间正确复制数据
【发布时间】:2011-08-02 10:56:06
【问题描述】:

我想将数据从 Qt 图像复制到 Boost Multi Array,对 Multi Array 进行一些操作,然后将数据复制回 QImage 以显示。

我正在使用Qimage::bits() 访问原始数据并尝试使用std::copy 进行复制,并且似乎存在我不理解的数据对齐问题。有一个 note here 关于访问 32-bpp 图像的数据,但即使我将 QImage 转换为不同的格式,问题仍然存在。

我整理了一个说明典型问题的 sn-p。很可能有很多事情我做错了,所以请耐心等待。在这里,我试图将Image 2 的上半部分复制到Image 1 并得到this output

#include <algorithm>
#include <boost/multi_array.hpp>
#include <QImage>

typedef boost::multi_array<uchar, 3> image_type;

int main() {
    QString path = "/path/to/images/";
    QImage qimage_1(path + "image1.jpg");
    QImage qimage_2(path + "image2.jpg");

    image_type bimage_1(boost::extents[qimage_1.width()][qimage_1.height()][4]);
    image_type bimage_2(boost::extents[qimage_2.width()][qimage_2.height()][4]);

    std::copy(qimage_1.bits(), qimage_1.bits() + qimage_1.width()*qimage_1.height()*4, &bimage_1[0][0][0]);
    std::copy(qimage_2.bits(), qimage_2.bits() + qimage_2.width()*qimage_2.height()*4, &bimage_2[0][0][0]);

    // copy top half of image 2 to image 1
    for(int i = 0; i < qimage_1.width(); i++) {
        for(int j = 0; j < qimage_1.height()/2; j++) {
            bimage_1[i][j][0] = bimage_2[i][j][0];
            bimage_1[i][j][1] = bimage_2[i][j][1];
            bimage_1[i][j][2] = bimage_2[i][j][2];
            bimage_1[i][j][3] = bimage_2[i][j][3];
        }
    }

    std::copy(&bimage_1[0][0][0], &bimage_1[0][0][0] + bimage_1.num_elements(), qimage_1.bits());
    qimage_1.save(path + "output.png");
    return 0;
}

我的 .pro 文件只包含 SOURCES += main.cpp

非常感谢任何帮助。

【问题讨论】:

    标签: c++ qt boost qimage boost-multi-array


    【解决方案1】:

    我认为最简单的方法可能是使用 QImage::scanLine(row) 逐行复制它。
    我认为在每条扫描线的末尾可能会有填充物让你失望。

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 2019-11-28
      相关资源
      最近更新 更多