Items with no label
3322 Discussions

Convert YUYV frame to RGB8 frame

THark
Novice
2,162 Views

Are there methods available in the librealsense SDK to convert a YUYV frame into a RGB8 frame?

When saving a stream in a .bag file I prefer saving the files in YUYV format, to minimize CPU load.

However, when viewing/ exporting the color data of the stream I'd like to convert this into RGB8 colors.

Therefore I was wondering if a conversion from a YUYV frame to RGB8 frame is integrated in the SDK.

0 Kudos
6 Replies
MartyG
Honored Contributor III
1,089 Views

There is a tool called Convert for converting a bag file into a range of formats such as PNG, color frames only or depth frames only. Would this be an acceptable alternative to a YUYV to RGB8 conversion?

https://github.com/IntelRealSense/librealsense/tree/master/tools/convert librealsense/tools/convert at master · IntelRealSense/librealsense · GitHub

THark
Novice
1,089 Views

The converter will still keep the same image format if I understand correctly. For example, when selecting the png export, the exported color frames are still gray. So this does not seem to be the solution I'm looking for. It could be that I just need to write a solution myself, but I was just wondering if this conversion between YUYV and RGB8 already had been done before.

MartyG
Honored Contributor III
1,089 Views

YUYV to RGB conversion can be done using OpenCV. Here's an example:

http://chapmankids.net/blog/2016/09/16/yuyv-yuv422-to-bgrrgb-conversion-for-logitch-c270-camera-usin... YUYV (YUV422) to BGR/RGB conversion (for Logitch C270 camera using openCV) | Chapman Kids Blog

THark
Novice
1,089 Views

Ok great, then I'll use OpenCV to do the conversion.

THark
Novice
1,089 Views

It turns out that OpenCV has quite a friendly mechanism to convert between different image formats.

Below you can find a snippet of the code I'm using to convert from YUYV to a BGR frame (I had some issues with a RGB frame).

Hopefully this will be useful for other people trying to do similar conversions.

It should be relatively easy to change the YUYV or BGR format for different image formats when necessary.

// Includes

# include

# include

// Code snippet

rs2::pipeline pipe;

rs2::config cfg;

//Create a configuration for configuring the pipeline with a non default profile

int width = 1280;

int height = 720;

cfg.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_YUYV, 30);

pipe.start(cfg);

// Record x frames

int x = 10;

for (int i = 0; i < x; i++){

auto frames = pipe.wait_for_frames();

rs2::frame color_frame = frames.get_color_frame();

// Creating OpenCV Matrix from a color image.

cv::Mat color(cv::Size(width, height), CV_8UC2, (void*)color_frame.get_data(), cv::Mat::AUTO_STEP);

cv::Mat colorRGB;

// Apply color conversion

cvtColor(color, colorRGB, cv::COLOR_YUV2BGRA_YUY2);

// Display the last frame of the recording to test if the conversion worked (the first frame of the recording is quite dark)

if (i ==x-1) {

imshow("RGB image", colorRGB);

cv::waitKey(0);

}

}

MartyG
Honored Contributor III
1,089 Views

Awesome, thanks so much for sharing your method with the community!

Reply