-1
我是python的新手。我正在编写一个将相机输入转换为二进制格式的程序。它显示了那里4秒的滞后。我正在设计手势识别。所以这4秒的延迟是不可接受的。谁能帮我 ?提高Python中图像处理循环的速度
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()
我会首先使用Profiler来寻找热点开始。这应该是你为这种情况达到的第一个工具之一。 – Carcigenicate
你的形象有多大?我认为你的binarize_arry函数效率很低,因为它在普通的python嵌套循环中单独处理numpy数组中的所有值。在处理numpy时,这是一个不可能的事情。研究在整个阵列上运行的numpy特定操作 –
我从我的摄像头获取输入,其中1280 * 720 –