-1

我是 python 新手。我正在编写一个将相机输入转换为二进制形式的程序。它显示有 4 秒的延迟。我正在设计手势识别。所以这个 4 秒的延迟是不可接受的。谁能帮我 ?

import numpy as np
import cv2,cv 
from PIL import Image
from scipy.misc import imsave
import numpy
def binarize_image(image, threshold):
    """Binarize an image."""
    image = numpy.array(image)
    image = binarize_array(image, threshold)
    return image
def binarize_array(numpy_array, threshold):
    """Binarize a numpy array."""
    for i in range(len(numpy_array)):
        for j in range(len(numpy_array[0])):
            if numpy_array[i][j] > threshold:
                numpy_array[i][j] = 255
            else:
                numpy_array[i][j] = 0
    return numpy_array
cap = cv2.VideoCapture(0)
while(True):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    im_bw=binarize_image(gray, 50)


    cv2.imshow('frame',im_bw)
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break

cap.release()
cv2.destroyAllWindows() 
4

1 回答 1

1

您可以重写您的 binarize_array 以仅使用 numpy(这是您在使用 numpy 时应该始终尝试做的事情):

>>> a
array([[ 0.45789954,  0.05463345,  0.95972817],
       [ 0.32624327,  0.34981164,  0.4645523 ],
       [ 0.49630741,  0.44292407,  0.29463364]])
>>> mask = a > 0.5
>>> mask
array([[False, False,  True],
       [False, False, False],
       [False, False, False]], dtype=bool)
>>> a[mask] = 1
>>> a[~mask] = 0
>>> a
array([[ 0.,  0.,  1.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
于 2017-02-04T02:17:01.090 回答