Data compression (four)-color space conversion (non-sampling version) (2024)

experiment one:

  • Write RGB to YUV program, focus on function definition, initialization and calling of some lookup tables, buffer allocation. Convert the obtained RGB file to YUV file and watch it with YUV Viewer player to verify whether it is correct.

  • Write a program to convert YUV to RGB. Use this program to convert the given experimental data into RGB files. And with the original

    RGB files are compared, and if there are errors, analyze where the errors come from.

Article Directory

  • (1) Conversion formula and file storage format of YUV and RGB
  • (Two) the command line parameters of the main function
    • 2.1 Representation
    • 2.2 How to use
  • (3) Preliminary realization of color space conversion (no sampling) code
    • main.cpp
    • rgb2yuv.h
    • rgb2yuv.cpp
    • yuv2rgb.h
    • yuv2rgb.cpp
    • Experimental results
  • (4) Analysis of the causes of experimental errors and code modification
    • Error correction
    • Experimental results
  • (5) Optimize the code (using the lookup table method)
    • main.cpp
    • yuvrgb.h
    • yuvrgb.cpp
    • Experimental results

R=(298×Y+411×V57344)>>8G=(298×Y101×U211×V+34739)>>8B=(298×Y+519×U71117)>>8Y=(66×R+129×G+25×B)>>8+16U=(38×R74×G+112×B)>>8+128V=(112×R94×G18×B)>>8+128\begin{aligned} &R=(298\times Y+411\times V-57344)>>8\\ &G=(298\times Y-101\times U-211\times V+34739)>>8\\ &B=(298\times Y+519\times U-71117)>>8\\ &Y=(66\times R+129\times G+25\times B)>>8+16\\ &U=(-38\times R-74\times G+112\times B)>>8+128\\ &V=(112\times R-94\times G-18\times B)>>8+128 \end{aligned}R=(298×Y+411×V57344)>>8G=(298×Y101×U211×V+34739)>>8B=(298×Y+519×U71117)>>8Y=(66×R+129×G+25×B)>>8+16U=(38×R74×G+112×B)>>8+128V=(112×R94×G18×B)>>8+128

  The above formula is already quantified.

  Check out the information,RGBRGBRGBThe file storage format isBGRBGRBGRBGR BGR BGR\cdotsBGRBGRBGR,YUVYUVYUVThe file storage format is first save allYYY, Save allUUUAnd finally save allVVV

2.1 Representation

  One programmain()main()main()The function can contain two parameters:

  • The first parameter isintintintTypes of;
  • The second parameter is a string array;

  Usually, the first parameter is namedargcargcargc, The second parameter isargvargvargv. Since the declaration of the string array in the function header can have two forms, somain()main()main()There are also two ways to write functions.

  1. main()main()main()functionWriting one

    int main(int argc, char** argv){ return 0;}
  2. main()main()main()functionWriting two:

    int main(int argc, char* argv[]){ return 0;}

