Ask Your Question

Revision history [back]

To build on Mathieu Barnachon's earlier answer, looking through opencv2/imgproc.hpp is key. When using webcams, a common format is YUYV 4:2:2. (Apparently in the Windows world that format is known as YUY2.) To convert that format to BGR which can then be used natively in OpenCV, you want to use the ColorConversionCodes enum COLOR_YUV2BGR_YUYV which is actually an alias for COLOR_YUV2BGR_YUY2.

cv::Mat bgr_mat;
cv::cvtColor(camera_mat, bgr_mat, cv::COLOR_YUV2BGR_YUYV);

If you plan on using the mat in OpenCV, remember to convert to BGR, not RGB.

For additional details on the format, look up fourcc YUYV or see https://www.linuxtv.org/downloads/v4l-dvb-apis-new/uapi/v4l/pixfmt-yuyv.html