Solved Get RGB value of pixel
-
@MHermann said in Get RGB value of pixel:
YUV420P
Well, if you want to get the pixel value on some position in the image you could get the pointer to the image bits "uchar* imageBits()" to get the bits of the internal data and then do the YUV to RGB conversion. This will be faster instead of converting the entire image to RGB, so it depends on what you want to achieve.
-
@ollarch : Thanks.
But at the moment I have the problem that I don't have a valid image.
I get a QVideoFrame in format QVideoFrame::Format_YUV420P.
I think I first have to convert this QVideoFrame to a QImage. And after that I want to search for red pixels in this image.
What is your advice? Can you tell me how I can get a RGB image? Or is it possible to get a YUV image and then search there for the red pixels? -
@MHermann The easiest way to search for red pixels would be to convert the image to RGB first. I don't know how to do the conversion but you can search for this conversion on your search engine. It will not be so difficult to get the RGB pixels from a YUV pixels. You also can look how OpenCV does the conversion.
Finally, for searching the red pixels, do you want only the pixels with R=255,G=0,B=0 ?
-
@ollarch : Ok. At the moment I try to do the conversion correctly.
I am not sure if I want to search only for 255,0,0. Does this matter? Or do you mean that I can use V component of YUV then? -
@MHermann I was only asking. I don't know how to work directly with the YUV components. I was thinking that getting only the (255,0,0) values on a real image will get you 0 pixels as you will not get a perfect red pixels on an image(could be but it will be strange).
-
@ollarch : Ok. Now it works. I am doing the conversion from YUV to RGB. And then I am able to get the RGB values of each pixel.
-
@MHermann now that you have a solution would be great to share your solution with other people to help them if they need to implement the same.
-
@ollarch : Of course. Maybe someone is interested in this:
QImage convertFrameYUV420ToImageRGB888(QVideoFrame vidFrame) { vidFrame.map(QAbstractVideoBuffer::ReadOnly); unsigned char* pData = (unsigned char *) vidFrame.bits(); unsigned char pDataRgb[vidFrame.width()*vidFrame.height()*3]; int bytesPerLine = vidFrame.bytesPerLine(); int numYBytes = vidFrame.width()*vidFrame.height(); int numUBytes = numYBytes / 4; int idxY = 0; int idxU = numYBytes; int idxV = numYBytes+numUBytes; int dstIdx=0; for (int h=0; h<vidFrame.height(); h++) { for (int w=0; w<vidFrame.width(); w++) { idxY = h*bytesPerLine+w; int y = pData[idxY]; idxU = (int)(numYBytes+(h/2)*(bytesPerLine/2)+w/2); int u = pData[idxU]; idxV = (int)(numYBytes*1.25 + (h/2)*(bytesPerLine/2)+w/2); int v = pData[idxV]; pDataRgb[dstIdx++] = y + 1.402 * (v-128); // r pDataRgb[dstIdx++] = y - 0.344 * (u-128) - 0.714 * (v-128); // g pDataRgb[dstIdx++] = y + 1.772 * (u-128); // b } } vidFrame.unmap(); QImage imgRgb (pDataRgb, vidFrame.width(), vidFrame.height(), QImage::Format_RGB888); return imgRgb; }
And after that conversion go over all pixels in the image and access them via QImage::PixelColor().
-
@MHermann Thank you for sharing. Now, I can suggest you some modifications to make it faster.
First of all, the "vidFrame" could be changed to a pointer or a const reference to avoid the copy of the QVideoFrame object(time consuming).
Finally, reading the Qt Docs, the "pixelColor" method "is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read". I suggest you to access directly to the internal data bits and check the values there using data pointer offsets. -
@ollarch : Thanks for your suggestions.
But I think I have to copy the QVideoFrame. Because I can only access the bytes of the frame while they are mapped. I am doing this conversion and further code in a separate thread. So if I map and then unmap the frame the bytes are no longer accessable.