5

我是opencv的初学者,学习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;  
4

1 回答 1

8

参考:

例如,

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 接口。

于 2012-06-22T00:43:56.407 回答