在 2D 数组中以最快的速度搜索大于 0 的值



这是我的代码:

if frame is not None: self.x, self.y = np.where(frame > 0)

基本上它的搜索值大于 0,但它需要永远,我需要它每秒至少对数组 480x640 执行 10 次。如果它有帮助,它总是只是其中的几点。我正在附加我的整个班级,也许有人会改进它:(这是编辑后(

import numpy as np

class Rect:
def __init__(self):
self.rect = [(0, 0), (0, 0)]
self.draw = False
self.x = []
self.y = []
def count_rect(self):
self.rect[0] = (self.y[0], self.x[0])
element, index = max(list(zip(self.y, range(len(self.y)))))
self.rect[1] = (element + 20, self.x[index] + 150)
self.draw = True
def points(self, frame):
if frame is not None: self.x, self.y = np.where(frame > 0)
if len(self.x) != 0 and len(self.y) != 0: self.count_rect()
else: self.draw = False

理想情况下,函数 np.where(( 应该在第一个结果上停止,然后从最后开始并执行相同的操作

你试过使用 where 函数吗?

https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.where.html

它返回一个索引,其中包含 x,y 坐标以及满足条件的点。 例如:

import numpy as np
#being A  your NxM array
index = np.where(A > o)

最新更新