# YCbCr video frame to BGR Mat

**URL:** <https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964>\
**Category:** Uncategorized\
**Tags:** core, imgcodecs\
**Created:** [October 30, 2021, 8:12am UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964 "2021-10-30T08:12:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![edgar-bcn](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@edgar-bcn](https://forum.opencv.org/u/edgar-bcn)\
**Post date:** [October 30, 2021, 8:12am UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964/1 "2021-10-30T08:12:45Z")

</div>

Hi,

I’m newie to OpenCV and I’m struggling trying to create BGR Mat objects from YCbCr video frames and showing them using Imshow.

I’ve access to raw frames from a third-party videoconference solution through its API.

Within video frame object’s public properties I have access to both YCbCr frame payload and individual color space buffers (array of bytes).

The steps I plan to follow are:

1. YCbCr byte buffers to YCrCb Mat
2. YCrCb Mat to BGR Mat
3. Display BGR Mat

Second and third steps don’t seem to be a problem thanks to OpenCV’s CvtColor and Imshow methods, but how can I solve the first one?

I’m using C# and Emuge wrapper if it helps.

Any help would be appreciated.

Regards,  
Edgar

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [October 30, 2021, 8:28am UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964/2 "2021-10-30T08:28:10Z")

</div>

> [@edgar-bcn](#):
>
> I’m using C# and Emuge wrapper if it helps.

it doesnt, in fact it’s a problem

> [@edgar-bcn](#):
>
> YCbCr byte buffers to YCrCb Mat

this is entirely emgu / c# specific, dont expect much help from here

have you tried [their own place](https://github.com/emgucv/emgucv/discussions) ?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 30, 2021, 9:08am UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964/3 "2021-10-30T09:08:23Z")

</div>

ok so create the Mat from a data pointer (which is then shared), or create the mat and copy data over. I’m sure emgu has those constructors as appropriate for C#

note: the usual order is `YCbCr` but whoever implemented this stuff in OpenCV got the _names_ wrong and calls it `YCrCb`. the code _probably_ does the right thing though. there really ought to be a pull request fixing this and deprecating these broken constants.

be aware of related formats.

- planar vs interleaved
- chroma-subsampled or not

---

<div class="post-metadata">

**Author:** ![edgar-bcn](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@edgar-bcn](https://forum.opencv.org/u/edgar-bcn)\
**Post date:** [October 30, 2021, 9:46am UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964/4 "2021-10-30T09:46:14Z")

</div>

Which part of I’m newie with OpenCV and its community did you don’t understand? I did not mean to offend anyone…

Anyway, nothing would make me happy than stick to C++ and be able to use OpenCV directly, but this is a project requirement.

---

<div class="post-metadata">

**Author:** ![edgar-bcn](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@edgar-bcn](https://forum.opencv.org/u/edgar-bcn)\
**Post date:** [October 30, 2021, 9:49am UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964/5 "2021-10-30T09:49:53Z")

</div>

Thank you crackwitz!

---

<div class="post-metadata">

**Author:** ![edgar-bcn](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@edgar-bcn](https://forum.opencv.org/u/edgar-bcn)\
**Post date:** [October 31, 2021, 8:50am UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964/6 "2021-10-31T08:50:59Z")

</div>

Ok, so after some digging and a lot of try & error I’m still facing the same problem: how to get a Mat object from data?

crackwitz pointed me into the right direction, now I know (as per Y, Cb and Cr buffers size) data frames are using color subsampling, not sure if 4:2:0 or other.

So I tried creating Mat objects from channels Y, Cb and Cr, and then merge all together expecting getting the desired YCbCr Mat object.

But it seems Merge function expects an array of Mat objects with the same size, which is -because of subsampling- not the case: Y buffer is 921600 bytes (1280x720) while Cb and Cr buffers are 230400 (640x360).

Any suggestions? I’m in the right path?

Thanks,  
Edgar

---

<div class="post-metadata">

**Author:** ![edgar-bcn](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@edgar-bcn](https://forum.opencv.org/u/edgar-bcn)\
**Post date:** [October 31, 2021, 10:32am UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964/7 "2021-10-31T10:32:33Z")

</div>

Finally format is YUV 4:2:0 planar…

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 31, 2021, 12:24pm UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964/8 "2021-10-31T12:24:43Z")

</div>

just glue all three planes together, don’t interleave.

see if (some of!) this helps: [c - Processing YUV I420 from framebuffer? - Stack Overflow](https://stackoverflow.com/questions/69518644/processing-yuv-i420-from-framebuffer/69519581#69519581)

---

<div class="post-metadata">

**Author:** ![edgar-bcn](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@edgar-bcn](https://forum.opencv.org/u/edgar-bcn)\
**Post date:** [November 2, 2021, 9:40am UTC](https://forum.opencv.org/t/ycbcr-video-frame-to-bgr-mat/5964/9 "2021-11-02T09:40:25Z")

</div>

Hi,

Got it working, just my few cents for newbies to come…

First of all, be sure which format are you handling. You’ll save a lot of time!

My case was I420 and I’d available both in packed (Y, U and V components stored in a single array) and planar (Y, U and V components stored separately) formats.

So I ended creating a Mat object from the packed array keeping in mind the chroma sampling used (4:2:0 means 2:1 horizontal and vertical downsampling) and then converting to BGR (using Yuv2BgrI420 color conversion).

Instead of Imshow I ended saving Mat as Image\<Bgr, Byte\> object while figuring which color conversion to use.

Hope it helps.

Regards,  
Edgar
