YUV/RGB format analysis and quick table Search Algorithm Design

Source: Internet
Author: User

Author: Liu Xuhui colorant@163.com reprinted please indicate the source

Http://blog.csdn.net/colorant/

1 PrefaceThe color of nature is ever changing. In order to give color a quantitative measurement standard, we need to establish a color space model to describe various colors, as human perception of color is a complex process of combination of physiological and psychological effects, in order to better and more accurately meet their needs in different application fields, there are various color space models to quantify the color description. We often see RGB, CMYK, yiq, YUV, and HSI. For the digital electronic multimedia field, we often see the concept of color space, mainly RGB and YUV (in fact, these two systems contain many specific color Expressions and models, such as sRGB, Adobe RGB, yuv422, yuv420 ...), RGB describes the color based on the principle of the three-color + optical system, while YUV describes the color based on the principle of brightness and chromatic aberration. Even if only the two color spaces of rgb yuv are involved, the knowledge involved is also very rich and complex. I am not aware of the relevant professional knowledge, therefore, this article mainly discusses the application and algorithms in the engineering field.2 YUV-related Color Space Model2.1 YUV and yiq ycrcbFor the YUV model, we often confuse it with the yiq/ycrcb model. In fact, the YUV model is used in the pal TV system. y indicates the brightness. UV is not short for any word. The yiq model is similar to the YUV model for ntsc TV systems. The I and Q components in the yiq color space are equivalent to 33 degrees of rotation of the UV components in the YUV space. The YCbCr color space is a color space derived from the YUV color space. It is mainly used in digital TV systems. In the conversion from RGB to YCbCr, the input and output are both octal binary formats. The conversion equation between the three and RGB is as follows: RGB-> YUV is actually: Y = 0.30r + 0.59G + 0.11b, u = 0.493 (B-Y), V = 0.877 (R-Y) RGB-> yiq RGB-> ycrcb from the formula, we need to understand that the UV/cbcr signal is actually the blue and red signals, in fact, it indirectly represents the intensity of blue and red. Understanding this is of great help for us to understand the color transformation process. The YUV format we talked about in the digital electronic multimedia field is actually accurate, it is a family of color models (including yuv444/yuv422/yuv420/yuv420p) that have multiple storage formats based on the ycrcb color space model ). It is not a traditional YUV model used for palting analog TVs. The main difference between these YUV models is that the sampling and storage methods of UV data are not described here. In camera sensor, the most common YUV model is the yuv422 format, because it uses four bytes to describe two pixels and is compatible with the rgb565 model. It facilitates the design of hardware and software interfaces of camera sensor and camera controller.3 yuv2rgb Quick Algorithm AnalysisYUV actually refers to ycrcb 8) The yuv2rgb conversion formula itself is very simple, but involves floating point operations, so if you want to implement a fast algorithm, the algorithm structure itself has nothing to do with, mainly using integer operations or table queries to speed up the computation. First, we can derive the conversion formula: r = Y + 1.4075 * (V-128) G = Y-0.3455 * (u-128)-0.7169 * (V-128) B = Y + 1.779 * (u-128)3.1 integer AlgorithmReplace floating-point operations with integer operations. Of course, shift is required. We can easily obtain the following algorithms: U = yuvdata [upos]-128; V = yuvdata [VPOs]-128; rdif = V + (V * 103)> 8); invgdif = (u * 88)> 8) + (V * 183)> 8); bdif = u + (u * 198)> 8); r = yuvdata [ypos] + rdif; G = yuvdata [ypos]-invgdif; B = yuvdata [ypos] + bdif, make a judgment similar to the following. If (r> 255) r = 255; If (r <0) r = 0; to convert data from rgb24 to rgb565, perform the shift and or operation: rgbdata [1] = (R & 0xf8) | (G> 5); rgbdata [0] = (G & 0x1c) <3) | (B> 3 ));Part 1 lookup MethodThe first thing you can think of in the lookup method is to use the lookup table to replace the multiplication operation in the preceding integer algorithm. Rdif = fac_00004075 [u]; invgdif = fac_m_0_3455 [u] + fac_m_0_7169 [v]; bdif = fac_0000779 [u]; four 1-dimensional arrays are required, the subscript starts from 0 to 255. The table occupies about 1 kb of memory. UV does not need to be reduced by 128. It is enough to calculate the value of the corresponding array element in advance. For each pixel, the partial lookup method uses the lookup table to replace two subtraction operations and four multiplication operations, and four shift operations. However, it still requires multiple addition operations, six comparison operations, and possible assignment operations. The speed of the operation is not significantly higher than that of the first method.3.3 full lookup MethodCan the YUV table directly display the corresponding RGB values? At first glance, it seems unlikely that, taking the most complex g operations as an example, because g and YUV are related, therefore, for an algorithm similar to G = yuv2g [y] [u] [v], an array with a dimension of 256 must occupy about 16 MB of space to the power of 24 of 2, it is absolutely unacceptable. Currently, most of them adopt the partial lookup method. However, if we analyze it carefully, we can find that we do not actually need to use a three-dimensional array for G, because y is only related to the result of UV calculation, and has nothing to do with the UV individual, therefore, we can use the secondary lookup method to simplify the G operation into two-dimensional arrays, as follows: g = yig2g_table [y] [uv2ig_table [u] [v]; RB itself is only related to Yu or YV. Therefore, we need four 8*8 two-dimensional tables which occupy K of memory at the power of 4x2. It is basically acceptable. However, for embedded applications such as mobile phones, it is a little bigger. Further analysis, we can see that in the case of mobile phones and other embedded applications, we ultimately want to convert the data into the rgb565 format and send it to the LCD screen for display. Therefore, for RGB, we don't need the accuracy of 8 bits at all. to unify the simplicity and operation, we only need 6 bits of data for each component, so we can further change the table to four 6*6 two-dimensional tables, so that we only need to occupy 16 K of memory! When calculating the table element value, you can complete the final overflow judgment in advance. The final algorithm is as follows: Y = (yuvdata [y1pos]> 2); U = (yuvdata [upos]> 2 ); V = (yuvdata [VPOs]> 2); r = yv2r_table [y] [v]; G = yig2g_table [y] [uv2ig_table [u] [v]; B = yu2b_table [y] [u]; rgbdata [1] = (R & 0xf8) | (G> 5 )); rgbdata [0] = (G & 0x1c) <3) | (B> 3); in this way, three shift operations are added to the table search method, this further reduces four addition operations and six comparison assignment operations. When calculating the element values in a table, consider the rounding and offset factors to make the calculation intermediate results meet the non-negative requirements of the array downloading. Some tips are required: use the full table query method, compared with the first algorithm, the final computing speed can be significantly improved and the specific performance can be improved, depending on the CPU computing speed of the platform and the relative ratio of memory access speed. The faster the memory access speed, the more obvious the performance improvement brought about by using the look-up table method. The performance of the test results on my PC can be improved by about 35%. On an ARM platform, the test only increased by about 15%.3.4 Further ThoughtsIn fact, the above algorithm: rgbdata [1] = (R & 0xf8) | (G> 5); rgbdata [0] = (G & 0x1c) <3) | (B> 3); (R & 0xf8) and (B> 3) can be calculated in advance in the table. In addition, the value of YU/YV cannot actually cover the range of 6*6. Some vertices in the middle are never input-free, you can also use a 5*5 Table for RB operations. These operations may further increase the computation speed and reduce the table size. In addition, in the embedded application, if you try to put tables in High-Speed memory such as SRAM, you should be able to take advantage of the advantages of the lookup method than in the SDRAM.4 rgb2yuv?Currently, we cannot simplify the table search operation of a three-dimensional table to a two-dimensional table. Only some table queries can be used to replace the multiplication operation. In addition, in most cases, we still need the yuv2rgb conversion, because the data obtained from the sensor usually uses YUV data. In addition, JPG and MPEG are actually encoded Based on YUV format, therefore, it is also necessary to display the decoded data in yuv2rgb 8) Luck.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.