BulletRain
BulletRain

Reputation: 92

Find image contain another image

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 ?

Upvotes: 1

Views: 921

Answers (3)

Mace
Mace

Reputation: 1410

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

Upvotes: 0

Yunus Temurlenk
Yunus Temurlenk

Reputation: 4377

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!!!

Upvotes: 1

nnovember
nnovember

Reputation: 26

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.

Upvotes: 0

Related Questions