The RGB8 color format is an 8 bit monochrome format. Every pixel is represented by one byte. The organization of the pixels in the image buffer is from left to right and bottom up. In other words: The first byte of the image buffer corresponds to the first pixel of the last line of the image.
A video capture device, video format, FrameHandlerSink with a MemBufferCollection, which defines the image data color format must first have been setup. The following code fragments show step-by-step how to access and manipulate the pixel data of RGB8.
First of all, we have to capture an image. Otherwise, the image buffer would be empty. To do so, live mode is started and Grabber::snapImages is called.
The following code retrieves a byte pointer to the image data:
// get pointer to the image data BYTE* pbImgData = pActiveBuf->getPtr();
In this example, we want to output the first (upper left hand) two pixels of the image. In a second step we manipulate the first 3 pixels. As mentioned before, the image data is stored bottom up. Therefore, pbImgData points to the first pixel of the last line of the image. To get access to the first pixel of the first line of the image, the following calculation has to be performed:
// Calculate the index of the upper left pixel // Images are stored upside down in the image buffer SIZE dim = pActiveBuf->getFrameType().dim; int iOffsUpperLeft = (dim.cy-1) * dim.cx;
At first, we retrieve the width and height of the image in terms of pixels. Then the offset to the upper left hand pixel is calculated. Since every pixel is exactly 1 byte in size, we can calculate the offset as:
(Height-1) * Width
Now that we have the offset to the the first pixel, we can read it out:
printf( "\nImage buffer pixel format is eRGB8\n" ); printf( "Pixel 1: %d\n", pbImgData[iOffsUpperLeft ] ); printf( "Pixel 2: %d\n", pbImgData[iOffsUpperLeft+1] );
Instead of only reading pixel data, it is, of course, possible to manipulate the data as well. The following code will set the upper left hand pixel to black and the next 2 pixels to gray and white. After this manipulation, the image is saved as a BMP file.
// overwrite the first 3 pixels and save image to disk pbImgData[iOffsUpperLeft ] = 0; // Set the first pixel to BLACK pbImgData[iOffsUpperLeft+1] = 128; // Set the second pixel to GRAY pbImgData[iOffsUpperLeft+2] = 255; // Set the third pixel to WHITE pActiveBuf->save( "RGB8.bmp" );
To check the result, open the saved image and take a look at the upper left hand pixels. They should look as follows: