首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

再學 GDI+ [94]:TGPImage 調整 RGB 三色成分增減 - ColorMatrix 平移分量應用

     編輯:Delphi

利用 TColorMatrix 第 5 行 (索引 4) 的平移分量,獨立調整紅、綠、藍三色通道的亮度偏移。

核心矩陣結構

ColorMatrix: TColorMatrix = (
  (1.0, 0.0, 0.0, 0.0, 0.0), { R' = 1*R + 0*G + 0*B + 0*A + tR }
  (0.0, 1.0, 0.0, 0.0, 0.0), { G' = 0*R + 1*G + 0*B + 0*A + tG }
  (0.0, 0.0, 1.0, 0.0, 0.0), { B' = 0*R + 0*G + 1*B + 0*A + tB }
  (0.0, 0.0, 0.0, 1.0, 0.0), { A' = 0*R + 0*G + 0*B + 1*A + tA }
  (0.0, 0.0, 0.0, 0.0, 1.0)  { W  = 1 (齊次座標) }
);

平移分量映射

矩陣位置影響通道TrackBar 對應數值範圍
ColorMatrix[4,0]紅色偏移TrackBar1-1.0 ~ +1.0 (-100~100)
ColorMatrix[4,1]綠色偏移TrackBar2-1.0 ~ +1.0
ColorMatrix[4,2]藍色偏移TrackBar3-1.0 ~ +1.0

完整程式碼結構

var img: TGPImage; { 表單層級持有,Create/Free 管理 } 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
  img := TGPImage.Create('c:\temp\test.png'); { 專用色彩測試圖 }
  { 三個 TrackBar: Min=-100, Max=100, Position=0 }
  DoubleBuffered := True; 
end; 

procedure TForm1.FormPaint(Sender: TObject); 
var g: TGPGraphics; ImageAttributes: TGPImageAttributes; 
begin 
  g := TGPGraphics.Create(Canvas.Handle); 
  ImageAttributes := TGPImageAttributes.Create; 
  ColorMatrix[4,0] := TrackBar1.Position / 100; { -1.0 到 1.0 }
  ColorMatrix[4,1] := TrackBar2.Position / 100; 
  ColorMatrix[4,2] := TrackBar3.Position / 100; 
  ImageAttributes.SetColorMatrix(ColorMatrix); 
  g.DrawImage(img, MakeRect(10, 10, img.GetWidth, img.GetHeight), 0, 0, img.GetWidth, img.GetHeight, UnitPixel, ImageAttributes); 
  ImageAttributes.Free; g.Free; 
end; 

procedure TForm1.Button1Click(Sender: TObject); { 重設按鈕 } 
begin TrackBar1.Position := 0; TrackBar2.Position := 0; TrackBar3.Position := 0; Repaint; end;

效果圖

RGB 偏移調整效果

數學原理

R' = R + tR
G' = G + tG  
B' = B + tB
{ tR/tG/tB 為 -1.0 到 1.0,對應 -255 到 +255 亮度偏移 }

{ 齊次座標系統:第五行平移分量實現加法偏移 }

應用場景

  • 色彩平衡調整 (白平衡修正)
  • 藝術濾鏡效果 (暖色調/冷色調)
  • 圖像增強預處理
  • 色盲模擬與輔助顯示

此技術利用仿射變換中的平移分量,以極低運算成本實現通道級亮度獨立控制,是即時圖像處理的高效手段。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved