Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Aus dem Internet geklaut.
- // Lässt sich aber mithilfe von Wikipedia auch selbst schreiben.
- procedure RGBtoHSV(Red, Green, Blue: Byte; var Hue: Integer; var Saturation, Value: Byte);
- var
- Maximum, Minimum: Byte;
- Rc, Gc, Bc: Single;
- H: Single;
- begin
- Maximum := Max(Red, Max(Green, Blue));
- Minimum := Min(Red, Min(Green, Blue));
- Value := Maximum;
- if Maximum <> 0 then
- Saturation := MulDiv(Maximum - Minimum, 255, Maximum)
- else
- Saturation := 0;
- if Saturation = 0 then
- Hue := 0 // arbitrary value
- else
- begin
- Assert(Maximum <> Minimum);
- Rc := (Maximum - Red) / (Maximum - Minimum);
- Gc := (Maximum - Green) / (Maximum - Minimum);
- Bc := (Maximum - Blue) / (Maximum - Minimum);
- if Red = Maximum then
- H := Bc - Gc
- else if Green = Maximum then
- H := 2 + Rc - Bc
- else
- begin
- Assert(Blue = Maximum);
- H := 4 + Gc - Rc;
- end;
- H := H * 60;
- if H < 0 then
- H := H + 360;
- Hue := Round(H);
- end;
- end;
- function DetectHorizon(ABitmap: TBitmap; ATolerance: Integer): TBitmap;
- var i, j: Integer;
- SrcRGB: PRGBTriple;
- ResRGB: PRGBTriple;
- BaseColor: TColor;
- BaseH:Integer;
- BaseS, BaseV: Byte;
- H: Integer;
- S,V: Byte;
- begin
- // Result Bitmap erstellen
- Result := TBitmap.Create;
- Result.Width := ABitmap.Width;
- Result.Height := ABitmap.Height;
- Result.PixelFormat := pf24Bit;
- // Farbe des Basispixels ermitteln
- // Testweise einfach den Pixel oben links in der Ecke genommen 1/1
- // Außerdem wird die Farbe ins HSV-Format umgewandelt
- BaseColor := ABitmap.Canvas.Pixels[1,1];
- RGBtoHSV(GetRValue(BaseColor), GetGValue(BaseColor), GetBValue(BaseColor), BaseH, BaseS, BaseV);
- // Ursprungsbild/Result durchlaufen
- for i := 0 to Result.Height - 1 do
- begin
- ResRGB := Result.ScanLine[i];
- SrcRGB := ABitmap.ScanLine[i];
- for j := 0 to Result.Width - 1 do
- begin
- // HSV-Werte des aktuellen Pixels ermitteln
- RGBtoHSV(SrcRGB^.rgbtRed, SrcRGB^.rgbtGreen, SrcRGB^.rgbtBlue, h, s, v);
- // Wenn der Unterschied im Farbton (H-Anteil (Hue)) zwischen dem Basispixel
- // und dem aktuellen Pixel > als ATolerance ist, dann Pixel rot färben..
- if (Abs(BaseH-H) > ATolerance) then
- begin
- ResRGB^.rgbtRed := 255;
- ResRGB^.rgbtGreen := 0;
- ResRGB^.rgbtBlue := 0;
- end
- else
- ResRGB^ := SrcRGB^; // ... Ansonsten aus dem Ursprungsbild übernehmen
- inc(SrcRGB);
- inc(ResRGB);
- end;
- end;
- end;
- // Anwendung
- procedure TForm1.Button2Click(Sender: TObject);
- var bmp: TBitmap;
- begin
- bmp := DetectHorizon(imgSrc.Picture.Bitmap,StrToIntDef(Edit1.Text,10)); // ImgSrc ist das Quellbild TImage
- try
- imgRes.Picture.Graphic := bmp; // imgRes ist ein TImage
- finally
- bmp.Free;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement