Ask Your Question

Revision history [back]

Hi Himanshu!

That seems about right. But if you are concerned about the execution speed, the following might be a bit faster (just extracted common operations out of the loop, a good compiler should be able to generate efficient loops for your code as well):

IplImage* bgr_frame = cvQueryFrame( capture );
int width, height, nchannels, step, offset;
int i, j, r_ch, b_ch, g_ch;

width = bgr_frame->width;
height = bgr_frame->height;
nchannels = bgr_frame->nChannels;
step = bgr_frame->widthStep;

for(i = 0 ; i < height ; i++) 
{
  uchar* data = (uchar*)(bgr_frame->imageData + i*step);
  for( j = 0 ; j < width ; j++)
  {
    offset = j * nchannels;
    b_ch = data[offset];
    g_ch = data[offset + 1];
    r_ch = data[offset + 2];
  }
}

Cheers!

click to hide/show revision 2
added a comment on the code

Hi Himanshu!

That seems about right. But if you are concerned about the execution speed, the following might be a bit faster (just extracted common operations out of the loop, a good compiler should be able to generate efficient loops for your code as well):

IplImage* bgr_frame = cvQueryFrame( capture );
int width, height, nchannels, step, offset;
int i, j, r_ch, b_ch, g_ch;

width = bgr_frame->width;
height = bgr_frame->height;
nchannels = bgr_frame->nChannels;
step = bgr_frame->widthStep;

for(i = 0 ; i < height ; i++) 
{
  uchar* data = (uchar*)(bgr_frame->imageData + i*step);
  for( j = 0 ; j < width ; j++)
  {
    offset = j * nchannels;
nchannels; //You should make sure that nchannels is 3
    b_ch = data[offset];
    g_ch = data[offset + 1];
    r_ch = data[offset + 2];
  }
}

Cheers!