2011-06-09 68 views
1

我試圖符合的函數需要三個類型爲double的1維數組[19200]。下面陣列是RGB陣列,使得:QImage(或圖像一般)轉換爲3個1D陣列的RGB

double r[19200]; // r 
double g[19200]; // g 
double b[19200]; // b 

到目前爲止,我可以從提取QImage的像素信息,並填充上述陣列。

問題出在測試。我不知道如何做相反的處理:給出三個1維數組我該如何從這些數據創建一個新的QImage?

我想驗證我確實獲得了正確的值。 (像列與行的主要命令給我懷疑)。因此,我試圖從這三個一維維數組中構建一個圖像QImage。

回答

2

我不明白爲什麼你遇到問題,如果你設法做到這一點。該過程基本上是一樣的:

for (int x=0; x<w; x++) 
    for (int y=0; y<h; y++) 
    image.setPixel(x,y, convertToRGB(r[x*w+y], ...); 

哪裏convertToRGB是逆變換你對轉換和RGB值的浮點值,假設圖像具有尺寸w*h。如果您發現這是錯誤的行 - 主要/列主要變體,則反之。

由於您沒有提供有關如何進行色彩空間轉換的信息,而且我們也不知道它是否是行大或列大小,所以不能幫助您更多。

0

看起來好像QImage支持幾種從像素陣列加載的方式。

QImage(const uchar *data, int width, int height, Format format) 
bool QImage::loadFromData(const uchar *buf, int len, const char *format=0) 

使用的第一個例子,如果你有你提到的陣列,那麼你可能會希望使用的格式的QImage :: Format_RGB888(從qimage.h)。

您需要自己知道寬度和高度。

最後你會想重新包裝數組到一個單一的UCHAR *陣列

uchar* rgb_array = new uchar[19200+19200+19200]; 

for(int i = 0, j = 0; j < 19200; ++j) 
{ 
    // here we convert from the double range 0..1 to the integer range 0..255 
    rgb_array[i++] = r[j] * 255; 
    rgb_array[i++] = g[j] * 255; 
    rgb_array[i++] = b[j] * 255; 
} 

{ 
    QImage my_image(rgb_array, width, height, QImage::Format_RGB888); 

    // do stuff with my_image... 
} 

delete[] rgb_array; // note you need to hold onto this array while the image still exists