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

时间:2012-06-22 00:40:06

标签: opencv

我是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;  

1 个答案:

答案 0 :(得分:7)

参考:

例如,

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界面。