• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
phyitbos

Speedy Image Likeness Comparison

8 posts in this topic

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:

[img]http://dzaletanski.com/projects/imageevolution/images/sample.JPG[/img]

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 ;)
0

Share this post


Link to post
Share on other sites
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?
1

Share this post


Link to post
Share on other sites
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 [url="http://en.wikipedia.org/wiki/Mean_squared_error"]MSE[/url] as used by [url="http://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio"]PSNR[/url] 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 Edited by L. Spiro
1

Share this post


Link to post
Share on other sites
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#):
[CODE]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);
}[/CODE]

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. Edited by phyitbos
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.

[url="http://stackoverflow.com/questions/1563038/fast-work-with-bitmaps-in-c-sharp"]http://stackoverflow.com/questions/1563038/fast-work-with-bitmaps-in-c-sharp[/url]

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)?
1

Share this post


Link to post
Share on other sites
[quote name='phyitbos' timestamp='1349943981' post='4989022']
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)?
[/quote]
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.
1

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1349961454' post='4989088']
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.
[/quote]
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 [b]Math.Sqrt(dist)[/b] with [b]dist /= nRows * nCols[/b]), 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 [b]pixel *= 1.0f / 255.0f[/b] and square the result of that. That value is what should be accumulated for each channel.


L. Spiro Edited by L. Spiro
1

Share this post


Link to post
Share on other sites
Thanks to everyone (especially Spiro for the continued support!) your answers have been very helpful! I've learned some very important things here [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0