I have an image (segmentation map) which can be simplified to the following:
import numpy as np
x, y = np.indices((80, 80))
x1, y1, x2, y2 = 28, 28, 44, 52
r1, r2 = 16, 20
mask_circle1 = (x - x1) ** 2 + (y - y1) ** 2 < r1 ** 2
mask_circle2 = (x - x2) ** 2 + (y - y2) ** 2 < r2 ** 2
image = np.max([mask_circle1, mask_circle2*2], axis=0)
This image
contains two circles labeled 1 and 2 respectively. I now want to relabel all values according to a old_value : new_value
map / dictionary I have.
relabels = {1: 7, 2: 3}
The naive solution to this problem would be the following:
labels = []
for old_val, new_val in relabels.items():
labels.append(np.where(image==old_val, new_val, 0))
new_image = np.max(labels, axis=0)
This works flawlessly but is rather slow when scaled to larger arrays with more labels. Is there any way to vectorise this operation instead of processing all relabelling steps one by one?
Thanks in advance.