//************************************
// Method: Fill_CV_IplImage
// FullName: Fill_CV_IplImage
// Access: public
// Returns: bool
// Qualifier:
// Parameter: int width
// Parameter: int height
// Parameter: char * imageData
// Parameter: IplImage * pOutImg
// Description:
// pOutImg must be a IplImage* whose header has been created as 4 channels
// and IPL_DEPTH_8U depth
//************************************
void Fill_CV_IplImage(int width, int height, char* imageData, IplImage* pOutImg)
{
// artoolkit video data to IplImage*
// the key lies in the fact that arVideoGetImage returns buffer with order
// RGBA, which is the 4-channel and IPL_DEPTH_8U-depth OpenCV buffer
IplImage *img = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 4);
cvSetImageData(img, imageData, width * 4);
cvCopy(img, pOutImg);
cvReleaseImageHeader(&img);
}
如果需要再额外地将4通道图像转换到3通道的话,那么就需要使用以下的函数。
// Method: Fill_CV_IplImage
// FullName: Fill_CV_IplImage
// Access: public
// Returns: bool
// Qualifier:
// Parameter: int width
// Parameter: int height
// Parameter: char * imageData
// Parameter: IplImage * pOutImg
// Description:
// pOutImg must be a IplImage* whose header has been created as 3 channels
// and IPL_DEPTH_8U depth
//************************************
void Fill_CV_IplImage(int width, int height, char* imageData, IplImage* pOutImg)
{
// artoolkit video data to IplImage*
// the key lies in the fact that arVideoGetImage returns buffer with order
// RGBA, which is the 4-channel and IPL_DEPTH_8U-depth OpenCV buffer
IplImage *img = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 4);
cvSetImageData(img, imageData, width * 4);
// convert 4 channels to 3 channels
IplImage *pRed = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
IplImage *pGreen = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
IplImage *pBlue = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
IplImage *pAlpha = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
cvSplit(img, pBlue, pGreen, pRed, pAlpha);
cvMerge(pBlue, pGreen, pRed, NULL, pOutImg);
cvReleaseImage(&pBlue);
cvReleaseImage(&pGreen);
cvReleaseImage(&pAlpha);
}
推荐一篇博文,关于cvsplit和cvMerge写得很不错