0

I dont always have square/rectangle images, sometimes I should match round images as well. Below are 2 images for example. Ball is the template image and the second is Source where the template should be searched. I can make the background of template transparent, but this gives error, making it white decreases the match score, because as you see on the Source image there is no white around the ball. These are just 2 examples pictures. Do you have suggestions/solutions?

Template image to be searched

Source image

Anarkie
  • 657
  • 3
  • 19
  • 46

3 Answers3

2

I think you can also use histogram backprojection for this. There you can use an arbitrary shape mask as well. Convolve the mask with the backprojected image and you'll detect a peak in the region where the object occurs in the image as shown in the images (color mapped and scaled) below.

backprojected:

enter image description here

convolved:

enter image description here

EDIT:

This is based on this paper. I was experimenting it and was hoping to post in a blog. This is in C++.

// model histogram: this is the football template
calcHist(&model32fc3, 1, (const int*)channels, modelMask, histModel, 3, (const int*)histSize, (const float**)ranges, true, false);
// image histogram
calcHist(&image32fc3, 1, (const int*)channels, Mat(), histImage, 3, (const int*)histSize, (const float**)ranges, true, false);
// ratio histogram
divide(histModel, histImage, histRatio);
cv::min(histRatio, 1.0, histRatio);
// backproject ratio histogram
calcBackProject(&image32fc3, 1, (const int*)channels, histRatio, backprj, (const float**)ranges);
// convolve the kernel with the backprojected image
filter2D(backprj, conv, CV_32F, modelMask);
dhanushka
  • 10,492
  • 2
  • 37
  • 47
  • Could you give example with code please? or a link to the tutorial where this is made? – Anarkie Nov 07 '14 at 13:26
  • Added the code. I'm still experimenting it. I'll update the code or provide a link if I get a chance to publish this in my future blog. – dhanushka Nov 07 '14 at 14:11
  • I know C++ but working in Java right not, could you post it in Java? Or I will try it myself after you are fully finished. – Anarkie Nov 07 '14 at 14:15
  • Sorry, I haven't used OpenCV with Java. I said I was experimenting because I haven't tested it extensively, but it worked quite well for images that were not affected by varying illumination. And I forgot to mention that I was using RBG color space. – dhanushka Nov 07 '14 at 15:56
1

That's OK, you can still use matchTemplate() and get excellent results:

enter image description here

You can find a decent tutorial on OpenCV's documentation. By the way, this is the output of the demo shared there.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Template matching finds the best result, it will mark a square even if the template is not in source. If the searched image is really in the source then the score is like 0.9987... but in this case with whites the score will be 0.7... I will make lets say 1000 comparisons for different images, some will have green some will have blue, some gray background. Which will always affect the matching score. Im setting a threshold according to the score image found or not found for my case and having random thresholds will be a problem. I know it will be always above 0.6... but still... – Anarkie Nov 07 '14 at 13:24
0

If you know the pixels belonging to the template, you can write your matcher

sum of absolute differences trial (pseudo code)

Mat I, T // image and template
vector<Point> template_pixels
Rect sliding_window
vector<double> match_rates

for all rows in image
update sliding_window
    for all cols in image
    update sliding_window
    Mat W = I(sliding_window)
    sum = 0
         for all rows in template
              for all cols in template
              if(template_pixels contains pixel i)
                   sum += abs(W(i) - T(i))
              end for
         end for
    match_rates.pushback(sum)
    end for
end for

minMaxLoc(match_rates)

and optimize it with multithreading on image rows

baci
  • 2,528
  • 15
  • 28
  • i dont know python if this is python i might try it :) i wrote this in berky. it is very simple yet powerful language - and awesome, as you figured it out already, - just hard to get used to.. 8) – baci Nov 07 '14 at 01:00
  • You are right, Python is a bit different. If this was Java code I would +1 for sure. – karlphillip Nov 07 '14 at 01:01