0

I am trying to implement a multiclass image segmentation task. The mask of the image is of the following kind-

Mask

I need to convert the image into labels corresponding to each class where classes are red,green,blue and black(background),which I am doing using the below code-

 def mask_to_class(self,mask):
    target = torch.from_numpy(mask)
    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()
    target = target.permute(2, 0, 1).contiguous()
    mapping = {tuple(c): t for c, t in zip(colors.tolist(), range(len(colors)))}
    for k in mapping:
        idx = (target==torch.tensor(k, dtype=torch.uint8).unsqueeze(1).unsqueeze(2))
        validx = (idx.sum(0) == 3) 
        masks[validx] = torch.tensor(mapping[k], dtype=torch.long)
   return masks

The issue is number of distinct colors present in image should come out to be 4 but they are coming 338 in this case, also they are coming irregular in different images too.

Reproduction of the problem could be done using the below 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()
print(colors.shape)

(338, 3)

The shape of colors should be (4,3) but it is coming 338,3).I am unable to find the cause behind it.Below is another image which is identical with the above image with respect to colors present and is giving (4,3) colors shape which is required. Where I am doing wrong? Image 2

Beginner
  • 721
  • 11
  • 27
  • 1
    Is your image a jpg? In that cases you may have many intermediate colours, artefact of jpg compression. Use png or tiff, which keep the exact colour of every pixel). Or pass jpg in a filter to get just the 4 colours. – Giacomo Catenazzi Sep 17 '19 at 14:56
  • Yes,I have 2 kind of images - jpg and bmp. so should i save every image with tiff or png extension or do I need to convert them in a certain way. Could you please elaborate the passing `jpg in a filter` method? – Beginner Sep 17 '19 at 14:59
  • I checked the second image, and I find no artefact from jpg, but in the green lines I see an aliasing. "filter": just quantize the colours, e.g. if L < 0.2: black, if red and green < 0.1: blue, if blue and green < 0.1: red, else green. But save the result and check, maybe you should change the parameters. – Giacomo Catenazzi Sep 17 '19 at 15:08
  • I tried quantizing using the method below but I am still not getting the desired shape.https://stackoverflow.com/a/237193/6642287 .Where I am doing wrong? – Beginner Sep 17 '19 at 17:35
  • i used this code to check the unique colors in both the image. """colors, count = np.unique(a.reshape(-1,a.shape[-1]), axis=0, return_counts=True)""" print(colors) obvious that the first image had more colors. and the second image has only 4 colors. probable and quick solution/workaround will be thresholding – venkata krishnan Sep 18 '19 at 01:32

0 Answers0