Advertisement
Guest User

Untitled

a guest
Nov 28th, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.83 KB | None | 0 0
  1. // Aus dem Internet geklaut.
  2. // Lässt sich aber mithilfe von Wikipedia auch selbst schreiben.
  3. procedure RGBtoHSV(Red, Green, Blue: Byte; var Hue: Integer; var Saturation, Value: Byte);
  4. var
  5.   Maximum, Minimum: Byte;
  6.   Rc, Gc, Bc: Single;
  7.   H: Single;
  8. begin
  9.   Maximum := Max(Red, Max(Green, Blue));
  10.   Minimum := Min(Red, Min(Green, Blue));
  11.   Value := Maximum;
  12.   if Maximum <> 0 then
  13.     Saturation := MulDiv(Maximum - Minimum, 255, Maximum)
  14.   else
  15.     Saturation := 0;
  16.   if Saturation = 0 then
  17.     Hue := 0 // arbitrary value
  18.   else
  19.   begin
  20.     Assert(Maximum <> Minimum);
  21.     Rc := (Maximum - Red) / (Maximum - Minimum);
  22.     Gc := (Maximum - Green) / (Maximum - Minimum);
  23.     Bc := (Maximum - Blue) / (Maximum - Minimum);
  24.     if Red = Maximum then
  25.       H := Bc - Gc
  26.     else if Green = Maximum then
  27.       H := 2 + Rc - Bc
  28.     else
  29.     begin
  30.       Assert(Blue = Maximum);
  31.       H := 4 + Gc - Rc;
  32.     end;
  33.     H := H * 60;
  34.     if H < 0 then
  35.       H := H + 360;
  36.     Hue := Round(H);
  37.   end;
  38. end;
  39.  
  40. function DetectHorizon(ABitmap: TBitmap; ATolerance: Integer): TBitmap;
  41. var i, j: Integer;
  42.     SrcRGB: PRGBTriple;
  43.     ResRGB: PRGBTriple;
  44.     BaseColor: TColor;
  45.     BaseH:Integer;
  46.     BaseS, BaseV: Byte;
  47.     H: Integer;
  48.     S,V: Byte;
  49. begin
  50.   // Result Bitmap erstellen
  51.   Result := TBitmap.Create;
  52.   Result.Width := ABitmap.Width;
  53.   Result.Height := ABitmap.Height;
  54.   Result.PixelFormat := pf24Bit;
  55.  
  56.   // Farbe des Basispixels ermitteln
  57.   // Testweise einfach den Pixel oben links in der Ecke genommen 1/1
  58.   // Außerdem wird die Farbe ins HSV-Format umgewandelt
  59.   BaseColor := ABitmap.Canvas.Pixels[1,1];
  60.   RGBtoHSV(GetRValue(BaseColor), GetGValue(BaseColor), GetBValue(BaseColor), BaseH, BaseS, BaseV);
  61.  
  62.   // Ursprungsbild/Result durchlaufen
  63.   for i := 0 to Result.Height - 1 do
  64.   begin
  65.     ResRGB := Result.ScanLine[i];
  66.     SrcRGB := ABitmap.ScanLine[i];
  67.  
  68.     for j := 0 to Result.Width - 1 do
  69.     begin
  70.       // HSV-Werte des aktuellen Pixels ermitteln
  71.       RGBtoHSV(SrcRGB^.rgbtRed, SrcRGB^.rgbtGreen, SrcRGB^.rgbtBlue, h, s, v);
  72.  
  73.       // Wenn der Unterschied im Farbton (H-Anteil (Hue)) zwischen dem Basispixel
  74.       // und dem aktuellen Pixel > als ATolerance ist, dann Pixel rot färben..
  75.       if (Abs(BaseH-H) > ATolerance) then
  76.       begin
  77.         ResRGB^.rgbtRed := 255;
  78.         ResRGB^.rgbtGreen := 0;
  79.         ResRGB^.rgbtBlue := 0;
  80.       end
  81.       else
  82.         ResRGB^ := SrcRGB^;  // ... Ansonsten aus dem Ursprungsbild übernehmen
  83.  
  84.       inc(SrcRGB);
  85.       inc(ResRGB);
  86.     end;
  87.   end;
  88. end;
  89.  
  90. // Anwendung
  91. procedure TForm1.Button2Click(Sender: TObject);
  92. var bmp: TBitmap;
  93. begin
  94.   bmp := DetectHorizon(imgSrc.Picture.Bitmap,StrToIntDef(Edit1.Text,10)); // ImgSrc ist das Quellbild TImage
  95.   try
  96.     imgRes.Picture.Graphic := bmp; // imgRes ist ein TImage
  97.   finally
  98.     bmp.Free;
  99.   end;
  100. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement