2

So i have a program written already that captures the image from a webcam, into a vector called pBuffer. I can easily acess the RGB pixel information of each pixel, simply by

pBuffer[i]=R;pBuffer[i+1]=G;Buffer[i+2]=B.

No problem in here.

The next step is now create an IplImage* img, and fill it in with the information of the pBuffer...some sort of SetPixel.

There is a SetPixel Function on the web, that is :

(((uchar*)(image­>imageData + image­>widthStep*(y))))[x * image­>nChannels + channel] = (uchar)value;

where the value is the pBuffer information, x and y the pixel coordinates.However i simply cannot put this to work. Any ideas?? I am working with C++.

Flot2011
  • 4,601
  • 3
  • 44
  • 61

1 Answers1

1

What you are trying to do you can do like this (assuming width and height are the image dimensions):

CvSize size;
size.height = height;
size.width = width;
IplImage* ipl_image_p = cvCreateImage(size, IPL_DEPTH_8U, 3);

for (int y = 0; y < height; ++y)
    for (int x = 0; x < width; ++x)
        for (int channel = 0; channel < 3; ++channel)
            *(ipl_image_p->imageData + ipl_image_p->widthStep * y + x * ipl_image_p->nChannels + channel) = pBuffer[x*y*3+channel];

However, you don't have to copy the data. You can also use your image data by IplImage (assuming pBuffer is of type char*, otherwise you need possibly to cast it):

CvSize size;
size.height = height ;
size.width = width;
IplImage* ipl_image_p = cvCreateImageHeader(size, IPL_DEPTH_8U, 3);
ipl_image_p->imageData = pBuffer;
ipl_image_p->imageDataOrigin = ipl_image_p->imageData;
Dani van der Meer
  • 6,169
  • 3
  • 26
  • 45
  • the data in pBuffer is BYTE however i can access it as a int –  Apr 16 '09 at 13:34
  • BYTE is a typedef for unsigned char, so you can safely cast it to char. So the code it becomes: ipl_image_p->imageData = (char*)pBuffer; – Dani van der Meer Apr 16 '09 at 13:48
  • dude it worked...however the image created in IplImage is upside down...any idea why? –  Apr 16 '09 at 14:04
  • I'm not sure. But you can try change ipl_image_p->origin. It should be 0 or 1. Try both and see what happends. – Dani van der Meer Apr 16 '09 at 14:14
  • nevertheless tks a lot...im writing my master thesys and this sh** has been bugging me for a while now. BTW where have u learn OpenCV and IplImage? By some book? –  Apr 16 '09 at 14:48
  • We use OpenCV for developing computer vision software. So I learned by doing. I have actually a book called "learning OpenCV". People say it is good. I didn't have time to read it yet... Good luck with your thesis! – Dani van der Meer Apr 16 '09 at 15:11