3
3
3Create2019-11-17 15:02:00
Python
3Create, 2019-11-17 15:02:00

How to detect rotated object (template matching)?

I am just learning.
This standard code detects the selected template in the picture.
But how to make detection if the picture is rotated and let's say black and white?
Does anyone have examples of implementation? Thank you.

import cv2
import numpy as np
img = cv2.imread("simpsons.jpg")
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread("barts_face.jpg", cv2.IMREAD_GRAYSCALE)
w, h = template.shape[::-1]
result = cv2.matchTemplate(gray_img, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(result >= 0.4)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 3)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2019-11-17
@dimonchik2013

https://medium.com/@deepanshut041/introduction-to-...

A
Andrey Dugin, 2019-11-17
@adugin

Learn to read documentation.
From there it follows that the function works like a fold - which means that you need to expand the template.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question