我是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()
答案 0 :(得分: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.]])