Speedy Image Likeness Comparison

Started by
7 comments, last by phyitbos 11 years, 6 months ago
Hello all,

I am writing a program that uses a genetic algorithm to 'evolve' a randomly generated image in to a template image that I provide. Here is what it looks like:

sample.JPG

Every generation it creates (by default) 150 copies with random mutations (ellipse color/shape), and then compares them all to the template picture to see which is most similar, and that one moves on to the next generation. The current method I use to compare them is a pixel by pixel Euclidean Distance measurement. This takes about 8-9 seconds to evaluate the 150 children. Which might not seem like much, but to get a decently evolved image it can take at least 40,000 generations (about 4 days at given speed). This is too slow to test the results of different mutation rate / children / etc combinations.

What faster methods of comparing the two images are there? Or what other ideas do you have to make this more efficient?
My only thought so far is to reduce the dimensions of the images but that takes the fun out of creating a high quality replica ;)
Advertisement
Sounds like you have either implemented the distance calculation incorrectly or you have a really slow computer. A very naive test in Matlab at full 80-bit floating point precision and 500x500 images (just making some estimate of the size from your screen shot) images takes roughly a millisecond per image pair, and that time includes a few memory allocations for temporary buffers as well. Can you show how you're calculating the distance?
I agree that you have made some kind of critical performance error in your comparison function, especially if your images are just black-and-white.

If you support color images, you also have a logical flaw.
I can tell you from personal experience that you can’t work with simple Euclidean distances between pixels and expect anything near accurate of a result. I tried it in the early stages of my DXT utility and the results were shameful.
If you are working with color images, you need to use MSE as used by PSNR followed by weighted per-channel sums.

For the record, while MSE is trivially more complicated than a simple Euclidean compare, you should still be able to handle around (estimating) 500 512 × 512 images per second on an average computer today. So you definitely have an implementational error on your end.


Make sure you are not converting the pixels every time you compare them, for example to and from floating-point etc.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

I was going to move in to making it work for color images eventually, but I figured to start with greyscale cause it should be faster. Thanks for the tip about color images though.

Kinda expected it to go faster myself, so I wouldn't be surprised if something's wrong. Here's my function for it (C#):
private double GetEuclideanDistance(Image img1, Image img2)
{
int dist = 0.0;
try
{
Bitmap a = new Bitmap(img1);
Bitmap b = new Bitmap(img2);
if (a.Width != b.Width || a.Height != b.Height)
{
System.Windows.Forms.MessageBox.Show("Target comparable image does not have the same dimensions. Cannot get Euclidean Distance.");
return 0;
}
int nRows = a.Height;
int nCols = a.Width;
for (int row = 0; row < nRows; row++)
{
for (int col = 0; col < nCols; col++)
{
int d = a.GetPixel(col, row).ToArgb() - b.GetPixel(col, row).ToArgb();
dist += (d * d);
}
}
}
catch (Exception ex)
{
IEvo.ReportException(ex, "GetEuclideanDistance");
}
return Math.Sqrt(dist);
}


My timings show that it takes around 400ms to complete every time. Only about 15 ms for it to copy the images in to Bitmaps.
GetPixel is probably very slow. Individually locking the image for each pixel.
If this was C++, I'd tell you to lock the whole image, and process it with a pointer for processing times close to that of a copy.
Conversion to argb should also be unneccessary for grayscale, and a bit expensive. (compared to just add's to step the pointer, and a sub to compare raw pixels directly)
You probably can do something like that in C# too, hopefully someone else can tell you how.
Wow, yes apparently GetPixel is incredibly slow. I don't understand the idea behind image locking, but I found this post that explains how to get access to the bitmaps byte array.

http://stackoverflow.com/questions/1563038/fast-work-with-bitmaps-in-c-sharp

That cut the algorithms time down to 11 ms using the Marshal'ing method. To subtract the RGB values from each other just (aR+aG+aB)-(bR+bG+bB)?

That cut the algorithms time down to 11 ms using the Marshal'ing method. To subtract the RGB values from each other just (aR+aG+aB)-(bR+bG+bB)?

Wouldn't it be more meaningful to compare each channel individually and merge results at the end (possibly based on perceptual intensities of red blue and green)? Blue looks nothing like red yet they'd get a difference of zero.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


Wouldn't it be more meaningful to compare each channel individually and merge results at the end (possibly based on perceptual intensities of red blue and green)? Blue looks nothing like red yet they'd get a difference of zero.

Yes, it would.
When performing an image compare, you should accumulate the squares of all the points for each channel separately, then normalize them separately (replace Math.Sqrt(dist) with dist /= nRows * nCols), and then combine them using weights.
Also the squares should be within a range of 0-1, meaning if you read pixels from 0 to 255, you should do pixel *= 1.0f / 255.0f and square the result of that. That value is what should be accumulated for each channel.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks to everyone (especially Spiro for the continued support!) your answers have been very helpful! I've learned some very important things here happy.png

This topic is closed to new replies.

Advertisement