Jump to content

  • Log In with Google      Sign In   
  • Create Account


Speedy Image Likeness Comparison


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 phyitbos   Members   -  Reputation: 128

Like
0Likes
Like

Posted 10 October 2012 - 11:53 AM

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:

Posted Image

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

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7745

Like
1Likes
Like

Posted 10 October 2012 - 12:26 PM

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?

#3 L. Spiro   Crossbones+   -  Reputation: 11938

Like
1Likes
Like

Posted 10 October 2012 - 06:59 PM

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

Edited by L. Spiro, 10 October 2012 - 07:04 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#4 phyitbos   Members   -  Reputation: 128

Like
0Likes
Like

Posted 11 October 2012 - 12:35 AM

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.

Edited by phyitbos, 11 October 2012 - 12:36 AM.


#5 Olof Hedman   Crossbones+   -  Reputation: 2618

Like
1Likes
Like

Posted 11 October 2012 - 01:33 AM

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.

#6 phyitbos   Members   -  Reputation: 128

Like
1Likes
Like

Posted 11 October 2012 - 02:26 AM

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

#7 Bacterius   Crossbones+   -  Reputation: 7975

Like
1Likes
Like

Posted 11 October 2012 - 07:17 AM

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.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 L. Spiro   Crossbones+   -  Reputation: 11938

Like
1Likes
Like

Posted 11 October 2012 - 09:25 AM

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

Edited by L. Spiro, 11 October 2012 - 09:27 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#9 phyitbos   Members   -  Reputation: 128

Like
0Likes
Like

Posted 11 October 2012 - 11:22 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS