The article below discusses doing the conversion with the older R200 and SR300 camera models in Librealsense 1, and may give you some hints that you can adapt for RS2.
For example, one suggestion for an approach is:
IplImage *I = cvCreateImageHeader(cvSize(dc_frame->size,dc_frame->size),IPL_DEPTH_8U,1);
Bear in mind that I am adapting an instruction to convert cv::VideoCapture to CvCapture, so this may be wrong. You could try:
// failed, print error message
I believe that in the above script, capture1 is a custom variable name chosen by its author and you could use your own name.