如何在opencv中将此c代码转换为c

我是opencv的初学者,而Learning Opencv是用C编写的代码.
我需要转换为C.

IplImage *imgY = cvCreateImage(imageSize, IPL_DEPTH_8U, 1);  
IplImage *imgCr = cvCreateImage(imageSize, IPL_DEPTH_8U, 1);  
IplImage *imgCb = cvCreateImage(imageSize, IPL_DEPTH_8U, 1);  
IplImage *imgYCrCb = cvCreateImage(imageSize, img->depth, img->nChannels); 

cvCvtColor(img,imgYCrCb,CV_BGR2YCrCb);  
cvSplit(imgYCrCb, imgY, imgCr, imgCb, 0);  

unsigned char *pY, *pCr, *pCb, *pMask;
pY = (unsigned char *)imgY->imageData;  
pCr = (unsigned char *)imgCr->imageData;  
pCb = (unsigned char *)imgCb->imageData;
pMask = (unsigned char *)mask->imageData;  
最佳答案
参考:

> http://opencv.willowgarage.com/documentation/cpp/c++_cheatsheet.html
>和http://opencv.willowgarage.com/wiki/Welcome?action=AttachFile&do=get&target=opencv_cheatsheet.pdf

例如,

IplImage *imgY = cvCreateImage(imageSize, IPL_DEPTH_8U, 1); 

cv::Mat imgY = cv::Mat(imageSize, CV_8UC1);

cvCvtColor(img,imgYCrCb,CV_BGR2YCrCb);

cv::cvtColor(img, imgYCrCb, CV_BGR2YCrCb);

等等 …

编辑:

回答你的评论:
OpenCV 2无需手动管理内存.

因此,代码可以重写为:

cv::Mat imgYCrCb;
std::vector<cv::mat> yCrCb_channels;

cv::cvtColor(img,imgYCrCb,CV_BGR2YCrCb);   
cv::split(imgYCrCb, yCrCb_channels);
unsigned char * pY = (uchar *) yCrCn_channels[1].data;

建议:阅读http://www.amazon.com/dp/1849513244/?tag=stackoverfl08-20,它涵盖了C opencv接口.

转载注明原文:如何在opencv中将此c代码转换为c - 代码日志