'How to create segmentation annotations from mask in COCO Format?
I am trying to create my own dataset in COCO format. However, I have some challenges with the annotation called segmentation. I tried to reproduce it by finding the edges and then getting the coordinates of the edges. However, this is not exactly as it in the COCO datasets. As I see it, the annotation segmentation pixels are next to eachother.
Heres an simple example of a COCO dataset:
"annotations": [
{
"segmentation": [[510.66,423.01,511.72,420.03,...,510.45,423.01]],
"area": 702.1057499999998,
"iscrowd": 0,
"image_id": 289343,
"bbox": [473.07,395.93,38.65,28.67],
"category_id": 18,
"id": 1768
},
This is my try, however, it is not correct.
from PIL import Image, ImageFilter
import numpy as np
import matplotlib.pyplot as plt
# Opening the image (R prefixed to string
# in order to deal with '\' in paths)
image = Image.open("hjerte.png")
# Converting the image to grayscale, as edge detection
# requires input image to be of mode = Grayscale (L)
image = image.convert("L")
# Detecting Edges on the Image using the argument ImageFilter.FIND_EDGES
image = image.filter(ImageFilter.FIND_EDGES)
img_in_numpy = np.asarray(image)
plt.imshow(img_in_numpy, cmap='gray')
Which outputs the following image:
I am then able to achieve the coordinates around it with the following code:
edge_coordinates = np.where(img_in_numpy != 0)
segmentation = []
for x, y in zip(edge_coordinates[0], edge_coordinates[1]):
segmentation.extend([x,y])
But then the coordinates is not next to each other. I have tried to search for a script that does this, but I havent been able to find it. Can anyone help me with this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

