How to deal with .mha files

How to deal with .mha files  

  By: vertin on Aug. 21, 2024, 4:15 p.m.

In Docker, .mha is read as a numpy array and then placed into the model for segmentation, but the AI I trained divides .jpg and outputs .png format images, which is different from what is in Docker. I have an idea now, but I don't know if it's feasible:

First, convert the .mha format file to a .jpg image, perform AI segmentation, and then convert the output image to .mha format.

I also have two more questions:

  1. When saving the. mha file in Docker, only the numpy array was written, but I found there is a header describing the data in the .mha file. I don't know if the absence of this header will affect the entire file.

  2. I only found tutorials on converting .png to .mhd (similar to .mha, but it separate the header and image data in two files), but I couldn't find how to save it as .mha. Can I directly change .mhd to .mha in the code to save files that meet the requirements? However, this may result in errors or lack of headers describing the data.

 Last edited by: vertin on Aug. 21, 2024, 4:16 p.m., edited 1 time in total.

Re: How to deal with .mha files  

  By: ezequieldlrosa on Aug. 22, 2024, 11:42 a.m.

Hi Vertin,

I dont have an exact solution for this. You can try doing the double conversion -mha to jpg and png to mha-. What is important is that you dont change the final input reading and output export of the example docker. But in between them, do whatever is needed (e.g. conversions).

My suggestion: I guess your AI model does not output a .png, but just an array that you later on export as .png. You can do the same with .mha: stack the AI predictions (before png conversion) into a 3D volume -same dimension as the input- and (skip exporting png) but export it as .mha, using the code provided in the example-docker.

If you wanna convert any example data .nii image to .mha (to try that things work on your side), its straightforward using grand-challenge tool: https://github.com/DIAGNijmegen/rse-panimg Other formarts are supported also there.

Good luck, Ezequiel