Extracting RGB from image
-
wrote on 15 Nov 2021, 14:32 last edited by
Is it possible with Qt to take an image of a (palette) and analyze the image and extract the color information from left to right (so total width of image and only first scanline)
Position, Red, Green, Blue
like this (example)
{ 0, 120, 0, 0, 22, 179, 22, 0, 51, 255,104, 0, 85, 167, 22, 18, 135, 100, 0, 103, 198, 16, 0, 130, 255, 0, 0, 160 };
if yes, how?
-
Is it possible with Qt to take an image of a (palette) and analyze the image and extract the color information from left to right (so total width of image and only first scanline)
Position, Red, Green, Blue
like this (example)
{ 0, 120, 0, 0, 22, 179, 22, 0, 51, 255,104, 0, 85, 167, 22, 18, 135, 100, 0, 103, 198, 16, 0, 130, 255, 0, 0, 160 };
if yes, how?
-
wrote on 15 Nov 2021, 16:33 last edited by
QImage img( ":/image/palettes/Rainbow.png" ); img.convertTo(QImage::Format_ARGB32); struct newRGB { int Pos; int Red; int Green; int Blue; }; newRGB testRGB[img.width()]; for(uint32_t Y = 0; Y < 1; ++Y) { uint8_t* pPixel = img.scanLine(Y); for(int X = 0; X < img.width(); ++X) { const int Blue = *pPixel++; const int Green = *pPixel++; const int Red = *pPixel++; testRGB[X].Pos = X; testRGB[X].Red = Red; testRGB[X].Green = Green; testRGB[X].Blue = Blue; } } for(int i = 0; i < img.width(); ++i) { qDebug() << testRGB[i].Pos << "," << testRGB[i].Red << "," << testRGB[i].Green << "," << testRGB[i].Blue; }
produces
0 , 255 , 0 , 0 1 , 4 , 0 , 255 2 , 0 , 255 , 255 3 , 255 , 255 , 9 4 , 255 , 13 , 0 5 , 18 , 0 , 255 6 , 0 , 255 , 255 7 , 255 , 255 , 22
Pos 0
is 255 Red, that is correct! but thenPos 1
is suddenly 255 Blue with 4 on the red :S -
QImage img( ":/image/palettes/Rainbow.png" ); img.convertTo(QImage::Format_ARGB32); struct newRGB { int Pos; int Red; int Green; int Blue; }; newRGB testRGB[img.width()]; for(uint32_t Y = 0; Y < 1; ++Y) { uint8_t* pPixel = img.scanLine(Y); for(int X = 0; X < img.width(); ++X) { const int Blue = *pPixel++; const int Green = *pPixel++; const int Red = *pPixel++; testRGB[X].Pos = X; testRGB[X].Red = Red; testRGB[X].Green = Green; testRGB[X].Blue = Blue; } } for(int i = 0; i < img.width(); ++i) { qDebug() << testRGB[i].Pos << "," << testRGB[i].Red << "," << testRGB[i].Green << "," << testRGB[i].Blue; }
produces
0 , 255 , 0 , 0 1 , 4 , 0 , 255 2 , 0 , 255 , 255 3 , 255 , 255 , 9 4 , 255 , 13 , 0 5 , 18 , 0 , 255 6 , 0 , 255 , 255 7 , 255 , 255 , 22
Pos 0
is 255 Red, that is correct! but thenPos 1
is suddenly 255 Blue with 4 on the red :Swrote on 15 Nov 2021, 16:39 last edited byuchar *QImage::scanLine(int i) Returns a pointer to the pixel data at the scanline with index i. The first scanline is at index 0. The scanline data is as minimum 32-bit aligned. For 64-bit formats it follows the native alignment of 64-bit integers (64-bit for most platforms, but notably 32-bit on i386). Warning: If you are accessing 32-bpp image data, cast the returned pointer to QRgb* (QRgb has a 32-bit size) and use it to read/write the pixel value. You cannot use the uchar* pointer directly, because the pixel format depends on the byte order on the underlying platform. Use qRed(), qGreen(), qBlue(), and qAlpha() to access the pixels. See also bytesPerLine(), bits(), Pixel Manipulation, and constScanLine().
So I would suggest following the documentation and sticking with qRGB approach? For alpha aware images there is also separate type.
-
wrote on 15 Nov 2021, 16:51 last edited by
Instead of using scanLine you can use
https://doc.qt.io/qt-5/qimage.html#pixel
or also
https://doc.qt.io/qt-5/qimage.html#pixelColor -
QImage img( ":/image/palettes/Rainbow.png" ); img.convertTo(QImage::Format_ARGB32); struct newRGB { int Pos; int Red; int Green; int Blue; }; newRGB testRGB[img.width()]; for(uint32_t Y = 0; Y < 1; ++Y) { uint8_t* pPixel = img.scanLine(Y); for(int X = 0; X < img.width(); ++X) { const int Blue = *pPixel++; const int Green = *pPixel++; const int Red = *pPixel++; testRGB[X].Pos = X; testRGB[X].Red = Red; testRGB[X].Green = Green; testRGB[X].Blue = Blue; } } for(int i = 0; i < img.width(); ++i) { qDebug() << testRGB[i].Pos << "," << testRGB[i].Red << "," << testRGB[i].Green << "," << testRGB[i].Blue; }
produces
0 , 255 , 0 , 0 1 , 4 , 0 , 255 2 , 0 , 255 , 255 3 , 255 , 255 , 9 4 , 255 , 13 , 0 5 , 18 , 0 , 255 6 , 0 , 255 , 255 7 , 255 , 255 , 22
Pos 0
is 255 Red, that is correct! but thenPos 1
is suddenly 255 Blue with 4 on the red :S@Kris-Revi said in Extracting RGB from image:
Pos 0 is 255 Red, that is correct! but then Pos 1 is suddenly 255 Blue with 4 on the red :S
dont increment the pointer for each color, but only for each position
const int Blue = *pPixel++; const int Green = *pPixel++; const int Red = *pPixel++;
-
Hi,
@Kris-Revi said in Extracting RGB from image:
QImage::Format_ARGB32
Just in case: this is a 4 channels image. You completely ignore the A in your loop.
-
Hi,
@Kris-Revi said in Extracting RGB from image:
QImage::Format_ARGB32
Just in case: this is a 4 channels image. You completely ignore the A in your loop.
-
Even so, if you read the description of the format, the A channel is locked at 0xFF.
1/9