Data: 19/01/2011 16:58:59
De: Lindsay (loslds7@hotmail.com)
IP: 187.114.124.164
Assunto: Re: Comparar imagem
De: Lindsay (loslds7@hotmail.com)
IP: 187.114.124.164
Assunto: Re: Comparar imagem
|
function CompararImgs(Image1, Image2: TImage): Boolean; var b1, b2: TBitmap; c1, c2: PByte; x, y, i, different, BytesPerPixel: Integer; begin b1 := Image1.Picture.Bitmap; b2 := Image2.Picture.Bitmap; Assert(b1.PixelFormat = b2.PixelFormat); different := 0; case b1.PixelFormat of pf1bit, pf4bit, pf8bit: BytesPerPixel := 1; pf15bit, pf16bit: BytesPerPixel := 2; pf24bit: BytesPerPixel := 3; pf32bit: BytesPerPixel := 4; end; for y := 0 to b1.Height - 1 do begin c1 := b1.Scanline[y]; c2 := b2.Scanline[y]; for x := 0 to b1.Width - 1 do for i := 0 to BytesPerPixel - 1 do begin Inc(different, Integer(c1^ <> c2^)); Inc(c1); Inc(c2); end; end; Result := (different <> 0); end; ///////////////////////////// procedure TForm1.Button1Click(Sender: TObject); var b1, b2: TBitmap; c1, c2: PByte; x, y, i, different: Integer; // Counter for different pixels begin b1 := Image1.Picture.Bitmap; b2 := Image2.Picture.Bitmap; Assert(b1.PixelFormat = b2.PixelFormat); // they have to be equal different := 0; for y := 0 to b1.Height - 1 do begin c1 := b1.Scanline[y]; c2 := b2.Scanline[y]; for x := 0 to b1.Width - 1 do for i := 0 to BytesPerPixel - 1 do // 1, to 4, dep. on pixelformat begin Inc(different, Integer(c1^ <> c2^)); Inc(c1); Inc(c2); end; end; end; ////////////////////////////////////// ================ //esta rtorna em percentual a diferença ================ function BitmapDifference(BmpA, BmpB: TBitmap): double; var x, y: integer; P, Q: PByte; Diff: int64; begin if not assigned(BmpA) or not assigned(BmpB) or (BmpA.PixelFormat <> pf24bit) or (BmpB.PixelFormat <> pf24bit) or (BmpA.Width <> BmpB.Width) or (BmpA.Height <> BmpB.Height) or (BmpA.Width * BmpA.Height = 0) then raise Exception.Create('Cannot compare bitmaps'); Diff := 0; for y := 0 to BmpA.Height - 1 do begin P := BmpA.Scanline[y]; Q := BmpB.Scanline[y]; for x := 0 to BmpA.Width - 1 do begin Diff := Diff + Sqr(P^ - Q^); inc(P); inc(Q); Diff := Diff + Sqr(P^ - Q^); inc(P); inc(Q); Diff := Diff + Sqr(P^ - Q^); inc(P); inc(Q); end; end; Result := Sqrt(Diff / (BmpA.Width * BmpA.Height)); end; ================ |