2.2 How to use

  • The meaning of the parameters:

      int argc​: Represents the number of strings.argc = 1 + the number of strings entered by the user, The value of argc is calculated automatically by the operating system, and the programmer does not need to assign it.

      char argv[]*: It stores multiple strings, the form of the string is as follows:

    argv[0] = the name of the executable file. For example, change.exe. (This string does not require user input, and is the same as argc, which can be automatically generated by the operating system.

    argv[1] = string 1

    argv[2] = string 2

    argv[3] = string 3

    \vdots

  • How to input parameters in programming mode?

  The platform used isVisualStudio2019Visual Studio 2019VisualStudio2019, The file to be used isdown.rgbdown.rgbdown.rgb, The file to be generated isup.yuv,cho.rgbup.yuv,cho.rgbup.yuv,cho.rgb,The steps of parameter input are shown in the following figure:

1. Open the properties window of the upper taskbar debugging interfaceData compression (four)-color space conversion (non-sampling version) (1)
2. Select debug in configuration propertiesData compression (four)-color space conversion (non-sampling version) (2)
3. Modify the command parameters as requiredData compression (four)-color space conversion (non-sampling version) (3)

  Based on the above knowledge, the preliminary realization of color space conversion can be easily carried out. Header filergb2yuv.h,yuv2rgb.hrgb2yuv.h,yuv2rgb.hrgb2yuv.h,yuv2rgb.hAnd source filesmain.cpp,rgb2yuv.cpp,yuv2rgb.cppmain.cpp,rgb2yuv.cpp,yuv2rgb.cppmain.cpp,rgb2yuv.cpp,yuv2rgb.cppcomposition.

  Solution Explorer is shown in the figure below:

Data compression (four)-color space conversion (non-sampling version) (4)

  Experiment codeas follows:

main.cpp

#include <iostream>#include <cstdio>#include <fstream>#include "rgb2yuv.h"#include "yuv2rgb.h"using namespace std;#define size 196608#define usize 65536#define vsize 131072using namespace std;int main(int argc, char** argv){ifstream infile(argv[1],ios::binary);ofstream outYUV(argv[2], ios::binary);ofstream outRGB(argv[3], ios::binary);if (!infile) { cout << "error to open file1!" << endl; }if (!outYUV) { cout << "error to open file2" << endl; }if (!outRGB) { cout << "error to open file3" << endl; }unsigned char* in = new unsigned char[size];unsigned char* YUV = new unsigned char[size];unsigned char* RGB = new unsigned char[size];infile.read((char*)in, size);rgb2yuv(in,YUV,size, usize, vsize);//First conversionyuv2rgb(YUV, RGB, usize, vsize);//The second conversion/*for (int i = 0; i < size; i++){if (abs(in[i] - RGB[i]) > 5)cout << "i=" << i << " in[" << i << "]=" << int(in[i]) << " RGB[" << i << "]=" << int(RGB[i]) << endl;}*/outYUV.write((char*)YUV, size);outRGB.write((char*)RGB, size);delete in;delete YUV;delete RGB;infile.close();outYUV.close();outRGB.close();return 0;}

rgb2yuv.h

#pragma oncevoid rgb2yuv(unsigned char* rgb, unsigned char* yuv, int size,int usize,int vsize);

rgb2yuv.cpp

void rgb2yuv(unsigned char* rgb, unsigned char* yuv,int size,int usize,int vsize){unsigned char r, g, b, y, u, v;int j = 0;for (int i = 0;i < size;){b = *(rgb + i);g = *(rgb + i + 1);r = *(rgb + i + 2);y = ((66 * r + 129 * g + 25 * b) >> 8) + 16;u = ((-38 * r - 74 * g + 112 * b) >> 8) + 128;v = ((112 * r - 94 * g - 18 * b) >> 8) + 128;*(yuv + j) = y;*(yuv + j + usize) = u;*(yuv + j + vsize) = v;i = i + 3;//Each rgb is 1 groupj++;}}

yuv2rgb.h

#pragma oncevoid yuv2rgb(unsigned char* yuv, unsigned char* rgb,int usize,int vsize);

yuv2rgb.cpp

#pragma once#include "yuv2rgb.h"#include <iostream>using namespace std;void yuv2rgb(unsigned char* yuv, unsigned char* rgb,int usize,int vsize){unsigned char r, g, b, y, u, v;int j = 0;for (int i = 0; i < usize; i++){y = *(yuv + i);u = *(yuv + i + usize);v = *(yuv + i + vsize);r = (298 * y + 411 * v - 57344) >> 8;g = (298 * y - 101 * u - 211 * v + 34739) >> 8;b = (298 * y + 519 * u - 71117) >> 8;*(rgb + j) = b;*(rgb + j + 1) = g;*(rgb + j + 2) = r;j = j + 3;}}

Experimental results

down.rgbup.yuvcho.rgb
Data compression (four)-color space conversion (non-sampling version) (5)Data compression (four)-color space conversion (non-sampling version) (6)Data compression (four)-color space conversion (non-sampling version) (7)

among them,down.rgbdown.rgbdown.rgbwithcho.rgbcho.rgbcho.rgbuseYUVviewerPlusYUVviewerPlusYUVviewerPlusThe way to open is:

Data compression (four)-color space conversion (non-sampling version) (8)

  The opened image is an inverted image (due tobmpbmpbmpThe image format is stored backwards, so.rgb.rgb.rgbImage usebmpbmpbmpIt will fall when the mode is opened). The pictures in the above table have been rotated using WeChat for easy identification, butYUVYUVYUVFiles andRGBRGBRGBThere is still mirror flipping between files, but it does not affect viewing and comparison.

  up.yuvup.yuvup.yuvuseYUVviewerPlusYUVviewerPlusYUVviewerPlusThe way to open is:

Data compression (four)-color space conversion (non-sampling version) (9)

  It can be seen from the comparison chart of the three images,RGBtoYUVRGB to YUVRGBtoYUV'S experiment was successfully completed, andYUVtoRGBYUVtoRGBYUVtoRGBThere is a problem with the experiment, which is transferred outcho.rgbcho.rgbcho.rgbThere are more red noise in the image.

Error correction

  Inferred available, in progressYUVtoRGBYUVtoRGBYUVtoRGBWhen you getRGBRGBRGBThree figures may exceedunsignedcharunsigned\ charunsignedcharThe range that the type can represent, that is, possible<0<0<0or>255>255>255

   So it’s necessary toyuv2rgb.cppyuv2rgb.cppyuv2rgb.cppThe file is appropriately revised,>255>255>255Values ​​are direct=255=255=255,<0<0<0Values ​​are direct=0=0=0

  After modificationyuv2rgb.cppyuv2rgb.cppyuv2rgb.cppIs as follows:

#pragma once#include "yuv2rgb.h"#include <iostream>using namespace std;void yuv2rgb(unsigned char* yuv, unsigned char* rgb,int usize,int vsize){int r, g, b, y, u, v;int j = 0;for (int i = 0; i < usize; i++){y = int(*(yuv + i));u = int(*(yuv + i + usize));v = int(*(yuv + i + vsize));r = (298 * y + 411 * v - 57344) >> 8;if (r > 255) { r = 255; }if (r < 0) { r = 0; }g = (298 * y - 101 * u - 211 * v + 34739) >> 8;if (g > 255) { g = 255; }if (g < 0) { g = 0; }b = (298 * y + 519 * u - 71117) >> 8;if (b > 255) { b = 255; }if (b < 0) { b = 0; }*(rgb + j) = unsigned char(b);*(rgb + j + 1) = unsigned char(g);*(rgb + j + 2) = unsigned char(r);j = j + 3;}}

Experimental results

down.rgbup.yuvcho.rgb
Data compression (four)-color space conversion (non-sampling version) (10)Data compression (four)-color space conversion (non-sampling version) (11)Data compression (four)-color space conversion (non-sampling version) (12)

  So far, almost doneRGBtoYUVRGB to YUVRGBtoYUVwithYUVtoRGBYUVtoRGBYUVtoRGBTwo experiments.

  Using a lookup table to optimize the code. Header fileyuvrgb.hyuvrgb.hyuvrgb.hAnd source filesmain.cpp,yuvrgb.cppmain.cpp,yuvrgb.cppmain.cpp,yuvrgb.cppcomposition.

  Solution Explorer is shown in the figure below:

Data compression (four)-color space conversion (non-sampling version) (13)

main.cpp

#include <iostream>#include <cstdio>#include <fstream>#include "yuvrgb.h"using namespace std;#define size 196608#define usize 65536#define vsize 131072#define height 256#define weight 256//Lookup table initializationint* RGBYUV298 = new int[256];int* RGBYUV411 = new int[256];int* RGBYUV101 = new int[256];int* RGBYUV211 = new int[256];int* RGBYUV519 = new int[256];int* RGBYUV66 = new int[256];int* RGBYUV129 = new int[256];int* RGBYUV25 = new int[256];int* RGBYUV38 = new int[256];int* RGBYUV74 = new int[256];int* RGBYUV112 = new int[256];int* RGBYUV94 = new int[256];int* RGBYUV18 = new int[256];int main(int argc, char** argv){initLookupTable();ifstream infile(argv[1],ios::binary);ofstream outYUV(argv[2], ios::binary);ofstream outRGB(argv[3], ios::binary);if (!infile) { cout << "error to open file1!" << endl; }if (!outYUV) { cout << "error to open file2" << endl; }if (!outRGB) { cout << "error to open file3" << endl; }unsigned char* infi = new unsigned char[size];unsigned char* YUVfi = new unsigned char[size];unsigned char* RGBfi = new unsigned char[size];infile.read((char*)infi, size);rgb2yuv(infi, YUVfi, size, usize, vsize);yuv2rgb(YUVfi, RGBfi, usize, vsize);outYUV.write((char*)YUVfi, size);outRGB.write((char*)RGBfi, size);fileend(infi,YUVfi,RGBfi);infile.close();outYUV.close();outRGB.close();return 0;}

yuvrgb.h

#pragma oncevoid yuv2rgb(unsigned char* yuv, unsigned char* rgb,int usize,int vsize);void rgb2yuv(unsigned char* rgb, unsigned char* yuv, int size, int usize, int vsize);void initLookupTable();void fileend(unsigned char* infi, unsigned char* YUVfi, unsigned char* RGBfi);

yuvrgb.cpp

#pragma once#include "yuvrgb.h"#include <iostream>using namespace std;extern int* RGBYUV298;extern int* RGBYUV411;extern int* RGBYUV101;extern int* RGBYUV211;extern int* RGBYUV519;extern int* RGBYUV66 ;extern int* RGBYUV129;extern int* RGBYUV25 ;extern int* RGBYUV38 ;extern int* RGBYUV74 ;extern int* RGBYUV112;extern int* RGBYUV94 ;extern int* RGBYUV18 ;void initLookupTable(){for (int i = 0; i < 256; i++){RGBYUV298[i] = 298 * i;RGBYUV411[i] = 411 * i;RGBYUV101[i] = 101 * i;RGBYUV211[i] = 211 * i;RGBYUV519[i] = 519 * i;RGBYUV66[i] = 66 * i;RGBYUV129[i] = 129 * i;RGBYUV25[i] = 25 * i;RGBYUV38[i] = 38 * i;RGBYUV74[i] = 74 * i;RGBYUV112[i] = 112 * i;RGBYUV94[i] = 94 * i;RGBYUV18[i] = 18 * i;}}void yuv2rgb(unsigned char* yuv, unsigned char* rgb,int usize,int vsize){int r, g, b, y, u, v;int j = 0;for (int i = 0; i < usize; i++){y = int(*(yuv + i));u = int(*(yuv + i + usize));v = int(*(yuv + i + vsize));/*r = (298 * y + 411 * v - 57344) >> 8;*/r = (RGBYUV298[y]+ RGBYUV411[v]-57344)>>8;if (r > 255) { r = 255; }if (r < 0) { r = 0; }/*g = (298 * y - 101 * u - 211 * v + 34739) >> 8;*/g = (RGBYUV298[y] - RGBYUV101[u] - RGBYUV211[v] + 34739) >> 8;if (g > 255) { g = 255; }if (g < 0) { g = 0; }/*b = (298 * y + 519 * u - 71117) >> 8;*/b = (RGBYUV298[y] + RGBYUV519[u] - 71117) >> 8;if (b > 255) { b = 255; }if (b < 0) { b = 0; }*(rgb + j) = unsigned char(b);*(rgb + j + 1) = unsigned char(g);*(rgb + j + 2) = unsigned char(r);j = j + 3;}}void rgb2yuv(unsigned char* rgb, unsigned char* yuv, int size, int usize, int vsize){int r, g, b, y, u, v;int j = 0;for (int i = 0; i < size;){b = int(*(rgb + i));g = int(*(rgb + i + 1));r = int(*(rgb + i + 2));/*y = ((66 * r + 129 * g + 25 * b) >> 8) + 16;*/y = ((RGBYUV66[r] + RGBYUV129[g] + RGBYUV25[b]) >> 8) + 16;/*u = ((-38 * r - 74 * g + 112 * b) >> 8) + 128;*/u = ((-RGBYUV38[r] - RGBYUV74[g] + RGBYUV112[b]) >> 8) + 128;/*v = ((112 * r - 94 * g - 18 * b) >> 8) + 128;*/v = ((RGBYUV112[r] - RGBYUV94[g] - RGBYUV18[b]) >> 8) + 128;/*if ((y > 255) || (u > 255) || (v > 255) || (y < 0) || (u < 0) || (v < 0)){cout << "y=" << y << "u=" << u << "v=" << v << endl;}*/*(yuv + j) = unsigned char(y);*(yuv + j + usize) = unsigned char(u);*(yuv + j + vsize) = unsigned char(v);i = i + 3;//Each rgb is 1 groupj++;}}void fileend(unsigned char* infi, unsigned char* YUVfi, unsigned char* RGBfi){delete infi;delete YUVfi;delete RGBfi;deleteRGBYUV298;deleteRGBYUV411;deleteRGBYUV101;deleteRGBYUV211;deleteRGBYUV519;deleteRGBYUV66;deleteRGBYUV129;deleteRGBYUV25;deleteRGBYUV38;deleteRGBYUV74;deleteRGBYUV112;deleteRGBYUV94;deleteRGBYUV18;}

Experimental results

down.rgbup.yuvcho.rgb
Data compression (four)-color space conversion (non-sampling version) (14)Data compression (four)-color space conversion (non-sampling version) (15)Data compression (four)-color space conversion (non-sampling version) (16)

  So far, 4:4:4 is completedRGBRGBRGBFile with 4:4:4YUVYUVYUVConversion between files.

Data compression (four)-color space conversion (non-sampling version) (2024)

FAQs

What is 4 4 4 color space? ›

Full color depth is usually referred to as 4:4:4. The first number indicates that there are four pixels across, the second indicates that there are four unique colors, and the third indicates that there are four changes in color for the second row. These numbers are unrelated to the size of individual pixels.

What's the difference between RGB and YUV? ›

As we know, RGB stands for red, blue and green and YUV stands for (Y) luma, or brightness, (U) blue projection and (V) red projection. HSL stands for (H) hue, (S) saturation, and L (lightness) and CMYK stands for (C ) cyan, (M) magenta, (Y) yellow and (K) black. Still with us?

What is colorspace conversion? ›

Color space conversion is the translation of the representation of a color from one basis to another. This typically occurs in the context of converting an image that is represented in one color space to another color space, the goal being to make the translated image look as similar as possible to the original.

What does 4 4 4 mean? ›

Many believe that encountering 444 signifies the presence of angels, offering protection and support during challenging times.

Is 4.2 2 better than 4.2 0? ›

- 4:2:0 and 4:2:2 are different methods of colour sampling during signal encoding. - 4:2:0 records less colour information than 4:2:2, which affects colour grading and edge transitions. - 4:2:2 provides more flexibility for colour grading, and gives better results for green screen footage.

What is the difference between YCbCr 4 4 4 and full RGB? ›

For monitors, always use full RGB (0-255). For TVs, use YCbCr 4:4:4 (preferably). From there, to YCbCr 4:2:2 and to YCbCr 4:2:0 the amount of chroma subsampling increases, while the bandwidth required decreases. With that, also the quality degrades, as more subpixel color values are discarded/approximated.

What's the advantage of using Yuv color space? ›

YUV color-spaces are a more efficient coding and reduce the bandwidth more than RGB capture can. Most video cards, therefore, render directly using YUV or luminance/chrominance images. The most important component for YUV capture is always the luminance, or Y component.

What is YUV used for? ›

In computer video, the term YUV almost always refers to one particular color space named Y'CbCr, discussed later. However, YUV is often used as a general term for any color space that works along the same principles as Y'CbCr.) The Y' component, also called luma, represents the brightness value of the color.

Which color space is best? ›

Most digital devices use sRGB, which has a wider color gamut than CMYK. In addition, many photo printers use sRGB. For digital uses, such as posting on social media or displaying on your smartphone, sRGB is always going to be a safe bet.

What is an example of a color space? ›

Some of the most common color spaces used within design include arbitrary systems such as the Pantone collection, alongside structured mathematical systems like Adobe RGB, sRGB and CIELAB. A color space is a useful way of organizing colors to understand the color capabilities of a particular device or digital file.

How does color conversion work? ›

Color space conversion is what happens when a Color Management Module (CMM) translates color from one device's space to another. Conversion may require approximations in order to preserve the image's most important color qualities.

What pixel format should I use? ›

Each pixel format has it's unique advantages and disadvantages. 8-bit formats are compact, but lack quality. 32-bit ones are more accurate, but overkill in some situations. Compressed formats are great for storing raw frames, but bad for effects processing.

What is the 777 number? ›

The angel numbers 777, 7777 or 77777 are strong signs from the universe of intuition, connection and growth. "This is a number associated with divine luck," Thomas tells PEOPLE. At the same time, a series of sevens is also tied to "fresh and lucrative opportunities." He puts forth, "We could see a lucky breakthrough."

Why is 4 so important? ›

Other examples for the importance of number four are the 4 seasons, the 4 sides and the 4 corners of a square, the rare four-leafed clover, a symbol of good luck in some cultures, in Hinduism the 4 faces of Brahma the creator, the 4 directions, the 4 elements water, fire earth and air and, finally, the 4 human ...

What does 4 4 4 mean in color? ›

4:4:4 may refer to: Digital images or video in which all color components have the same sampling rate, thus not using chroma subsampling. Another name for the RGB color space.

What is the best color space setting for TV? ›

sRGB is the most widely used color space and is compatible with the largest range of TV models. It is used in operating systems, TV shows, and games. Choosing the DCI-P3 color space can result in videos that appear more vivid and lifelike.

What is the difference between raw and 444? ›

444XQ is the closest to uncompressed data and RAW is visually identical to 444XQ but with 60% of the file size. The problem with Prores RAW is that it's not supported on Resolve and You are forced to transcode to another format for usage in that software.

How do I know if my TV is chroma 4 4 4? ›

Just open up our test pattern in Windows Paint using a PC, then observe it and check if any of the lines and text are blurred together. If none of the text blends together and shows artifacting, then the TV and mode you are using does not use chroma compression and is showing chroma 4:4:4.

References

Top Articles
25 vegetarian Christmas dinner recipes
Easy Baked Feta Pasta - TikTok Recipe
neither of the twins was arrested,传说中的800句记7000词
Where are the Best Boxing Gyms in the UK? - JD Sports
Blorg Body Pillow
Television Archive News Search Service
Mrh Forum
Algebra Calculator Mathway
Archived Obituaries
Ati Capstone Orientation Video Quiz
877-668-5260 | 18776685260 - Robocaller Warning!
Erskine Plus Portal
Walgreens Alma School And Dynamite
Umn Pay Calendar
123 Movies Black Adam
Imbigswoo
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Moe Gangat Age
Seafood Bucket Cajun Style Seafood Restaurant in South Salt Lake - Restaurant menu and reviews
Facebook Marketplace Charlottesville
Chicken Coop Havelock Nc
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
R/Afkarena
Les Rainwater Auto Sales
Jenn Pellegrino Photos
Dtab Customs
Wausau Obits Legacy
H12 Weidian
Pickswise Review 2024: Is Pickswise a Trusted Tipster?
Ahn Waterworks Urgent Care
Breckie Hill Mega Link
The Banshees Of Inisherin Showtimes Near Broadway Metro
Preggophili
Downtown Dispensary Promo Code
Yayo - RimWorld Wiki
Osrs Important Letter
Why comparing against exchange rates from Google is wrong
Duke Energy Anderson Operations Center
Acuity Eye Group - La Quinta Photos
Car Crash On 5 Freeway Today
Unity Webgl Player Drift Hunters
Die Filmstarts-Kritik zu The Boogeyman
3400 Grams In Pounds
Evil Dead Rise (2023) | Film, Trailer, Kritik
What Does Code 898 Mean On Irs Transcript
Encompass.myisolved
Acts 16 Nkjv
Haunted Mansion Showtimes Near Millstone 14
60 Days From August 16
Campaign Blacksmith Bench
Tyrone Unblocked Games Bitlife
Itsleaa
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 6752

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.