如何只获取数组中的一些元素
How get just some elements in an array
(抱歉我的英语不好)
我正在使用 OpenCv 和其他小型库在 python 上编写程序,基本上我的程序需要根据模板在屏幕上查找图像。我为此使用了模板匹配。该程序识别屏幕上的模板并将左右像素作为数组发送到输出。但是,有些数字我不想要,我只想获取数组的前 3 个数字。
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)
我屏幕上对象的输出是:(array([367, 368, 368, 368, 369], dtype=int32), array([490, 489, 490, 491, 490], dtype =int32)).
但我只想要第一个数组的“367”和第二个数组的“490”
如果您有一个一维 Numpy 数组并且只想获取其中的一部分,那么您可以使用“:”运算符,就像使用 Python列表。
>>> 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'>
编辑: 如果 locc[0]
返回 [367, 368, 368, 368, 369] 而你只想要 367,那么试试 loc[0][0]
.如果你想要 [367] 这样你仍然可以将数据视为列表,请尝试 [loc[0][0]]
.
可以用locc[0]
获取数组的第一个元素。
(我假设您发布的两个数组对象来自 while
循环的两个循环,因此 locc
的每次迭代都是一维数组。)
(抱歉我的英语不好) 我正在使用 OpenCv 和其他小型库在 python 上编写程序,基本上我的程序需要根据模板在屏幕上查找图像。我为此使用了模板匹配。该程序识别屏幕上的模板并将左右像素作为数组发送到输出。但是,有些数字我不想要,我只想获取数组的前 3 个数字。
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)
我屏幕上对象的输出是:(array([367, 368, 368, 368, 369], dtype=int32), array([490, 489, 490, 491, 490], dtype =int32)).
但我只想要第一个数组的“367”和第二个数组的“490”
如果您有一个一维 Numpy 数组并且只想获取其中的一部分,那么您可以使用“:”运算符,就像使用 Python列表。
>>> 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'>
编辑: 如果 locc[0]
返回 [367, 368, 368, 368, 369] 而你只想要 367,那么试试 loc[0][0]
.如果你想要 [367] 这样你仍然可以将数据视为列表,请尝试 [loc[0][0]]
.
可以用locc[0]
获取数组的第一个元素。
(我假设您发布的两个数组对象来自 while
循环的两个循环,因此 locc
的每次迭代都是一维数组。)