6

I need an algorithm written in any language to find an image inside of an image, including at different scales. Does anyone know a starting point to solving a problem like this?

For example:

I have an image of 800x600 and in that image is a yellow ball measuring 180 pixels in circumference. I need to be able to find this image with a search pattern of a yellow ball having a circumference of 15 pixels.

Thanks

Paul
  • 65
  • 1
  • 4
  • 2
    The circumference of the circle is actually pretty useless as a measurement in this context. – Anon. Jan 18 '11 at 03:17
  • Do you have any math training in this area? this is just some basic vector space transformations. wouldn't be possible to explain without the math. – J-16 SDiZ Jan 18 '11 at 03:18

4 Answers4

24

Here's an algorithm:

  • Split the image into RGB and take the blue channel. You will notice that areas that were yellow in the color image are now dark in the blue channel. This is because blue and yellow are complementary colors.
  • Invert the blue channel
  • Create a greyscale search pattern with a circle that's the same size as what's in the image (180 pixels in circumference). Make it a white circle on a black background.
  • Calculate the cross-correlation of the search pattern with the inverted blue channel.
  • The cross-correlation peak will correspond to the location of the ball.

Here's the algorithm in action:

RGB and R:

alt text alt text

G and B:

alt text alt text

Inverted B and pattern:

alt text alt text

Python + OpenCV code:

import cv
if __name__ == '__main__':
    image = cv.LoadImage('ball-b-inv.png')
    template = cv.LoadImage('ball-pattern-inv.png')

    image_size = cv.GetSize(image)
    template_size = cv.GetSize(template)
    result_size = [ s[0] - s[1] + 1 for s in zip(image_size, template_size) ]

    result = cv.CreateImage(result_size, cv.IPL_DEPTH_32F, 1)

    cv.MatchTemplate(image, template, result, cv.CV_TM_CCORR)

    min_val, max_val, min_loc, max_loc = cv.MinMaxLoc(result)

    print max_loc

Result:

misha@misha-desktop:~/Desktop$ python cross-correlation.py 
(72, 28)

This gives you the top-left co-ordinate of the first occurence of the pattern in the image. Add the radius of the circle to both x and y co-ordinates if you want to find the center of the circle.

Brigham
  • 14,395
  • 3
  • 38
  • 48
mpenkov
  • 21,621
  • 10
  • 84
  • 126
  • 1
    1000x upvotes to you sir, I was working on this for some hours (I'm lousy at C++) before I found your answer. =) – Ken Jan 15 '12 at 09:52
  • I used this method and it worked fine. But if searched item has optional contour in same color, this pattern not work. How did change the pattern for original iamge that contain circles (not filled) same color as a pattern around searched circle? Actually i have a rectangle, but it does not matter. – Alexander Fresh Sep 24 '15 at 07:38
2

a version of one of previous posts made with opencv 3 and python 3

import cv2
import sys

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(cv2.matchTemplate(cv2.imread(sys.argv[1]),cv2.imread(sys.argv[2]),cv2.TM_CCOEFF_NORMED))

print(max_loc)

save as file.py and run as:
python file.py image pattern

user3694243
  • 234
  • 3
  • 13
2

You should take a look at OpenCV, an open source computer vision library - this would be a good starting point. Specifically check out object detection and the cvMatchTemplate method.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
0

A simple starting point would be the Hough transform, if you want to find circles.

However there is a whole research area arount this subject called object detection and recognition. The state of the art has advanced significantly the past decade.

carlosdc
  • 12,022
  • 4
  • 45
  • 62