Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert this c code to c++ in opencv

Tags:

opencv

I'm a starter in opencv, and Learning Opencv has is code written in C. I need to convert to 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;  
like image 586
RIK Avatar asked Jun 22 '12 00:06

RIK


1 Answers

Refer to:

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

For example,

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

becomes

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

and

cvCvtColor(img,imgYCrCb,CV_BGR2YCrCb);

becomes

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

and so on ...

Edit :

Answer to your comment : OpenCV 2 removes the need to manage manually the memory.

So, the code can be rewritten as :

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;

Advice : read http://www.amazon.com/dp/1849513244/?tag=stackoverfl08-20 , it covers the C++ opencv interface.

like image 160
Pascal T. Avatar answered Nov 16 '22 23:11

Pascal T.