LAB: Image Detection, Part 1
Posted by Monty on May 4th, 2009Every now and then, I get a crazy idea, to try something rather hard in .net – maybe hard isnt the right word for it, but something that hasn’t really been attempted before, or if it has, nothing public about it. So the idea I have is for some form of image detection, to say that Image X is x % similar to Image Y. I prefer to use the terms Source and Target, but it doesn’t really matter.
The source and the target images are here. That’s a photo I took a few weeks ago, if you do decide to steal it, please put a message on there that points back to me
Anyway, the target image is 14×15 pixels smaller – that’s a whopping 210 pixels different! It shouldn’t be too hard to match up the two images, or should it?
Histograms
My first attempt was with Histograms. I grabbed some open source (unsafe!) code that generates an array of int[] and lists the histogram values, so I shoved both images through that, and got it to output the Source’s histogram value, the Target’s histogram value, the difference between them both, and the percentage of how similar it is, like so: (The first number is the key# of the int in the array, just because)
![]()
Right at the bottom, I had an average of all the percentages, to see how “similar” the image is. I was expecting the percentage to be fairly high, since there is only a few hundred pixels difference, and I didn’t change any of the levels or colours when I cut the images out of each other, but it told me there was a 87.7% similarity! That was very shockingly low. If you see something wrong with my maths from the code below, please let me know:
public HistogramCompareResults(int key, int source, int target)
{
this.key = key;
this.source = source;
this.target = target;
difference = source – target;
if (difference < 0)
{
difference = difference*-1;
}
if (source != 0 && target !=0)
{
if (source > target)
{
percentage = (double)target / (double)source;
}
else
{
percentage = (double)source / (double)target;
}
percentage = percentage*100;
}
}
Image Processing
My next port of call was basically image processing, like what I did with my ANPR project that I created – basically filtering stuff out and building a “thumbprint” of the image, that hopefully will withstand being resized and stuff like that. Using image filters, flattening images and looking for large “blobs” of images, sofar I have come up with this:
![]()
That is the current “thumbprint” for the red channel, on the source image.








Recent Comments