图像有损压缩需先转换色域,避免色位或色域错乱。适合计算机显示器的为YCrCb或YCbCr,jpg采用的是这一标准,H264色域YUV也是采用的YCbCr标准。YCrCb具有色域宽,转换损失小的特点。这里采用的是RGB888格式,转换为YCrCb,再转换为RGB888格式,或YCrCb转换为RGB888格式。通过相互转换,验证理解算式。
1. 源文件
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- float Od299 = 0.299; float Od587 = 0.587; float Od114 = 0.114; float Od4187 = 0.4187; float Od0813 = 0.0813; float NOd1687 = -0.1687; float Od3313 = 0.3313; float Od5 = 0.5; float fv128 = 128; float Id402 = 1.402; float Id772 = 1.772; float Od34414 = 0.34414; float Od71414 = 0.71414; void __fastcall TForm1::SpeedButton1Click(TObject *Sender)//分解 { float fR,fG,fB,fY,fCr,fCb; // 输入RGB fR = (float)Edit1->Text.ToInt(); fG = (float)Edit2->Text.ToInt(); fB = (float)Edit3->Text.ToInt(); // 分解 fY = Od299 * fR + Od587 * fG + Od114 * fB; // Y = (0.299 * R) + (0.587 * G) + (0.114 * B) fCb = NOd1687 * fR - Od3313 * fG + Od5 * fB + fv128;// Cb = (-0.1687 * R) - (0.3313 * G) + (0.5 * B) + 128 fCr = Od5 * fR - Od4187 * fG - Od0813 * fB + fv128; // Cr = (0.5 * R) - (0.4187 * G) - (0.0813 * B) + 128 // 显示 Edit4->Text = IntToStr((int)fY); Edit5->Text = IntToStr((int)fCr); Edit6->Text = IntToStr((int)fCb); } //--------------------------------------------------------------------------- void __fastcall TForm1::SpeedButton2Click(TObject *Sender)//合成 { float fR,fG,fB,fY,fCr,fCb; // 输入Y,Cr,Cb fY = (float)Edit4->Text.ToInt(); fCr = (float)Edit5->Text.ToInt(); fCb = (float)Edit6->Text.ToInt(); // 合成 fR = fY + (Id402 * (fCr - fv128)); // R = Y + (1.402 * (Cr - 128)) fG = fY - (Od34414 * (fCb - fv128)) - (Od71414 * (fCr - fv128)); // G = Y - (0.34414 * (Cb - 128)) - (0.71414 * (Cr - 128)) fB = fY + (Id772 * (fCb - fv128)); // B = Y + (1.772 * (Cb - 128)) // 显示 Edit7->Text = FormatFloat("0", fR); Edit8->Text = FormatFloat("0", fG); Edit9->Text = FormatFloat("0", fB); } //---------------------------------------------------------------------------
2. 完整压缩验证文件