View Full Version : Capture a pixel and return the RGB value

April 11, 2003, 21:08:11
Hey I am new to image processing and would really like to know what is the easiest way to capture a single pixel from the live memory buffer? Thanks.

Stefan Geissler
April 14, 2003, 07:38:24

You will get a memory buffer, that contains the image data of a previously grabbed image (or frame). The image data format depends on the sink type you have set. If you have set the RGB 24 sink type, the pixels in the memory buffer need 3 bytes to store the R, G and B Value. To access a pixel given by a x/y coordinate, use following code:

smart_ptr <MemBuffer> pBuffer
BYTE *pPixel;
m_pInf = pBuffer->getBitmapInfoHeader();
m_iBytesPerPixel = m_pInf->biBitCount / 8;
m_iBytesPerLine = m_pInf->biWidth * m_iBytesPerPixel;

iIndex = (m_pInf->biHeight - y-1) * iBytesPerLine;
iIndex += x * iBytesPerPixel;
pPixel = pBuffer->getPtr();

// If you have a RGB 24 sink type, you can access the pixel now:
B = pPixel[iIndex+2 ];
G = pPixel[iIndex+1];
R = pPixel[iIndex ];

Yunjun Xu
February 14, 2004, 17:45:48
Will you please tell me how to define m_pInf and smart_ptr?
I get compile error said that these are undefined.

I have already include the #include "tisudshl.h".

Yunjun Xu
February 14, 2004, 17:54:47
The following is my code,
The error generate is: memory cannot be accessed.

smart_ptr <MemBuffer> pBuffer;
smart_ptr<BITMAPINFOHEADER> m_pInf = pBuffer->getBitmapInfoHeader();
int m_iBytesPerPixel = m_pInf->biBitCount / 8;
int m_iBytesPerLine = m_pInf->biWidth * m_iBytesPerPixel;

CString indexStr;
indexStr.Format( "%d", m_iBytesPerPixel);

int iIndex;
int x = 1, y = 1;
iIndex = (m_pInf->biHeight - y-1) * m_iBytesPerLine;
iIndex += x * m_iBytesPerPixel;
pPixel = pBuffer->getPtr();

// If you have a RGB 24 sink type, you can access the pixel now:
m_Blue = pPixel[iIndex+2 ];
m_Green = pPixel[iIndex+1];
m_Red = pPixel[iIndex ];

Stefan Geissler
February 16, 2004, 07:42:32

I send an image processing template code to you. Insert your code in the "CListerener::DoImageProcessing" method. And remark, the colors are stored as BGR not as RGB.

March 21, 2004, 17:32:15
error C2065: 'pPixel' : undeclared identifier

What is pPixel ???

Stefan Geissler
March 22, 2004, 07:25:50

pPixel should be of type BYTE*:

BYTE* pPixel=NULL;

This pointer is used, to point to the bytes of each pixel in the memory buffer of the frame. Please remark, the image is saved bottom up. After the pointer is assigned to pPixel by calling getPtr(), it points to the first byte of the first pixel in the last line.

March 22, 2004, 16:35:56
it Work !!!