Converting an np.unint8 to a GeoJSON file that can be imported to QuPath

154 Views Asked by At

I'm trying to convert the tissue "masks" created in one program (basically regions of tissue annotated on a tissue slide) and I wanted to import them into a program known as QuPath.

The "mask" files are essentially a series of shapes stored as np.unint8 arrays, each value being a pixel, where a positive value corresponds to the presence of tissue and a 0 corresponds to no tissue being present. As an example, this mask:

[[ 0, 0, 0, 0 ],

[ 0, 1, 1, 0 ],

[ 0, 1, 1, 0 ],

[ 0, 0, 0, 0 ]]

would correspond to a 4x4 square.

The arrays are all in a larger H5 file along with associated metadata.

The program I want to import them into (QuPath) only accepts annotations in a GeoJSON format. Is there an existing method to convert pixel-by-pixel arrays into a GeoJSON format or will this have to be a novel method?

Here's the code I have to extract the arrays from the H5 file.

# open and load file
asset_directory = 'D:\pathai_masks'
file_name = os.path.join(asset_directory, "path5_mask.h5")
h5_file = h5py.File(file_name, 'r')
 
# access attributes like a regular dictionary 
# ex. access'wsi_masks'
wsi_masks = h5_file['wsi_masks']

# Load the JSON from the H5 file:
necrosis_dataset = h5_file["wsi_masks"]["predicted_region_mask_l1_5"]

# Encodes as a numpy.ndarray of uint8 
necrosis_numpy_array = necrosis_dataset[:, :]

This is what I have to convert the array directly to a GeoJSON file.

coordinates = []

for row in necrosis_numpy_array:
    coordinates.append(row.tolist())

geojson = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": coordinates
            }
        }
    ]
}

json_data = json.dumps(geojson)

And this is the format that QuPath accepts.

{
        "type": "Feature",
        "id": "PathDetectionObject",
        "geometry": {
            "type": "Polygon",
            "coordinates": [[[17738.72, 42238], [17737.94, 42240.13], [17738.39, 42242.34], [17737.21, 42244.28], [17737.19, 42246.54], [17739.74, 42250.23], [17743.86, 42248.63], [17743.7, 42246.37], [17745.05, 42242.08], [17748.38, 42239.13], [17747.76, 42238.25], [17738.72, 42238]]]
        },
        "properties": {
            "isLocked": false,
            "measurements": [],
            "classification": {
                "name": "Other",
                "colorRGB": -377282
            }
        }
    }
1

There are 1 best solutions below

0
Gelliant On

You can use opencv to find the polygons with findContours. Then use geojson to export.