我正在尝试使用SLIC变体进行语义分割,并希望为原始图像创建一个蒙版,其中基于可用的基于点的注释为每个片段着色(根据其类)。如果该段中没有基于点的注释,则保留为0。
我目前有一张图像的x,y点及其关联的标签,以及一种(缓慢的)方法来查找和着色所需的片段。我对向量化很熟悉,或者说“ pythonic”正在做事,但是我似乎无法加快最后一个for循环的速度,并且不喜欢优化方面的一些建议或参考。谢谢。
# Point-based annotations
annotation = pd.read_csv("a_dataframe.csv") # [X, Y, Label]
color_label = {'class 1' : 25, 'class 2' : 50, 'class 3' : 75}
# Uses CPU to create single segmented image with current params
slic = SlicAvx2(num_components = n_segments, compactness = n_compactness)
segmented_image = slic.iterate(cv2.cvtColor(each_image, cv2.COLOR_RGB2LAB))
# Finds the segments of interest and records their ID
X = np.array(each_annotation.iloc[:, 0], dtype = 'uint8')
Y = np.array(each_annotation.iloc[:, 1], dtype = 'uint8')
L = np.array(each_annotation.iloc[:, 2], dtype = 'str') # Labels
DS = segmented_image[X, Y] # Desired Segments
# Empty mask, marks the segments of interest with the classes of the point in them
mask = np.zeros(each_image.shape[:2], dtype = "uint8")
# Would ideally like to find a more quickly way of doing this
for (index, segVal) in enumerate(DS):
mask[segmented_image == segVal] = color_label.get(L[index])
本质上,我想在这里替换该循环:
[mask[segmented_image == s] for i, s in enumerate(DS)]
但是我无法在mask
中用适当的标签分配X,Y位置。我认为这将类似于以下内容:
[mask[segmented_image == s] for i, s in enumerate(DS)] = color_label.get(L[i])
但是似乎我正在尝试为要生成的列表分配颜色值 ...