如何删除嵌套的 for 循环并使用 numpy 数组 insead

How to remove nested for loops and use numpy arrays insead

我有一个包含 580 帧的视频。我需要能够从视频中检测到绿色并创建一个遮罩,以便在发现绿色的地方放置零值,其余的应该是 255。我已经将视频转换为 HSV 格式并使用嵌套 for 循环,它大约需要一个小时才能完成我想知道是否有更快的方法。
这是我当前的代码

for i in range(0, len(temp)):
   temp[i] = cv2.cvtColor(temp[i], cv2.COLOR_BGR2HSV)
for k in range(0, len(temp)):
    for i in range(0, len(temp[k])):
        for j in range(0, len(temp[k][i])):
           if(temp[k][i][j][0] > 50 and temp[k][i][j][0] < 65 and temp[k][i][j][2] > 150):
               temp1[k][i][j][0] = 0
               temp1[k][i][j][1] = 0
               temp1[k][i][j][2] = 0
           else:
               temp1[k][i][j][0] = 255
               temp1[k][i][j][1] = 255
               temp1[k][i][j][2] = 255

temp 是我的 HSV 数组,temp1 是我正在创建的掩码

不是 cv2 专家,但如果它像 numpy 数组一样工作,那么 . . .

for i in range(0, len(temp)):
   temp[i] = cv2.cvtColor(temp[i], cv2.COLOR_BGR2HSV)
   temp1[i] = (1 - cv2.inRange(temp[i], (50, 0, 150), (65, 255, 255)).astype(int)) * 255