label_seg = np.zeros(img.shape,dtype=np.uint8)
for i in range(mask_labels.shape[0]):
label_seg[np.all(img == list(mask_labels.iloc[i, [1,2,3]]), axis=-1)] = i
label_seg = label_seg[:,:,0]
np.random.seed(0)
img = np.random.randint(0, 255, (5, 5, 3))
mask_labels = pd.DataFrame([[0, 58, 193, 230],
[0, 127, 32, 31],
[0, 193, 9, 185],
])
# generate an output of the same shape as the input image
label_seg = np.zeros(img.shape, dtype=np.uint8)
# for each row in the DataFrame
for i in range(mask_labels.shape[0]):
# take the columns 1,2,3
# if all 3 values match in the img array (same x/y position, all 3 channels)
# assign the row index in the output array at the same x/y position
label_seg[np.all(img == list(mask_labels.iloc[i, [1,2,3]]), axis=-1)] = i
label_seg = label_seg[:,:,0]
1条答案
按热度按时间o2g1uqev1#
我的(疯狂的)猜测,因为你没有提供输入的例子。
此代码采用3通道图像
img
(例如RGB),并将其与DataFramemask_labels
中的已知颜色进行比较,然后在新数组label_seg
中分配颜色的索引以识别匹配项。我认为代码中有一些错误:
0
,这也是输入中的默认值。使用示例:
输出
label_seg
:img
(作为图像,每个单元格是一个像素):masked_labels
:颜色: