Finding a coordinate of matched object from template matching
<p>I'm a newbie to computer vision and openCV, just for the heads up.
So I used this code from <a href="http://docs.opencv.org/3.1.0/d4/dc6/tutorial_py_template_matching.html" rel="nofollow">http://docs.opencv.org/3.1.0/d4/dc6/tutorial_py_template_matching.html</a></p>

<p>Below is the code:</p>

<pre><code>import cv2
import numpy as np

image = cv2.imread('new_test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
template = cv2.imread('new_template.jpg',0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.5
loc = np.where( res &gt;= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(image, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)

cv2.imshow('res.png',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code></pre>

<p>I modified this code, of course, to match with my own pictures.</p>

<p>So supposed I have 4 objects in 'new_test.jpg' that matches the template 'new_template.jpg'. The next step in my algorithm should be detecting the coordinate of each objects.</p>

<p>How do I do that ?</p>

<p>This piece of code:</p>

<pre><code>for pt in zip(*loc[::-1]):
    cv2.rectangle(image, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
</code></pre>

<p>draws red rectangle at every position where template probably is located. So at every step point <code>pt</code> (from <code>loc</code> list) contains coordinates of the left top corner of one of your objects (if it has been properly detected)</p>

python osx python-2.7 opencv opencv3.0