Blender RGB masking with segmentation. How to? If possible
You can try with this solution: Create a segmentation picture with each object class rendered in different color ?.
It worked for me.

Moreover, if you want sharp edges you can set render properties -&gt; film -&gt; filter size for eevee to 0.00.