需要了解代码中[:: - 1]的用途是什么? [重复]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

我试图删除我得到结果的部分,但只是按相反的顺序。所以我的问题是,为什么我们使用该部分来拼接数组。

import numpy as np
import cv2

img = cv2.imread("E:/tmp/pis.jpg")
template = cv2.imread("E:/tmp/pi templates.jpg",0)

img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
w,h = template.shape[::-1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.6

loc = np.where(res>=threshold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img,pt,(pt[0]+w,pt[1]+h),(255,255,255),1)

cv2.imshow("img",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
python python-3.x list numpy slice
1个回答
-1
投票

如果这就是你的意思,那不是切片。 [::-1]用于反转列表

l = [1,2,3]
l = l[::-1]
print(l) # [3, 2, 1]
© www.soinside.com 2019 - 2024. All rights reserved.