numpy 用Python编写循环需要花费大量时间

lc8prwob  于 2023-08-05  发布在  Python
关注(0)|答案(1)|浏览(72)

我在python中使用openCV制作了这个蓝色的滤镜,它可以工作,但是视频输出滞后。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)  

while True:
    ret, frame = cap.read()  
    for i in range(frame.shape[0]):
        for j in range(frame.shape[1]):
            frame[i][j] = [255,frame[i][j][1],frame[i][j][2]]

    cv2.imshow('frame',frame)

    if cv2.waitKey(1) == ord('q'):
        break

cap.release()  
cv2.destroyAllWindows()

字符串

csga3l58

csga3l581#

您可以在NumPy中使用向量化操作来避免显式循环:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    
    frame[:, :, 0] = 255  # Set blue channel to 255 (full blue)
    
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

字符串

相关问题