Thema: Delphi zwei Bitmaps vergleichen

Einzelnen Beitrag anzeigen

Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#18

Re: zwei Bitmaps vergleichen

  Alt 2. Feb 2010, 07:21
So dürfte das funzen:

Delphi-Quellcode:
function Bitmapcompare(pic1, pic2: Tbitmap; Posx,posy: Integer): Boolean;
var
  Pix1, Pix2 : PByte;
  y, k, x : Integer;
  bytes: Byte;
  compix, matchpix: integer;
const
  PixelFormatBytes: Array[TPixelFormat] of Byte = ( 0, 0, 0, 1, 0, 2, 3, 4, 0 );
begin
  result:=false;
  bytes := PixelFormatBytes[pic1.PixelFormat];
  if bytes <> PixelFormatBytes[pic2.PixelFormat] then
    Exit;
  if (bytes = 0) then
    Exit; // PixelFormat wird nicht unterstützt ... kannst du dann gerne von mir aus umändern ...
  if (pic1.Width <> pic2.Width) or (pic1.Height <> pic2.Height) then
    Exit;
  matchpix := 0;
  compix := 0;
  for y := 0 to pic2.Height - 1 do
  begin
    Pix1 := pic1.Scanline[posy+y];
    Pix2 := pic2.Scanline[y];
    inc( Pix2, Posx*bytes );
    for x := 0 to pic2.Width - 1 do
      for k := 0 to bytes - 1 do
      begin
    //VERGLEICH
        if pix1^= pix2^ then
          inc(matchpix); //x-Koordinate nicht berücksichtigt
        inc(Pix1);
        inc(pix2);
        inc(compix);
      end;
  end;
  if compix = matchpix then
    Result := true;
end;
Du hast eine Untergrenze (posx,posy) aber warum keine Obergrenze?

Ein Optimierungsvorschlag für die vorige Routine wäre das hier:

Delphi-Quellcode:
function Bitmapcompare(pic1, pic2: Tbitmap; Posx,posy: Integer): Boolean;
var
  Pix1, Pix2 : PByte;
  y, k, x : Integer;
  bytes: Byte;
const
  PixelFormatBytes: Array[TPixelFormat] of Byte = ( 0, 0, 0, 1, 0, 2, 3, 4, 0 );
begin
  result:=false;
  bytes := PixelFormatBytes[pic1.PixelFormat];
  if bytes <> PixelFormatBytes[pic2.PixelFormat] then
    Exit;
  if (bytes = 0) then
    Exit; // PixelFormat wird nicht unterstützt ... kannst du dann gerne von mir aus umändern ...
  if (pic1.Width <> pic2.Width) or (pic1.Height <> pic2.Height) then
    Exit;
  for y := 0 to pic2.Height - 1 do
  begin
    Pix1 := pic1.Scanline[posy+y];
    Pix2 := pic2.Scanline[y];
    inc( Pix2, Posx*bytes );
    for x := 0 to pic2.Width - 1 do
      for k := 0 to bytes - 1 do
      begin
    //VERGLEICH
        if pix1^ <> pix2^ then
          Exit; // ungleich, verlasse deshalb routine. Result ist in diesem Falle = False ...
        inc(Pix1);
        inc(pix2);
      end;
  end;
  // wenn wir es bis hierher geschafft haben, dann sind die bilder von (posx, posy) aufwärts gleich
  Result := true;
end;
Wie wärs denn mit soetwas?
Delphi-Quellcode:
{ Rückgabe = Gleichheit in % }
function CompareBitmap( bmp1, bmp2: TScanlineBitmap; const posX, posY, Width, Height: Integer ): Byte;
var
  x, y,
  TotalPixels,
  MatchingPixels : Integer;
  Bytes : Byte;
const
  PixelFormatBytes: Array[TPixelFormat] of Byte = ( 0, 0, 0, 1, 0, 2, 3, 4, 0 );
begin
  Result := 0;
  if (bmp1.Width <> bmp2.Width) or (bmp1.Height <> bmp2.Height) then
    Exit;
  if (bmp1.PixelFormat <> bmp2.PixelFormat) then
    Exit;
  Bytes := PixelFormatBytes[bmp1.PixelFormat];
  TotalPixels := (Height-posY) * (Width-posX);
  MatchingPixels := 0;
  for y := posY to Height - 1 do
    for x := posX to Width - 1 do
      if bmp1.Pixel[x,y] = bmp2.Pixel[x,y] then
        inc( MatchingPixels );
  Result := Round(MatchingPixels * 100 / TotalPixels);
end;
MfG
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG
  Mit Zitat antworten Zitat