2013-02-26 108 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