 |
| Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 14th, 2006, 05:10 AM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
image compare
hi everone;
I tyr to compare two images.This images come from camera (or webcam)captured.my question is how can i compare this images and i can say same or diffrent.please help me.
thanks
|
|

June 15th, 2006, 03:47 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
What you are talking about is [u]extremely</u> complex.
You practically have to teach the computer how to âsee.â
That's the reason that websites use pictures of numbers and letters to protect themselves from web robots. They put up a picture of a number with some background imagery and ask you to type what you see into a text box. Reading the numbers is easy for a human, near impossible for a computer.
You are describing something even more complicated. In the one case, the possibilities are limited to 26 letters and 10 digits. In your case the possibilities are practically without limit.
Brian
|
|

June 15th, 2006, 03:53 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Actually, this is pretty easy to accomplish. Since an image is nothing more than a stream og bytes, all you need to do is compare the bytes. When they're the same, the image is the same, otherwise, they are different.
I have a simple .NET C# implementation to compare an image in just a few lines of code. I haven't posted it because you asked for a VB6 solution, but let me know if you're still interested.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

June 16th, 2006, 06:19 AM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi Imar and Brian.Imar, yes i'm interesting other codes.please replay me.
thaks
|
|

June 16th, 2006, 06:36 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi selamiduvar,
The following method returns a unique hash for an image, like this: "ANJwS2GbcLPiSZaZSk5RPw=="
Since each different image returns a unique hash, you can simply compare two hashes and you know whether the images are the same:
public bool CompareImage(string image1, string image2)
{
return GetImageHash(image1) == GetImageHash(image2);
}
Hope this helps,
Imar
Code:
using System.Drawing;
using System.Drawing.Imaging;
using System.Security.Cryptography;
public static string GetImageHash(string theImage)
{
try
{
Image myImage = Image.FromFile(theImage);
MemoryStream stream = new MemoryStream();
myImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] bytes = stream.ToArray();
Byte[] HashVal = (new MD5CryptoServiceProvider()).ComputeHash(bytes);
return Convert.ToBase64String(HashVal);
}
catch (OutOfMemoryException)
{
throw new ArgumentException("Path does not contain a valid image");
}
catch (ArgumentException)
{
throw new ArgumentException("This is not a valid path");
}
catch (FileNotFoundException)
{
throw new ArgumentException("File not found");
}
catch (Exception)
{
throw;
}
}
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

June 16th, 2006, 06:42 AM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thank you Imar
|
|

June 16th, 2006, 02:48 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Imar,
If the pictures are absolutely identical (which will only be the case when there has been a file copy), then what you say is right. But even taking the exact same photo twice in a row, with the same camera, without moving it at all results in files that will show False through byte-by-byte comparison.
If the two items being compared are identical files, then any file comparison will do the trick. (I presumed that the topic was not merely trying to find a copy, or there would have been no cause for a post in the 1st place. Perhaps I presumed too much.)
|
|

June 16th, 2006, 03:28 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Brian,
Yes, you're right. Two pictures from the same camera will never be the same. Stuff like lightning and movement always influence the picture.
However, a simple file comparison may not work. I have used this code in an application where from different channels people upload the same picture. However, one department added meta data, while the others didn't. This meta data (IPTC and EXIF) is stored in the image as well, so you may see different file sizes and file contents, for files that contain the same image. The code I posted reads the image from the file and calculates a hash based purely on that image.
So, yes, this code doesn't answer the question "Am I on both pictures celebrating and drinking beer because the Dutch made it to the next round of the World Championship", but it does give you the answer as to whether two files contain the exact same image.
Hopefully, this is what selamiduvar asked for.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

July 5th, 2006, 08:00 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
there are however simple ways to compare images when they are taken from the same camera that is not moving (like a surveillance camera or a web camera).
You have to define a "difference" function, that is a function that computes the difference between two pixels. On example is the euclidean distance compute as
wR*(r1-r2)^2 + wB*(b1-b2)^2 + wG*(g1-g2)^2
where r1,b2,g1 are the components of the pixels in one camera, and r2,b2,g2 the components of the pixel in the same position in the other camera and wR,wB,wG three weights (they can be 1)
You compare the image pixel by pixel, and if the difference is bigger than a specified threshold you can trigger a pixel count. The image are the same if the pixel count is less than another thershold (you have to experiment with the values of the thresholds)
You can also "build" an image using the pixel difference, so you can have an idea where the images are different.
If the light can change a good deal, a possible solution is to subtract to the pixel values the "average" brightness of the image before comparing them.
|
|

May 20th, 2008, 01:52 PM
|
|
Registered User
|
|
Join Date: May 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
|
quote:there are however simple ways to compare images when they are taken from the same camera that is not moving (like a surveillance camera or a web camera).
|
Quote:
You have to define a "difference" function, that is a function that computes the difference between two pixels. On example is the euclidean distance compute as
wR*(r1-r2)^2 + wB*(b1-b2)^2 + wG*(g1-g2)^2
where r1,b2,g1 are the components of the pixels in one camera, and r2,b2,g2 the components of the pixel in the same position in the other camera and wR,wB,wG three weights (they can be 1)
You compare the image pixel by pixel, and if the difference is bigger than a specified threshold you can trigger a pixel count. The image are the same if the pixel count is less than another thershold (you have to experiment with the values of the thresholds)
You can also "build" an image using the pixel difference, so you can have an idea where the images are different.
If the light can change a good deal, a possible solution is to subtract to the pixel values the "average" brightness of the image before comparing them.
|
Dear marcostraf:
I have been looking for the solution that is described in your above recommendation. I am a C# programmer with no knowledge of graphics or image manipulation / comparison libraries.
We would like to be able to compare 2 images and return 1 of the following results:
A. Images are IDENTICAL
B. Images are NOT IDENTICAL
C. Images are MORE THAN 75% similar (or whatever the % might be). This number can be dictated by the user as a threshold.
I like your idea of somehow building the image with the differences highlighted, since I can predict it now that is a piece of functionality that will be required.
Ideally there are algorithms out there that can be followed and implemented. If not, could you please point me to learning resources that I might follow and build a solution?
Many thanks for your time,
S
|
|
 |