Extract pixel values from a bitmap in Qt
December 24, 2012
The first step in writing an image processing program is usually extracting the image data from a file container such as a bitmap or jpeg file. Using Qt, the QImage class makes this easy. The QImage can open/save directly to bmp, jpeg, and other common container formats. Once a file has been opened into an instance of a QImage, the RGB pixel values can be extracted into a luminance array for processing.
Here is a code example for extraction. In this case, an array of 32-bit value is used for storage even though only the lower byte has a value.
The RGB values are mapped to the luminance vector of the YUV color space.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
void ImageUtils::getPixelsFromBmpIntoLuminance(QImage * image, int *dstPtr)
{
int width = image->width();
int height = image->height();
char * rgbValues = (char *) image->scanLine(0);
QImage::Format format = image->format();
int x = 0;
int y = 0;
int b = 0;
int bv = 0;
int rv = 0;
int gv = 0;
int xOffset = 0;
if (format == QImage::Format_Indexed8)
{
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
xOffset = y * width;
*(dstPtr+xOffset +x ) = rgbValues[b] & 0xff;
b ++;
}
}
}
else if ( format == QImage::Format_RGB32)
{
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
xOffset = y * width;
bv = ((int)rgbValues[b + 0] & 0xff) * 114;
gv = ((int)rgbValues[b + 1]& 0xff) * 587;
rv = ((int)rgbValues[b + 2]& 0xff) * 299;
*(dstPtr+xOffset +x ) = ((((bv + rv + gv) / 1000))) & 0xff;
b += 4;
}
}
}
} |
Entry filed under: Image Processing, Qt. Tags: .