4

Anyway to make this thing go faster ? coz right now it's like 6 seconds on the sourceImage the size of 1024x768 and template 50x50 around. This is using AForge, if anyone knows other faster and rather simple ways please submit. The task i'm trying to do is to find a smaller image within a screenshot. And preferably fast my limit is 1 second. The image i'm looking for is a red rectangle simple image and the screenshot is more complex.

System.Drawing.Bitmap sourceImage = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\1.jpg");
System.Drawing.Bitmap template = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\2.jpg");
// create template matching algorithm's instance
// (set similarity threshold to 92.5%)

ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.921f);
// find all matchings with specified above similarity

TemplateMatch[] matchings = tm.ProcessImage(sourceImage, template);
// highlight found matchings

BitmapData data = sourceImage.LockBits(
    new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
    ImageLockMode.ReadWrite, sourceImage.PixelFormat);
foreach (TemplateMatch m in matchings)
{

        Drawing.Rectangle(data, m.Rectangle, Color.White);

    MessageBox.Show(m.Rectangle.Location.ToString());
    // do something else with matching
}
sourceImage.UnlockBits(data);
Mandah Mr.
  • 355
  • 4
  • 10
  • picture example no can do : it says that i need reputation above 10 to do it. I've got only 4 for now – Mandah Mr. Feb 25 '12 at 12:21
  • I have upvoted both of your questions, so now you have 10+ rep. Can you post the pictures now? – SynerCoder Feb 25 '12 at 12:31
  • There's a white rectangle and the red one in the middle of it is what i'm looking for – Mandah Mr. Feb 25 '12 at 12:39
  • @SynerCoder, thanks Syner for proping up my rep – Mandah Mr. Feb 25 '12 at 12:40
  • Also please do the thingi that Syner did ppl so this question would stay relevant, there are similar questions like these but they are all unanswered. – Mandah Mr. Feb 25 '12 at 12:43
  • If you havent read this in my thread: I simply used 2 other features of the object. Regarding your problem: simply do a color filter and before matching prove, if in the matching area the color meets the requirements (if your object is filled red, simply try, if the middle-point meets "red"). my second feature was the depth via kinect. only if both features meet the requirements, let my slow algorithm run over these positions (not over any position as i posted it). Interesting links for you: codeproject.com/Articles/9727/Image-Processing-Lab-in-C and aforgenet.com/articles/shape_checker – nico Mar 07 '12 at 21:58
  • @nico, hey thanks, 1st link is working but it needs a black background as a requirement, 2nd link seems dead can't access it. But i decided to simply the task by not searching small icon image but to search for the color RED on the source image. Any ideas from AForge? PixelColor() method seems to be slow as well to scan printSCREEN. – Mandah Mr. Mar 09 '12 at 09:46
  • I dont know, if it is faster, but I am using the folowing: System.Drawing.Color color=bitmap.GetPixel(x,y); //extract color-values from the pixels byte[] bytes = BitConverter.GetBytes(color.ToArgb()); byte aVal = bytes[0]; byte rVal = bytes[1]; byte gVal = bytes[2]; byte bVal = bytes[3]; int aInt = BitConverter.ToInt32(bytes, 0); int rInt = BitConverter.ToInt32(bytes, 1); int gInt = BitConverter.ToInt32(bytes, 2); int bInt = BitConverter.ToInt32(bytes, 3); The conversion at the end is obviously not mandatory, you could also AND the red byte with a mask. – nico Mar 09 '12 at 20:10
  • btw, both links are working here. the second one might even be more interesting for you. – nico Mar 09 '12 at 20:14

1 Answers1

2

http://opencv.willowgarage.com/wiki/FastMatchTemplate - here you can find interesting idea for speeding up the template matching using two steps, first try to match downsampled images and when found match the original ones with smaller search region.

Also there is opencv implementation of template matching in matchTemplate function. This function is ported to GPU which can get significant speed up.

See the following

http://opencv.willowgarage.com/documentation/cpp/object_detection.html - matchTemplate function. http://opencv.willowgarage.com/wiki/OpenCV_GPU - about OpenCV functionality ported to GPU.

Michael Kupchick
  • 433
  • 2
  • 10
  • Gpu may have a problem on a server. Fe hosting servers and nearly no vps have gpu access ;( Sadly. – TomTom Feb 27 '12 at 10:16
  • 1
    You are right, anyway the trick with downsampling from the first link can make sufficient speed up without GPU implementation – Michael Kupchick Feb 27 '12 at 11:15
  • @MichaelKupchick Wow ......... this is heavy stuff.... i'ma need to get more ideas on images and all Thank you appreciate it. I dont know if it works for now but let me check this and if it matches i'll vote your answer up. Hope not to take too long. – Mandah Mr. Feb 27 '12 at 13:18
  • @MichaelKupchick sorry for taking this long, i don't if it's right but as i understand the idea is to break up the source image into smaller ones to improve the search time is that right ?? – Mandah Mr. Mar 09 '12 at 09:48
  • No, the idea is to downsample the original image and the template (make it smaller but with the same content), then if you find smaller template you can calculate the area of the template at the original image and perform fine tuning search – Michael Kupchick Mar 10 '12 at 00:48