-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(segmentGroups): support overlapping segments in SEG files
Each component in the image to convert is a non overlapping labelmap. Create another segment group for each component of image to convert. closes #672
- Loading branch information
Showing
6 changed files
with
210 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData'; | ||
import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray'; | ||
import vtkImageExtractComponentsFilter from '../imageExtractComponentsFilter'; | ||
|
||
describe('vtkImageExtractComponentsFilter', () => { | ||
it('should extract specified components', () => { | ||
// Create an image data with known scalar components | ||
const imageData = vtkImageData.newInstance(); | ||
imageData.setDimensions([2, 2, 1]); | ||
|
||
// Create scalar data with 3 components per voxel | ||
const scalars = vtkDataArray.newInstance({ | ||
numberOfComponents: 3, | ||
values: new Uint8Array([ | ||
// Voxel 0 | ||
10, 20, 30, | ||
// Voxel 1 | ||
40, 50, 60, | ||
// Voxel 2 | ||
70, 80, 90, | ||
// Voxel 3 | ||
100, 110, 120, | ||
]), | ||
}); | ||
|
||
imageData.getPointData().setScalars(scalars); | ||
|
||
// Create the filter and set components to extract | ||
const extractComponentsFilter = | ||
vtkImageExtractComponentsFilter.newInstance(); | ||
extractComponentsFilter.setComponents([0, 2]); // Extract components 0 and 2 | ||
extractComponentsFilter.setInputData(imageData); | ||
extractComponentsFilter.update(); | ||
|
||
const outputData = extractComponentsFilter.getOutputData(); | ||
const outputScalars = outputData.getPointData().getScalars(); | ||
const outputValues = outputScalars.getData(); | ||
|
||
// Expected output | ||
const expectedValues = new Uint8Array([ | ||
// Voxel 0 | ||
10, 30, | ||
// Voxel 1 | ||
40, 60, | ||
// Voxel 2 | ||
70, 90, | ||
// Voxel 3 | ||
100, 120, | ||
]); | ||
|
||
// Check if output matches expected values | ||
expect(outputScalars.getNumberOfComponents()).toBe(2); | ||
expect(outputValues).toEqual(expectedValues); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import macro from '@kitware/vtk.js/macro'; | ||
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData'; | ||
import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray'; | ||
|
||
function vtkImageExtractComponentsFilter(publicAPI, model) { | ||
model.classHierarchy.push('vtkImageExtractComponentsFilter'); | ||
|
||
publicAPI.requestData = (inData, outData) => { | ||
const inputData = inData[0]; | ||
const outputData = vtkImageData.newInstance(); | ||
|
||
const components = model.components; | ||
if (!components || !Array.isArray(components) || components.length === 0) { | ||
throw Error('No components specified for extraction.'); | ||
} | ||
|
||
const inputScalars = inputData.getPointData().getScalars(); | ||
const numInputComponents = inputScalars.getNumberOfComponents(); | ||
components.forEach((c) => { | ||
if (c < 0) { | ||
throw Error('Component index must be greater than or equal to 0.'); | ||
} | ||
if (c >= numInputComponents) { | ||
throw Error( | ||
'Component index must be less than the number of components in the input data.' | ||
); | ||
} | ||
}); | ||
|
||
outputData.shallowCopy(inputData); | ||
|
||
const inputArray = inputScalars.getData(); | ||
const numPixels = inputArray.length / numInputComponents; | ||
|
||
const outputNumComponents = components.length; | ||
const outputArray = new inputArray.constructor( | ||
numPixels * outputNumComponents | ||
); | ||
|
||
for (let pixel = 0; pixel < numPixels; pixel++) { | ||
for (let c = 0; c < components.length; c++) { | ||
outputArray[pixel * outputNumComponents + c] = | ||
inputArray[pixel * numInputComponents + components[c]]; | ||
} | ||
} | ||
|
||
outputData.getPointData().setScalars( | ||
vtkDataArray.newInstance({ | ||
numberOfComponents: outputNumComponents, | ||
values: outputArray, | ||
}) | ||
); | ||
|
||
outData[0] = outputData; | ||
}; | ||
} | ||
|
||
const DEFAULT_VALUES = { | ||
components: [], | ||
}; | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export function extend(publicAPI, model, initialValues = {}) { | ||
Object.assign(model, DEFAULT_VALUES, initialValues); | ||
|
||
macro.obj(publicAPI, model); | ||
|
||
macro.algo(publicAPI, model, 1, 1); | ||
|
||
macro.setGet(publicAPI, model, ['components']); | ||
|
||
vtkImageExtractComponentsFilter(publicAPI, model); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export const newInstance = macro.newInstance( | ||
extend, | ||
'vtkImageExtractComponentsFilter' | ||
); | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export default { newInstance, extend }; |
This file was deleted.
Oops, something went wrong.