2011-08-02 26 views
0

我想将Qt图像的数据复制到Boost Multi Array中,对Multi Array进行一些操作并将数据复制回QImage进行显示。在Qt Qimage和Boost Multi Array之间正确复制数据

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

我已经放在一起说明一个典型问题的片段。可能有很多事情我做错了,所以请忍受我。在这里,我想的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

任何帮助,非常感谢。

回答

0

我认为最简单的方法可能是使用QImage::scanLine(row)逐行复制它。
我认为可能会在每条扫描线结束时出现填充现象,导致您丢失。