How To Compare 2 Images



Here i show a function to compare 2 images. If the images are different then the result will be True, the function as follow:


function checkIsDiffrentImage(img1, img2: TBitmap): boolean;
var
  p1, p2  : PByte;
  x, y, i : integer;
  checkRes: boolean;
begin
  if not(img1.PixelFormat = img2.PixelFormat) then
  begin
    result := true;
    exit;
  end;
  checkRes := false;

  y:=0;
  while not(checkRes) and (y < img1.Height) do
  begin
    p1 := img1.Scanline[y];
    p2 := img2.Scanline[y];
    x := 0;
    while not(checkRes) and (x < img1.Width) do
    begin
      if Integer(p1^) <> Integer(p2^) then
         checkRes := true
      else begin
        Inc(p1);
        Inc(p2);
      end;
     inc(x);
    end;
    inc(y);
  end;
  result := checkRes;
end;



Below is a sample code, how to use the function.
1.       Prepare 2 Bitmap Image



2.       Put Timage component into Form, then load the images

3.       Put Tbutton component into Form then set OnClick Event
procedure TForm1.Button1Click(Sender: TObject);
begin
 if checkIsDiffrentImage(Image1.Picture.Bitmap,image2.Picture.Bitmap) then
   showMessage('The images are not same')
 else
   showMessage('The images are same');
end;

let's test the function by running the application 



Related product you might see:

Share this product :
 
Delphi Programming Tutorial © Mang Yadi Site 2015