{"id":301,"date":"2018-10-26T17:25:25","date_gmt":"2018-10-27T00:25:25","guid":{"rendered":"https:\/\/videoq.com\/blog2\/?p=301"},"modified":"2018-10-29T18:32:19","modified_gmt":"2018-10-30T01:32:19","slug":"rgb-yuv-travel-across-color-spaces","status":"publish","type":"post","link":"https:\/\/videoq.com\/blog2\/2018\/10\/26\/rgb-yuv-travel-across-color-spaces\/","title":{"rendered":"RGB &#038; YUV &#8211; Travel across Color Spaces"},"content":{"rendered":"<p>Let&#8217;s talk about\u00a0Color Spaces, Levels and Ranges.<\/p>\n<p style=\"text-align: left;\"><strong>Levels Ranges &amp; Mapping Schemes<\/strong><!--more--><\/p>\n<p>In broadcast environment\u00a0 <strong>RGB<\/strong> (and <strong>Y<\/strong>) values are conventionally shifted and scaled to the range [<strong>16, 235<\/strong>] referred to as<br \/>\n\u201c<strong>Narrow Range<\/strong>\u201d = \u201c<strong>NR<\/strong>\u201d (aka \u201cLimited Range\u201d, \u201cLow RGB\u201d, &#8220;TV\u201c, \u201cBroadcast\u201d, etc.).<\/p>\n<p>In 8 bit representation the boundaries of this range are\u00a0 [<strong>16, 235<\/strong>] , in 10 bit they are [<strong>64, 940<\/strong>], in 12 bit [<strong>256, 3760<\/strong>] and so on.<\/p>\n<p>Thus, for digital 8 bit RGB and Y data:<\/p>\n<ul>\n<li><strong style=\"font-size: 1rem;\">Reference Black Level<\/strong><span style=\"font-size: 1rem;\"> is\u00a0<\/span><strong>16<\/strong> (<strong>64<\/strong> on 10 bit scale),<\/li>\n<li><strong>Reference White Level<\/strong> is <strong>235<\/strong> (<strong>940<\/strong> on 10 bit scale)<\/li>\n<\/ul>\n<p>The headroom above 235 and the footroom below 16 accommodate signal overshoots (&#8220;ringing&#8221;) due to filtering and specular highlights.<\/p>\n<p>The <strong>U<\/strong> &amp; <strong>V<\/strong> signals are just scaled versions of\u00a0Color Difference signals <strong>B-Y<\/strong> and <strong>R-Y<\/strong>, so they are bi-polar by definition.<\/p>\n<p>To fit the available [0, 255] range UV signals are centered (after scaling) at mid-range point 128.<\/p>\n<p>Thus, for UV signals\u00a0<strong>Reference White Level <\/strong>=\u00a0<strong>Reference Black Level<\/strong> = <strong>128 <\/strong>(512 on 10 bit scale).<\/p>\n<p>In broadcast environment levels from<strong> 1<\/strong> to <strong>254<\/strong> are available for video, levels <strong>0<\/strong> and <strong>255<\/strong> are used <strong>exclusively <\/strong>for SDI interface<strong> synchronization<\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-304\" style=\"font-weight: bold; color: #666666; font-size: 0.8125rem; font-style: italic;\" src=\"http:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGBYUV_LevelsMapping.jpg\" alt=\"\" width=\"1688\" height=\"455\" srcset=\"https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGBYUV_LevelsMapping.jpg 1688w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGBYUV_LevelsMapping-300x81.jpg 300w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGBYUV_LevelsMapping-768x207.jpg 768w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGBYUV_LevelsMapping-1024x276.jpg 1024w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><span style=\"font-size: 14px; color: #666666; font-style: italic;\">YUV Narrow Range Levels<\/span><\/p>\n<p>The [<strong>0, 255<\/strong>]\u00a0 range referred to as \u201c<strong>Full Range<\/strong>\u201d = \u201c<strong>FR<\/strong>\u201d (aka \u201cHigh RGB\u201d, &#8220;PC\u201c, \u201cCG\u201d, etc) is also widely used for digital <strong>RGB<\/strong> interfaces, e.g. HDMI.<\/p>\n<p>For <strong>YUV<\/strong> signals the Narrow Range ( [<strong>16, 235 Y<\/strong>], [<strong>16, 240 UV<\/strong>]) is <strong>mandatory<\/strong>; some consumer product use non-standard \u201dyuvj\u201d [0, 255] color space.<\/p>\n<p>In file based environment <strong>all levels<\/strong> from <strong>0<\/strong> to <strong>255<\/strong> are available for video (NR and FR) &#8211; because there is no need to reserve levels 0 and 255 for synchronization purposes.<\/p>\n<p>To avoid possible confusion, due to the plurality of mapping schemes and bit ranges, the international standardization bodies prefer <strong>relative\u00a0 levels schemes<\/strong>\u00a0(also used in this post for all math expressions and coefficients):<\/p>\n<p>For <strong>Y<\/strong> and <strong>RGB<\/strong>: <strong>Black<\/strong> = <strong>0<\/strong>, <strong>White<\/strong> = <strong>1<\/strong>,<br \/>\n<em>and there is no &#8220;legal&#8221; levels beyond these limits<\/em><\/p>\n<p>For <strong>UV<\/strong>:\u00a0<strong>Black<\/strong> = <strong>White<\/strong> = <strong>0<\/strong>, <strong>Min Limit<\/strong> = <strong>-0.5<\/strong>, <strong>Max Limit<\/strong> = <strong>+0.5<\/strong>.<\/p>\n<p style=\"text-align: left;\"><strong>RGB \u21d4<\/strong>\u00a0<strong>YUV<\/strong> <strong>Conversion Matrices<\/strong><\/p>\n<p>Fundamental <strong>RGB to Y<\/strong> <strong>conversion<\/strong> coefficients (single column matrices) are defined by the international standards:<\/p>\n<figure id=\"attachment_312\" aria-describedby=\"caption-attachment-312\" style=\"width: 1593px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-312 size-full\" style=\"font-weight: bold; color: #666666; font-size: 0.8125rem; font-style: italic;\" src=\"http:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2Y_Matrices.jpg\" alt=\"\" width=\"1593\" height=\"355\" srcset=\"https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2Y_Matrices.jpg 1593w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2Y_Matrices-300x67.jpg 300w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2Y_Matrices-768x171.jpg 768w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2Y_Matrices-1024x228.jpg 1024w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption id=\"caption-attachment-312\" class=\"wp-caption-text\">RGB to Y Encoding Matrices<\/figcaption><\/figure>\n<p><strong>UHD<\/strong>\u00a0in the matrix label means two standards:<br \/>\n<strong>BT.2020<\/strong>-NCL (<strong>UHD-SDR<\/strong>),<br \/>\n<strong>BT.2100<\/strong>-NCL (<strong>UHD-HDR<\/strong> &amp; <strong>HD-HDR),<\/strong><br \/>\n<strong>HD<\/strong> means <strong>BT.709<\/strong> and <strong>SD<\/strong> means <strong>BT.601<\/strong> standard.<\/p>\n<p>All matrices are <strong>balanced<\/strong>, i.e. the sums of their coefficients = 1, so the <strong>R = G = B = 1<\/strong> (100% white) input produces <strong>Y = 1<\/strong> (max Y value) output.<\/p>\n<p>For practical applications the RGB to Y gain factors should be\u00a0 rounded to the appropriate bit depth, and it\u00a0 is very important to keep <strong>Unity Gain<\/strong> principle (sum of 3 coefficient must be exactly 1).<\/p>\n<p>The following <strong>RGB \u21d4 YUV<\/strong> matrices are just math\u00a0<strong>derivatives<\/strong> of the 3 fundamental column matrices as above.<\/p>\n<figure id=\"attachment_315\" aria-describedby=\"caption-attachment-315\" style=\"width: 1034px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-315 size-full\" style=\"font-weight: bold; color: #666666; font-size: 0.8125rem; font-style: italic;\" src=\"http:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2YUV_Matrices.jpg\" alt=\"\" width=\"1034\" height=\"307\" srcset=\"https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2YUV_Matrices.jpg 1034w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2YUV_Matrices-300x89.jpg 300w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2YUV_Matrices-768x228.jpg 768w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2YUV_Matrices-1024x304.jpg 1024w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption id=\"caption-attachment-315\" class=\"wp-caption-text\">RGB to YUV Encoding Matrices<\/figcaption><\/figure>\n<p>Note\u00a0 the U &amp; V gain factors = <strong>0.5<\/strong> for the corresponding B &amp; R inputs and <strong>UV White Balance<\/strong> (if R=G=B=1, then U=V=0). This is due to the requested mapping scheme:\u00a0 UV output range is [<strong>-0.5<\/strong>,<strong> +0.5<\/strong>] centered at <strong>0.0<\/strong> point.<\/p>\n<p>MathCAD calculated UV coefficients with high accuracy (up to 5 digits after decimal dot). For practical applications they should be rounded to the appropriate bit depth, and it\u00a0 is very important to keep UV White Balance (sum of 3 coefficients in\u00a0 UV rows must be exactly zero).<\/p>\n<p>Inverse YUV to RGB matrices require high accuracy in all 3 rows:<\/p>\n<figure id=\"attachment_316\" aria-describedby=\"caption-attachment-316\" style=\"width: 1423px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-316 size-full\" src=\"http:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2RGB_Matrices.jpg\" alt=\"\" width=\"1423\" height=\"485\" srcset=\"https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2RGB_Matrices.jpg 1423w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2RGB_Matrices-300x102.jpg 300w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2RGB_Matrices-768x262.jpg 768w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2RGB_Matrices-1024x349.jpg 1024w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption id=\"caption-attachment-316\" class=\"wp-caption-text\">YUV to RGB Decoding Matrices<\/figcaption><\/figure>\n<p>Note the Y to RGB <strong>Unity Gain<\/strong> factor &#8211; all 1st column coefficients in all 3 matrices are exactly 1.<\/p>\n<p>Also note the U to R and V to B gain factors are 0. In other words, it means that the UV axes of YUV color pace are orthogonal and there are no cross-contributions.<\/p>\n<p>Thus, the U input contributes mainly to B and\u00a0 not so much to G; similarly the V input\u00a0contributes mainly to R with some smaller contribution to G.<\/p>\n<p>It should be noted that, in practice, the selected\u00a0<strong>Encoding Color Space<\/strong>\u00a0(i.e. the selection of RGB to YUV and YUV to RGB matrices) may or may not match <strong>Camera Color Primaries<\/strong> and\/or <strong>Display Color Primaries<\/strong>.<\/p>\n<p>Standards define the matrix coefficients and signal levels in relative units, i.e. [<strong>0<\/strong>,<strong> 1<\/strong>] or [<strong>0%<\/strong>, <strong>100%<\/strong>] for RGB and Y, [<strong>-0.5<\/strong>,<strong>\u00a0+0.5<\/strong>] or [<strong>-50%<\/strong>, <strong>+50%<\/strong>] for UV.<\/p>\n<p>Digital signals are usually defined not in percents, but in 8, 10 or 12 bit levels. Also there are two types of digital signal ranges in use: Full &amp; Narrow.<\/p>\n<p>Next image shows the generic block diagram of the RGB data to YUV data conversion process:<\/p>\n<figure id=\"attachment_319\" aria-describedby=\"caption-attachment-319\" style=\"width: 377px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-319 \" src=\"http:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2YUV_ConversionBlockDiag.jpg\" alt=\"\" width=\"377\" height=\"274\" srcset=\"https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2YUV_ConversionBlockDiag.jpg 684w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/RGB2YUV_ConversionBlockDiag-300x218.jpg 300w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><figcaption id=\"caption-attachment-319\" class=\"wp-caption-text\">RGB to YUV Conversion Block Diagram<\/figcaption><\/figure>\n<p>For calculation of 8, 10, 12, etc. bit digital levels it is convenient to use ubiquitous 8 bit values adjusted by the generic scaling parameter BAF:<\/p>\n<p><strong>BAF<\/strong> = BitDepth Adjustment Factor = pow(2, BitDepth &#8211; 8).<\/p>\n<p>The coefficients used for conversion of YUV <strong>relative levels in percents<\/strong> to Narrow Range (aka &#8220;D1&#8221;) YUV and Full Range YUV <strong>10 bit data levels<\/strong>\u00a0are shown below:<\/p>\n<figure id=\"attachment_322\" aria-describedby=\"caption-attachment-322\" style=\"width: 351px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-322\" src=\"http:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2D1.jpg\" alt=\"\" width=\"351\" height=\"162\" srcset=\"https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2D1.jpg 764w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2D1-300x139.jpg 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><figcaption id=\"caption-attachment-322\" class=\"wp-caption-text\">Relative YUV Levels to NR 10 bit Conversion<\/figcaption><\/figure>\n<figure id=\"attachment_356\" aria-describedby=\"caption-attachment-356\" style=\"width: 354px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-356\" src=\"http:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2FR.jpg\" alt=\"\" width=\"354\" height=\"134\" srcset=\"https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2FR.jpg 560w, https:\/\/videoq.com\/blog2\/wp-content\/uploads\/2018\/10\/YUV2FR-300x114.jpg 300w\" sizes=\"auto, (max-width: 354px) 100vw, 354px\" \/><figcaption id=\"caption-attachment-356\" class=\"wp-caption-text\">Relative YUV Levels to FR 10 bit Conversion<\/figcaption><\/figure>\n<p>The 1st column coefficients are 10 bit offsets, 2nd column coefficients are gain factors.<\/p>\n<p>These two sets of coefficients can be used for accurate calculation of\u00a0 digital YUV levels, e.g. Color Bars Test Pattern levels. They are suitable for any Dynamic Range format and for any Encoding Color Space.<\/p>\n<p>See next:<\/p>\n<p>Tables and numerical examples of reference values, such as\u00a0<a href=\"https:\/\/videoq.com\/blog2\/2018\/10\/28\/color-bars-reference-levels\/\"><strong>Color Bars Reference Levels<\/strong><\/a>\u00a0for a variety of <strong>Color Spaces<\/strong>.<\/p>\n<p>Learn more:<\/p>\n<p>See posts in the <a href=\"https:\/\/videoq.com\/blog2\/category\/video-science-fundamentals\/\"><strong>Video Science\u00a0Fundamentals<\/strong><\/a> category.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s talk about\u00a0Color Spaces, Levels and Ranges. Levels Ranges &amp; Mapping Schemes<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-301","post","type-post","status-publish","format-standard","hentry","category-video-science-fundamentals"],"_links":{"self":[{"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":31,"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/posts\/301\/revisions\/460"}],"wp:attachment":[{"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/videoq.com\/blog2\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}