2

所以我已经编写了一个程序,它可以从网络摄像头捕获图像,并将其放入一个名为 pBuffer 的向量中。我可以轻松访问每个像素的 RGB 像素信息,只需通过

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

这里没问题。

下一步现在是创建一个 IplImage* img,并用 pBuffer 的信息填充它……某种 SetPixel。

网上有一个SetPixel函数,即:

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

其中值是 pBuffer 信息,x 和 y 是像素坐标。但是我根本无法使用它。有任何想法吗??我正在使用 C++。

4

1 回答 1

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;
于 2009-04-16T04:47:22.747 回答