2013-02-26 111 views
-1

我想分析一個.Jpeg圖像600x600,但這樣做我想告訴我,紅色值在230以上,綠色值在200以上。 現在我有這個項目正在進行,但迭代整個圖像需要很長時間,並且會獲得奇怪的像素顏色值(Color= + inttostr(RGB))。 任何人都可以借我一隻手嗎?顏色檢測和分析

type 
    PRGBTripleArray = ^TRGBTripleArray; 
    TRGBTripleArray = array[0..4095] of TRGBTriple; 

procedure TForm2.Button1Click(Sender: TObject); 
var 
    RGB: Byte; 
    X, Y: Integer; 
    R, G, B: Byte; 
    Bitmap1: TBitmap; 
    ColorRGB: LongInt; 
    Pixels: PRGBTripleArray; 
begin 
    Memo1.Lines.Clear; 
    Bitmap1 := TBitmap.Create; 
    try 
    Bitmap1.Assign(Image1.Picture.Graphic); 

    for Y := 0 to Bitmap1.Height - 1 do 
    begin 
     Pixels:= Bitmap1.ScanLine[Y]; 

     Memo1.Lines.Add('======================'); 
     Memo1.Lines.Add('Line # ' + IntToStr(Y)); 
     Memo1.Lines.Add('======================'); 

     for X := 0 to Bitmap1.Width - 1 do 
     begin 
     ColorRGB := ColorToRGB(Bitmap1.Canvas.Pixels[x, y]); 
     R := GetRValue(ColorRGB); 
     G := GetGValue(ColorRGB); 
     B := GetBValue(ColorRGB); 
     RGB:= R*G*B; 

     Memo1.Lines.Add(
       'line=' + IntToStr(Y) 
       + ' row=' + IntToStr(X) 
       + ' Colour=' + IntToStr(RGB) 
       + ' Red=' + IntToStr (R) // red 
       + ' Green=' + IntToStr (G) // blue 
       + ' Blue=' + IntToStr (B) // green 
     ) 
     end; 
    end; 
    finally 
    Bitmap1.free; 
    end; 
end; 

end. 
+4

我根本沒有得到'RGB:= R * G * B;'。你爲什麼要計算產品?另外,您可能需要設置像素格式。 – 2013-02-26 19:22:42

+0

RGB值中的顏色從[0到16581375(255 * 255 * 255)]不等,我不應該使用該產品'R * G * B',但如果有時值爲0,解決方案如何? – Ammadeux 2013-02-26 19:33:04

+0

@AndreasRejbrand如何設置像素格式? – Ammadeux 2013-02-26 19:34:06

回答

2

如果你想記錄你的位圖,它有紅色通道值大於230,綠色值大於200的每個像素,可以使用以下。不要忘記使用BeginUpdate,EndUpdateTMemo.Lines鎖定潛在的頻繁更新。

無論如何,你仍然在混合兩種位圖像素訪問技術。不要將Canvas.Pixels用於大像素陣列操作;這是非常低效的。因爲只使用ScanLine

type 
    PRGBTripleArray = ^TRGBTripleArray; 
    TRGBTripleArray = array[0..4095] of TRGBTriple; 

procedure TForm1.Button1Click(Sender: TObject); 
var 
    C: TColor; 
    X: Integer; 
    Y: Integer; 
    Bitmap: TBitmap; 
    Pixels: PRGBTripleArray; 
begin 
    Bitmap := TBitmap.Create; 
    try 
    Bitmap.Assign(Image1.Picture.Graphic); 

    Memo1.Lines.BeginUpdate; 
    try 
     for Y := 0 to Bitmap.Height - 1 do 
     begin 
     Pixels := Bitmap.ScanLine[Y]; 
     for X := 0 to Bitmap.Width - 1 do 
     begin 
      if (Pixels[X].rgbtRed > 230) and (Pixels[X].rgbtGreen > 200) then 
      begin 
      C := RGB(
       Pixels[X].rgbtRed, 
       Pixels[X].rgbtGreen, 
       Pixels[X].rgbtBlue 
      ); 
      Memo1.Lines.Add(
       '===============' + sLineBreak + 
       'Pixel[' + IntToStr(X) + '; ' + IntToStr(Y) + ']' + sLineBreak + 
       'Color: ' + ColorToString(C) + sLineBreak + 
       'Red channel: ' + IntToStr(Pixels[X].rgbtRed) + sLineBreak + 
       'Green channel: ' + IntToStr(Pixels[X].rgbtGreen) + sLineBreak + 
       'Blue channel: ' + IntToStr(Pixels[X].rgbtBlue) 
      ); 
      end; 
     end; 
     end; 
    finally 
     Memo1.Lines.EndUpdate; 
    end; 
    finally 
    Bitmap.Free; 
    end; 
end; 
+0

'DupeString('=',15)'未聲明的標識符 – Ammadeux 2013-02-26 19:50:43

+0

我該如何聲明它? – Ammadeux 2013-02-26 19:51:37

+2

@Ammadeux:將'StrUtils'添加到'uses'子句中。 – 2013-02-26 19:51:43