I have a NumPy array with shape (300, 500). Consider this as an image with size (300, 500)
and there are 100 objects on it that I want to fill each of them with a different value.
image = np.zeros((300, 500))
I have bounding-box coordinates (x_min, x_max, y_min, y_max) for each of these objects. Then I create indexing arrays using these bounding-box coordinates.
array_of_x_indexing_arrays = []
array_of_y_indexing_arrays = []
for obj in objects:
x_indices, y_indices = np.mgrid[obj.x_min: obj.x_max + 1, obj.y_min: obj.y_max + 1]
x_indices, y_indices = x_indices.ravel(), y_indices.ravel()
array_of_x_indexing_arrays.append(x_indices)
array_of_y_indexing_arrays.append(y_indices)
Then, I want to assign a different value to image for each of these objects. I stored them the values for each object in an array with shape (100,)
data = np.array((100,))
# Assume that I filled data such as
# data[0] = 10
# data[1] = 2
# ...
# data[99] = 3
Then what I want to do is following
for i in range(len(objects)):
image[array_of_y_indexing_arrays[i], array_of_x_indexing_arrays[i]] = data[i]
But I want to do this in NumPy way, I have tried the following but does not work
image[array_of_y_indexing_arrays, array_of_x_indexing_arrays] = data