I am performing an image segmentation tasks. I am converting images to labels but the issue with images is due to artefact of jpg compression, there are intermediate colors present in images.So for an image which is supposed to have 4 colors(for my case), they are having many colors.for instance, the below image has 338 colors present in it-
which I checked using following code-
image = Image.open("Image_Path")
image = np.array(image)
target = torch.from_numpy(image)
h,w = target.shape[0],target.shape[1]
masks = torch.empty(h, w, dtype=torch.long)
colors = torch.unique(target.view(-1,target.size(2)),dim=0).numpy()
To resolve this problem, I tried this approach but the problem is it is converting the image in the non pre-determined pixel values.It converts the above image into the following pixel values-
array([[ 0, 0, 0],
[ 0, 0, 254],
[ 0, 254, 0],
[254, 0, 0]]
which is kinda problematic for me because I have different images and they all need to have same pixel values for each colors for every image but using the above method, it is not same for other images, it may convert red colored image to [255,0,0]
or similarly other colors too.
How to do it?