如何用代码的矢量化版本替换此处的“for”循环?

问题描述 投票:0回答:0

我有这个功能可以对给定的图像执行“形态学开放”-

def bwareaopen(img, min_size=10000, connectivity=8):
    
    ### Find all connected components (called here "labels")
    num_labels, labels, stats, centroids = cv.connectedComponentsWithStats(img,    
    connectivity=connectivity)
    
    ### Remove connected components smaller than min_size
    for i in range(num_labels):
        if stats[i, cv.CC_STAT_AREA] < np.around(min_size*(rsz**2)): img[labels == i] = 0
            
    return img

如何用矢量化实现替换此函数中的“for loop”?我想这样做是因为当我在大型图像堆栈(尺寸:2160 x 2560 x 300)上运行此功能时,在我的系统上执行时间太长(> 3 小时)。

如有任何帮助或建议,我们将不胜感激。

谢谢

我无法弄清楚如何在 Python 中矢量化 img[labels == i] = 0 部分。我尝试使用“pymlfunc”库中的“sub2ind”函数,但似乎卡住了。

python numpy image-processing vectorization
© www.soinside.com 2019 - 2024. All rights reserved.