cornerstonejs / cornerstone3D

Cornerstone is a set of JavaScript libraries that can be used to build web-based medical imaging applications. It provides a framework to build radiology applications such as the OHIF Viewer.
https://cornerstonejs.org
MIT License
492 stars 256 forks source link

How to export and import segmentation in stack? #1059

Open jlopes90 opened 5 months ago

jlopes90 commented 5 months ago

I had done the export or import of segmentation in volume, here is the link https://github.com/cornerstonejs/cornerstone3D/issues/664

Now what's missing is stack, does anyone know?

sedghi commented 5 months ago

we haven't done the import into stack yet, we are busy unfortunatelly with a lot of new contour segmentations. I get free next week and I will triage this

napest commented 5 months ago

we haven't done the import into stack yet, we are busy unfortunatelly with a lot of new contour segmentations. I get free next week and I will triage this

+1 for this issue. Highly needed

dima9911 commented 5 months ago

Hello dear @sedghi any update for this? i try to export/input brush data with cornerstoneTools.segmentation.state any methods, but nothing help

Can you give some time estimate for this feature?

sedghi commented 5 months ago

Sorry I'm not understanding the question, I need more context

dima9911 commented 5 months ago

@sedghi i have stack viewport with cornerstone3d library.

  1. i try to draw with brush tool circular on stack viewport,
  2. then i want export brush tool data, save to backend any format (for example annotation tools state - json)
  3. then redraw brush data to stack viewport with same dicom after page reload
napest commented 5 months ago

Hi Alireza @sedghi! I'll add a bit more context. We are developing a medical annotation tool. We need to export and import segmentations in some format. We found an example to export segmentation data to DICOM SEG, however:

  1. We don't understand how we can import it from SEG
  2. It doesn't work on stack viewports and we need segmentation on 2D images like mammograms/xray

The main criteria is to be able to draw segmentation -> save it on the server -> redraw it later

We (with dima9911) build it on vue js, so OHIF codebase on React doesn't help.

Could you please suggest a solution?

sedghi commented 5 months ago

As I mentioned the import and export from to stack viewport in DICOM SEG format is not done yet. So right now you can only create them.

If you want to save it in any format then you can get the segmentation imageId, which is inside the segmentation.state.getSegmentation(segmentationId), take a look inside the imageIdReferenceMap , then knowing your imageIds in the viewport (viewport.getImageIds) you can then find each segmentationImageId.

Next you need to grab each segmentation image (cache.getImage(segmentationImageId))), then there you have the getPixelData that will be a typed array and you can decide how to send it to your server.

Since labelmaps are usually sparse you can use RLE encoding to send less data over the network.

sedghi commented 5 months ago

The question regarding import is harder,

DICOM SEG import into stack viewport

We are working on writing that importer

Custom data from backend import

You need to create a local image and set the pixelData from the data you recieve from the server, you can use this one createAndCacheLocalImage

birdofpreyru commented 4 months ago

+1 to the issue importance. I'm trying to figure out the way to export / import the results of segmentation by SplineContourSegmentationTool. I might be missing something, but:

  1. Option A — I thought I can export the state of the created annotations, then import them... this fails because spline segmentation tool keeps in the annotation state instances of Spline (and probably some other classes). This gets wiped out if serialized / de-serialized. So, when imported the tool tries to call methods of the Spline instance inside the annotation state, and that crashes.

  2. Option B — Exporting the segmentation as a polyline, then importing it as a geometry, and showing it... would work but SegmentationDisplayTool does not draw into stack viewports :(

  3. Option C — Using 3D Volume viewport for segmentation & its rendering — fails for me because DICOM images I get have no imageOrientationPatient nor imagePositionPatient data (the images have XA modality). This crashes the volume loader. I kind of able to work it around with custom meta data providers injecting some made up orientation and position data, and thus avoiding the loader crash, but I end up with empty viewport... not sure because wrong values for orientation & position, or because something else is missing in these image metadata.

@sedghi can you advise something here? I am new to all this DICOM, medical imagery, and Cornerstone3D, thus might be missing something very basic here. It was kind of my basic assumption when signing up for a gig that if this library has segmentation tools that work on a image stack, the serialization / de-serialization of data they produce is a basic operation that should be supported... but after spending a way more time already on it than anticipated, and not being able to figure out the way, I am somewhat lost :(

jlopes90 commented 3 months ago

I just made a new Example Segmentation Stack example, see here #1186 #1205

tkt9k2562 commented 3 months ago

@jlopes90 Thank you very much.
Your example really works !

While I see a msg in console

image

I can reproduce with steps:

  1. Demo Dicom
  2. Use ThresholdCircle tool to label it , then Export Seg
  3. Refresh the page , then Demo Dicom and Import Seg

Although it seems not affect how to load segmentation , it just only shows a msg in console

Would you have idea ?

jlopes90 commented 3 months ago

@tkt9k2562 I cannot reproduce the error. If it works just ignore it for now.

jlopes90 commented 1 month ago

@sedghi news? export segmentation stack format in dicom?

sedghi commented 1 month ago

not yet, it is on my todo list

jlopes90 commented 1 month ago

I already got the generateGegmentation fix supported in just one frame.

PR https://github.com/dcmjs-org/dcmjs/pull/395

I've already tested an example of a "segmentation stack" edited in dcmjs. When exporting or importing the segmentation in just one frame and it works fine.

But we just have to wait for the pull request to be approved in dcmjs, and here we also need to update the new version of dcmjs. Next, I update the "segmentationStack" example.

jlopes90 commented 1 month ago

There is another way without using this pull request https://github.com/dcmjs-org/dcmjs/pull/395

How does just one frame work:

Cornerstone3D.Segmentation.generateSegmentation(
    [cacheImage, cacheImage],
    labelmapData,
    metaData
);

[cacheImage, cacheImage] both "cacheImage" are the same, just ignore it and it works only one frame.

jlopes90 commented 1 month ago

I just updated the segmentation stack example (deploy) https://deploy-preview-1336--cornerstone-3d-docs.netlify.app/live-examples/segmentationStack.html

PR https://github.com/cornerstonejs/cornerstone3D/pull/1336