João Freire
João Freire

Reputation:

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;

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++.

Upvotes: 2

Views: 3054

Answers (1)

Dani van der Meer
Dani van der Meer

Reputation: 6207

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;

Upvotes: 1

Related Questions