chrise96 / image-to-coco-json-converter

Convert segmentation RGB mask images to COCO JSON format
199 stars 57 forks source link

Grayscale image #10

Closed amida47 closed 2 years ago

amida47 commented 2 years ago

I want to use your code but I have grayscale masks, where each category is coded with a value, could you guide me to what should be changed in order to achieve this. Thanks

chrise96 commented 2 years ago

You can do something like this to get all RGB color codes in the image:

from PIL import Image
from collections import defaultdict

im = Image.open('example.jpg')
by_color = defaultdict(int)
for pixel in im.getdata():
     by_color[pixel] += 1
by_color

Next, add/change category_colors in the notebook.

amida47 commented 2 years ago

But I have grayscale masks so there is only one channel to the image and your code expect an 3 channels RGB image

chrise96 commented 2 years ago

Ah! Maybe not the best solution, but I would convert them to rgb: backtorgb = cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB)