-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compare-images): add compare-images.ts
- Loading branch information
Showing
4 changed files
with
87 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
Image, | ||
} from 'itk-wasm' | ||
|
||
import CompareDoubleImagesOptions from './compare-double-images-options.js' | ||
import CompareDoubleImagesResult from './compare-double-images-result.js' | ||
import compareDoubleImages from './compare-double-images.js' | ||
|
||
import toScalarDouble from './to-scalar-double.js' | ||
import vectorMagnitude from './vector-magnitude.js' | ||
|
||
/** | ||
* Compare images with a tolerance for regression testing. | ||
* | ||
* For multi-component images, the intensity difference threshold | ||
* is based on the pixel vector magnitude. | ||
* | ||
* @param {Image} testImage - The input test image | ||
* @param {CompareDoubleImagesOptions} options - options object | ||
* | ||
* @returns {Promise<CompareDoubleImagesResult>} - result object | ||
*/ | ||
async function compareImages( | ||
webWorker: null | Worker, | ||
testImage: Image, | ||
options: CompareDoubleImagesOptions = { baselineImages: [] as Image[], } | ||
) : Promise<CompareDoubleImagesResult> { | ||
|
||
async function vectorMagnitudeWorker(image: Image) { | ||
const { webWorker: usedWebWorker, magnitudeImage } = await vectorMagnitude(null, image) | ||
usedWebWorker?.terminate() | ||
return { magnitudeImage } | ||
} | ||
|
||
const testImageDouble = await toScalarDouble(vectorMagnitudeWorker, testImage) | ||
const baselineImagesDouble = await Promise.all(options.baselineImages.map(async (image) => { | ||
return await toScalarDouble(vectorMagnitudeWorker, image) | ||
})) | ||
|
||
const otherOptions = { ...options } | ||
otherOptions.baselineImages = baselineImagesDouble | ||
|
||
return compareDoubleImages(webWorker, testImageDouble, otherOptions) | ||
} | ||
|
||
export default compareImages |
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
34 changes: 34 additions & 0 deletions
34
packages/compare-images/typescript/src/to-scalar-double.ts
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,34 @@ | ||
import { | ||
Image, | ||
FloatTypes, | ||
PixelTypes, | ||
castImage, | ||
} from 'itk-wasm' | ||
|
||
import VectorMagnitudeNodeResult from './vector-magnitude-node-result' | ||
|
||
async function toScalarDouble(vectorMagnitudeFn: (img: Image) => Promise<VectorMagnitudeNodeResult>, image: Image): Promise<Image> { | ||
let scalarDouble = image | ||
|
||
if (scalarDouble.imageType.componentType !== FloatTypes.Float64) { | ||
let pixelType = undefined | ||
if (image.imageType.pixelType !== PixelTypes.Scalar && image.imageType.pixelType !== PixelTypes.VariableLengthVector) { | ||
pixelType = PixelTypes.VariableLengthVector | ||
} | ||
scalarDouble = castImage(image, { componentType: FloatTypes.Float64, pixelType }) | ||
} else { | ||
if (image.imageType.pixelType !== PixelTypes.Scalar && image.imageType.pixelType !== PixelTypes.VariableLengthVector) { | ||
const pixelType = PixelTypes.VariableLengthVector | ||
scalarDouble = castImage(image, { pixelType }) | ||
} | ||
} | ||
|
||
if (scalarDouble.imageType.pixelType === PixelTypes.VariableLengthVector) { | ||
const magnitude = await vectorMagnitudeFn(scalarDouble) | ||
scalarDouble = magnitude.magnitudeImage | ||
} | ||
|
||
return scalarDouble | ||
} | ||
|
||
export default toScalarDouble |