Solved Get RGB pixel values from QImage buffer
-
Hi all,
I'm currently trying to make some operations over the RGB values of a QImage region. To go over the buffer I use some char* variable but, the values I get are not the same as if I would try to acces with QImage::pixelColor(QPoint). Here is the code:
QImage img; bool res = img.load("./scattering.jpg"); QImage img2 = img.convertToFormat(QImage::Format_RGB888); x_or = 1; y_or = 254; wdth_r = 1278; height_r = 508; //Get gray values char* in = (char*)img2->bits(); *out = (float*) malloc(wdth_r * height_r*sizeof(float)); char R; char G; char B; int w_step = ((wdth_r) * 3 + 2); //RGB channels. This + 2 is only in Qt RGB24 buffers ? float * auxPt = *out; char* auxInPt = in + (y_or - 1) * w_step; char* auxInPtCol; for (int i = y_or - 1; i < (y_or + height_r - 1); i++) { char* line = (char*)inn->scanLine(i); auxInPtCol = auxInPt; //Equal to line. for (int j = x_or - 1; j < (x_or + wdth_r - 1); j++) { R = *auxInPtCol++; G = *auxInPtCol++; B = *auxInPtCol++; Color aux = inn->pixelColor(QPoint(i, j)); char R_R = aux.red(); char G_R = aux.green(); char B_R = aux.blue(); //Debugger cond if (R_R != R) int a = 2; if (G_R != G) int b = 2; if (B_R != B) int c = 3; *auxPt = (R+G+B) / 3.f; auxPt++; } auxInPt += w_step; }
-
I found what was the problem. Qt saves the image pixels in BGR order so, to get the right values it would be necccesary to catch them this way:
B = *auxInPtCol++; G = *auxInPtCol++; R = *auxInPtCol++;
-
Hi,
why don't you use QColor::getRGB() instead? This will give you a nice set if int values.Also docs for QImage::pixelColor have a note:
Warning: This function is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read. -
Hi artwaw,
Thanks for your reply. The thing is that those values are just to test if what I'm getting is right.
The code part with the for's is actually a wrapped function where I pass as a parameter the char* buffer of the image. About passing the QImage as a param it is not an option as I want that this function work with all kind of generical RGB24 buffers
Cheers
-
I found what was the problem. Qt saves the image pixels in BGR order so, to get the right values it would be necccesary to catch them this way:
B = *auxInPtCol++; G = *auxInPtCol++; R = *auxInPtCol++;