1

I have try using matchTemplate() function and minMaxLoc() function to find position image in another image but it not working because the container image it not same angle with the image I find.

This is I have done with origin image and it work fine with matchTemplate() function and minMaxLoc(). But if I rotate image. Nothing is recognize.

I try to find coin in the image

This is code I using to recognize coin:

img_rgb = cv.imread('mario.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('mario_coin.png',0)
w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv.imwrite('res.png',img_rgb)

My image rotate 30 degree:

enter image description here

Can anyone help me with this ?

3 Answers 3

1

It seems you are trying this example. There are some disadvantages of matchTemplate(). One of them is rotation problem. Such these dynamic features you can use other techniques like SIFT or SURF.

But if you really want to continue with template matching,you should effort. You can generate all possible rotations of the source image and you can try template matching for all. This is a brute way but can work.

You can also check this one and this one

Good Luck!!!

1
  • Still get error from example but it look promise. I copy example but get this error when run: (-5:Bad argument) The input arrays should be 2D or 3D point sets in function 'findHomography'
    – BulletRain
    Commented Apr 28, 2020 at 1:39
0

Because of the rotation, the matching value would be highly decreased. Use lower threshold for cv.matchTemplate.

or if you know the rotation degree before, rotate the template(mario_coin.png) and do the template matching.

0
0

Try it a different way.

You could calculate for example 360 templates, each template a coin rotated 1 degree farther. Do the matching while iterating through the 360 templates and find the best match.

The whole process, the rotation of the coin template and the matching, would take 360 times more time of course but I think you should find the position(s).

1
  • I think it take very long times to match 1 coin. But thanks for your idea
    – BulletRain
    Commented Apr 28, 2020 at 1:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.