def search():
    for q in np_query:
        c = [(i, cv2.Mahalanobis(q, x, icovar)) for i, x in enumerate(
            np_gallery)]
ids.append(map(lambda tup: tup[0], sorted(c, key=lambda tup: tup[1])[0:K]))
Nearest neighbors with uncertain points