I am trying to convert a numpy array of arrays. An example input is this:
np.array([[[0, 0, 0], [255, 255, 255]], [[255, 255, 255], [0, 0, 0]]], np.uint8)
I want to replace all arrays with values [0, 0, 0] with 0 and the [255, 255, 255] with 255. I want the final result to be in this format:
np.array([[0, 255], [255, 0]], np.uint8)
The array will always have the above values but will be different size.
This is the code I created for doing this:
array_list = []
for row in input_image:
row_list = []
for item in row:
if np.array_equal(item, [0, 0, 0]):
row_list.append(0)
else:
row_list.append(255)
array_list.append(row_list)
output_image = np.array(array_list, np.uint8)
The above code is very slow for big arrays and I was thinking that there might be a way to do this directly with numpy, though I couldn't find a way. Do you have any suggestions for doing this more efficient?
[1, 128, 32], or even[255, 0, 0]?input_image = np.where(input_image != 255, 0, input_image)[255, 0, 255]as a value. How do your plan on handling that?ais the input, you could usea[:, :, 0], ora[..., 0].