Create A single channel Target from RGB mask

I have an RGB image which is a segmentation ground truth. It has only two classes, Blue for the background and Red for the foreground. ( Most pixels are transparent)

Now I want to create a mask of size (h,w) where the pixel is 1 if the original corresponding mask’s pixel was blue, 2 if red, and 0 otherwise.

Any idea how do I go about doing this?

You could create a color mapping as described here to transform the color values to the class indices.