5

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;  
1
  • 1
    Almost all C code is valid C++. You'll need to be more specific than that. Commented Jun 22, 2012 at 0:43

1 Answer 1

8

Refer to:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

My 1st problem is with split function. In c++ only accepts a vector. The 2nd problem is that i don't know how to make pY = (unsigned char *)imgY->imageData; in C++. Thanks.
Hi Pascal, are these also equal? //frame = cvCreateImage(cvSize(depth_width, depth_height), 8, 3); // Create the frame cv::Mat frame = cv::Mat(cvSize(depth_width, depth_height), CV_8UC3);
Hi Mona, better use cv::Mat frame = cv::Mat(cv::Size(width, height), CV_8UC3) . In summary, replace cvSize by cv::Size. Also, the name depth_width makes me wonders you might be confusing image depth vs image size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.