views:

524

answers:

1
+2  Q: 

DirectX and OpenCV

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; being the value the pBuffer information, x and y the pixel coordinates.However i simply cannot put this to work. Any ideas?? Is in C++.

+1  A: 

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
the data in pBuffer is BYTE however i can access it as a int
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
dude it worked...however the image created in IplImage is upside down...any idea why?
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
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?
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
I used OpenCV in my Masters thesis too... good luck! :)
Zeus