(Sorry my bad english) I'm making an program write on python using OpenCv and another minor libraries, basically my program need to find an image on my screen based on an Template. I used the Template Matching to this. The program identifies the template on screen and send the right left pixel as an array to the output. But, there some numbers that i dont want, i just want grab the first 3 numbers of the array.
import cv2
import numpy as np
from matplotlib import pyplot as plt
import pyscreenshot as ImageGrab
while True:
#Template Matching of the block
img_rgb = ImageGrab.grab(bbox=(448, 168, 1471, 935))
img_rgb = np.asarray(img_rgb)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread("Templates/rock.jpg",0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.9
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
#Template Matching of the Character
templatec = cv2.imread("Templates/char.jpg",0)
wc, hc = templatec.shape[::-1]
resc = cv2.matchTemplate(img_gray,templatec,cv2.TM_CCOEFF_NORMED)
thresholdc = 0.6
locc = np.where( resc >= thresholdc)
for pt in zip(*locc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,0), 2)
cv2.imwrite('res.png',img_rgb)
print(locc)
The output with the object in my screen is: (array([367, 368, 368, 368, 369], dtype=int32), array([490, 489, 490, 491, 490], dtype=int32)).
But i just want the "367" of the first array, and the "490" of the second