【问题标题】:Need to understand what is the use of [::-1] in the code? [duplicate]需要了解代码中 [::-1] 的用途是什么? [复制]
【发布时间】:2019-09-03 22:13:08
【问题描述】:

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

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] 用于反转列表

l = [1,2,3]
l = l[::-1]
print(l) # [3, 2, 1]

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2018-11-29
    • 2015-06-30
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2011-04-15
    • 1970-01-01
    相关资源
    最近更新 更多