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
571 stars 290 forks source link

Segmentation tool doesn't show segments has Labelmap index over 255 #1256

Open mrsaleh opened 5 months ago

mrsaleh commented 5 months ago

Describe the Bug

I've loaded a label map volume, everything works OK , the only issue I have is that segments with indices over 255 doesn't show up. I have no error , when I change the visibility of these indices over 255, it works. but the viewport doesn't render them. I can't see them. here's my code to load label map volume , and add segmentation:

`public async LoadLabelMap(volumeId:string,niftiURL:string,segmentationId: string,labelmaps :LabelMap[]){ this.labelMaps = labelmaps;

    const colorHexToRGBA = (hex)=>{    
        var rgb = parseInt(hex, 16);    
        var r   = (rgb >> 16) & 0xFF;   
        var g = (rgb >> 8) & 0xFF;     
        var b  = rgb & 0xFF;   
        return [b,g,r,255]
    }

    const config = segmentation.config.getGlobalConfig();
    console.log(config);
    config.renderInactiveSegmentations = false
    config.representations.LABELMAP.renderOutline = false
    config.representations.LABELMAP.outlineWidthInactive = 0
    config.representations.LABELMAP.renderFillInactive = false;
    config.representations.LABELMAP.renderFill = true;
    config.representations.LABELMAP.fillAlpha = 1.0;
    segmentation.config.setGlobalConfig(config)

    const labelmapVolumeId = 'nifti:' + niftiURL;      

    await volumeLoader.createAndCacheVolume(labelmapVolumeId)

    const destVolume =  await volumeLoader.createAndCacheDerivedSegmentationVolume(volumeId,{
        volumeId: segmentationId            
    });  

    const sourceVolume = cache.getVolume(labelmapVolumeId);

    const { dimensions } = sourceVolume;
    const sourceData = sourceVolume.getScalarData();

    const destData = destVolume.getScalarData()

    console.log(dimensions)

    const keySet =  new Set<number>();
    let voxelIndex = 0;
    for (let z = 0; z < dimensions[2]; z++) {
        for (let y = 0; y < dimensions[1]; y++) {
          for (let x = 0; x < dimensions[0]; x++) {
            const data = sourceData[voxelIndex];
            destData[voxelIndex] = sourceData[voxelIndex]
            voxelIndex++;
            keySet.add(data);
          }
        }
    }

    console.log(keySet)

    destVolume.modified();

    segmentation.addSegmentations([
        {
          segmentationId,
          representation: {
            // The type of segmentation
            type: csToolsEnums.SegmentationRepresentations.Labelmap,
            // The actual segmentation data, in the case of labelmap this is a
            // reference to the source volume of the segmentation.
            data: {
              volumeId: segmentationId,                  
            },                
          },
        },
    ]);

    const maxLabel = Math.max(...labelmaps.map(l=>l.num));
    const colorLUT = Array<Types.Color>();

    console.log(maxLabel)
    for(let i=0;i<=maxLabel;i++){            
        colorLUT.push([0,0,0,0] as Types.Color)
    }
    labelmaps.forEach(label=>{
        colorLUT[label.num] = colorHexToRGBA(label.color) as Types.Color;
    })

    segmentation.config.color.addColorLUT(colorLUT, 0)

    const segmentationRepUUIDs = await segmentation.addSegmentationRepresentations(ToolsGroupId, [
        {
            segmentationId: segmentationId,
            type: csToolsEnums.SegmentationRepresentations.Labelmap,  
            options: {
                colorLUTOrIndex: 0,
            }
        }
    ])            

    this.segmentationRepUUID = segmentationRepUUIDs[0]

}`

Steps to Reproduce

Just load a label map that have data over 255.

The current behavior

It shows the segments with index lower than 255

The expected behavior

Show all the segments

OS

Windows 10

Node version

20

Browser

Firefox 125.0.3

mrsaleh commented 5 months ago

I found the source of the issue limited to 255 colors I wanted to make a pull request , but there's a comment above of the line , that try justify this limitation

// Note: MAX_NUMBER_COLORS = 256 is needed because the current method to generate
// the default color table uses RGB.

Though I don't know how these two relates.

sedghi commented 5 months ago

There is no support for more than 255 segments right now