对于大于0的值,在2d数组中搜索最快

时间:2018-05-19 12:21:11

标签: python performance loops dataframe time

这是我的代码:

    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

idealy函数np.where()应该在第一个结果上停止,然后从结尾开始并执行相同的操作

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用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)