-1

(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

  • What do you get from `print(type(locc))`? As posted, it looks like a tuple but I don't know if that is a quirk of how it's displayed. – roganjosh Jan 11 '19 at 23:29
  • If it is a tuple, `result = [item[0] for item in locc]` – roganjosh Jan 11 '19 at 23:31
  • If I use locc[0] it will return this: [367, 368, 368, 368, 369], but I only want the 367 of this part. – Victor Ferreira França Jan 12 '19 at 00:07
  • a) that doesn't address my first question and b) I guessed your issue and answered correctly in my second comment in that case. Rather than re-state the problem, could you perhaps read and take on-board the feedback you have been given? – roganjosh Jan 12 '19 at 00:08

2 Answers2

0

If you have a one-dimensional Numpy array and just want to grab part of it, then you can use the ":" operator the same way you can with a Python list.

>>> import numpy as np
>>> a = np.array((1,2,3,4))
>>> a
array([1, 2, 3, 4])
>>> a[0:2]
array([1, 2])
>>> a[1:2]
array([2])
>>> print(a[:3])
[1 2 3]
>>> b = a[0:3]
>>> print(b)
[1 2 3]
>>> type(b)
<type 'numpy.ndarray'>
>>> print(a[0])
1
>>> type(a[0])
<type 'numpy.int32'>

EDIT: If locc[0] gives you back [367, 368, 368, 368, 369] and you only want 367, then try loc[0][0]. If you want [367] so you can still treat the data as a list, try [loc[0][0]].

Bill M.
  • 1,388
  • 1
  • 8
  • 16
0

You can get the first element of the array with locc[0].

(I am assuming that the two array objects you've posted are from two cycles of the while loop, so each iteration of locc is a 1D array.)

samueljsb
  • 41
  • 3