Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare two pictures

Status
Not open for further replies.

Kudel

Programmer
Dec 3, 2002
36
NO
Hi!

I have two pictures of eaqual size. I need to find out if it is the same picture. How can i do this?

-Kudel:)
 
There's probably a simpler method, but you could load the files into memory and compare them.


function CompareImages(Filename1,Filename2 : string): boolean;
var
S1,S2 : TMemoryStream;
P1,P2 : Pointer;
Match : boolean;
begin
S1 := TMemoryStream.Create;
S2 := TMemoryStream.Create;
S1.LoadFromFile(Filename1);
S2.LoadFromFile(Filename2);
GetMem(P1,S1.Size);
GetMem(P2,S2.Size);
S1.ReadBuffer(P1^,S1.Size);
S2.ReadBuffer(P2^,S2.Size);
if (S1.Size = S2.Size) and CompareMem(P1,P2,S1.Size) then
Match := true
else
Match := false;
FreeMem(P1,S1.Size);
FreeMem(P2,S2.Size);
S1.Free;
S2.Free;
Result := Match;
end;


Cheers,
Jack
 
I would not rely on comparisons of the files like that--it will catch some but not all duplicates. Sometimes you'll find a file with a modified header but exactly the same graphics. It's a good first test but not the whole answer.

I've done it by loading both pictures into images and then looping through with scanline comparing them.
 
Yes Loren you are correct, it wouldn't be good to rely on the CRC itself, but you could first get the CRC, if they match you then continue on using the scanline routine, but if the CRCs are different then you know surley the images are different.

Here is what Earl F. Glynn suggests , he really knows this subject very well and points out the links to his site.

 
Hi

JackM2's solution works just fine for me, but thanks to all three of you.

-Kudel:)
 
If the CRC's match it's presumably identical. Only if they don't match do you use the scanline routine. I'm getting no response from that link so I can't comment on what it says.

I have found images that are pixel-by-pixel identical, but the file sizes and CRC's were different.
 
Sorry for that here is the contents of the link,

Are you using "Pixels" or "Scanline" for your comparisons?
Scanline will be much faster.

Look at the various "Comparison" links on this page:

You could use this SysUtils routine to compare two scanlines a bit faster.
function CompareMem(P1, P2: Pointer; Length: Integer): Boolean;

The addresses of scanlines can be increasing or decreasing so you usually can't use CompareMem for
the whole image. See "Copying Raw Data to Scanline Area" in the "Optimization" section of the
Scanline Tech Note:
Also in that technote are some examples of accessing the Scanline data using assembly language.

If you were storing images for later comparison, I think I'd compute and store the CRC-32 of each
image. If two images are the same size and PixelFormat, and have the same CRC-32, there's a very
small chance (like one in 4 billion) that they are really different. If the CRCs are different,
then the images are definitely different. Comparing two CRCs is simply the comparison of two
integers and would be very fast (once the CRC is computed).

--
efg -- Earl F. Glynn, Overland Park, KS USA
efg's Computer Lab Mirror:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top